Class | Erubis::Context |
In: |
lib/erubis/context.rb
|
Parent: | Object |
context object for Engine#evaluate
ex.
template = <<'END' Hello <%= @user %>! <% for item in @list %> - <%= item %> <% end %> END context = Erubis::Context.new(:user=>'World', :list=>['a','b','c']) # or # context = Erubis::Context.new # context[:user] = 'World' # context[:list] = ['a', 'b', 'c'] eruby = Erubis::Eruby.new(template) print eruby.evaluate(context)
# File lib/erubis/context.rb, line 34 34: def initialize(hash=nil) 35: hash.each do |name, value| 36: self[name] = value 37: end if hash 38: end
# File lib/erubis/context.rb, line 40 40: def [](key) 41: return instance_variable_get("@#{key}") 42: end
# File lib/erubis/context.rb, line 44 44: def []=(key, value) 45: return instance_variable_set("@#{key}", value) 46: end
# File lib/erubis/context.rb, line 52 52: def each 53: instance_variables.each do |name| 54: key = name[1..-1] 55: value = instance_variable_get(name) 56: yield(key, value) 57: end 58: end
# File lib/erubis/context.rb, line 48 48: def keys 49: return instance_variables.collect { |name| name[1..-1] } 50: end
# File lib/erubis/context.rb, line 60 60: def to_hash 61: hash = {} 62: self.keys.each { |key| hash[key] = self[key] } 63: return hash 64: end
# File lib/erubis/context.rb, line 66 66: def update(context_or_hash) 67: arg = context_or_hash 68: if arg.is_a?(Hash) 69: arg.each do |key, val| 70: self[key] = val 71: end 72: else 73: arg.instance_variables.each do |varname| 74: key = varname[1..-1] 75: val = arg.instance_variable_get(varname) 76: self[key] = val 77: end 78: end 79: end