Class: Erubis::Context
- Inherits:
-
Object
- Object
- Erubis::Context
- Includes:
- Enumerable
- Defined in:
- lib/erubis/context.rb
Overview
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)
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #each ⇒ Object
-
#initialize(hash = nil) ⇒ Context
constructor
A new instance of Context.
- #keys ⇒ Object
- #to_hash ⇒ Object
- #update(context_or_hash) ⇒ Object
Constructor Details
#initialize(hash = nil) ⇒ Context
Returns a new instance of Context.
34 35 36 37 38 |
# File 'lib/erubis/context.rb', line 34 def initialize(hash=nil) hash.each do |name, value| self[name] = value end if hash end |
Instance Method Details
#[](key) ⇒ Object
40 41 42 |
# File 'lib/erubis/context.rb', line 40 def [](key) return instance_variable_get("@#{key}") end |
#[]=(key, value) ⇒ Object
44 45 46 |
# File 'lib/erubis/context.rb', line 44 def []=(key, value) return instance_variable_set("@#{key}", value) end |
#each ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/erubis/context.rb', line 52 def each instance_variables.each do |name| key = name[1..-1] value = instance_variable_get(name) yield(key, value) end end |
#keys ⇒ Object
48 49 50 |
# File 'lib/erubis/context.rb', line 48 def keys return instance_variables.collect { |name| name[1..-1] } end |
#to_hash ⇒ Object
60 61 62 63 64 |
# File 'lib/erubis/context.rb', line 60 def to_hash hash = {} self.keys.each { |key| hash[key] = self[key] } return hash end |
#update(context_or_hash) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/erubis/context.rb', line 66 def update(context_or_hash) arg = context_or_hash if arg.is_a?(Hash) arg.each do |key, val| self[key] = val end else arg.instance_variables.each do |varname| key = varname[1..-1] val = arg.instance_variable_get(varname) self[key] = val end end end |