Sha256: dbb09b2dd06f1478273a3be993dea5e2dfa6219bf3ce119dd54cf957a95db6b3
Contents?: true
Size: 989 Bytes
Versions: 2
Compression:
Stored size: 989 Bytes
Contents
require 'erb' module Eff class Template attr_reader :result, :src def initialize(src, erb_vars = {}) @src = ERB.new(src) self.erb_vars = erb_vars end def erb_vars=(value) @erb_vars = normalize_erb_vars(value) interpolate_result @erb_vars end private def erb_vars @erb_vars end def interpolate_result @result = erb_binding_wrapper do |binding| @src.result(binding) end end def erb_binding_wrapper(&block) set_erb_vars value = yield(binding) unset_erb_vars value end def set_erb_vars erb_vars.each do |erb_var, value| instance_variable_set erb_var, value end end def unset_erb_vars erb_vars.each do |erb_var, value| remove_instance_variable erb_var end end def normalize_erb_vars(erb_vars) Hash[ erb_vars.to_h.map { |k, v| ["@#{k}".to_sym, v] } ] end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
eff-0.0.2 | lib/eff/template.rb |
eff-0.0.1 | lib/eff/template.rb |