Class: TemplatePage::Context
- Inherits:
-
Object
- Object
- TemplatePage::Context
- Defined in:
- lib/rdoc/template.rb
Overview
A context holds a stack of key/value pairs (like a symbol table). When asked to resolve a key, it first searches the top of the stack, then the next level, and so on until it finds a match (or runs out of entries)
Instance Method Summary collapse
-
#find_scalar(key) ⇒ Object
Find a scalar value, throwing an exception if not found.
-
#initialize ⇒ Context
constructor
A new instance of Context.
-
#lookup(key) ⇒ Object
Lookup any key in the stack of hashes.
- #pop ⇒ Object
- #push(hash) ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
47 48 49 |
# File 'lib/rdoc/template.rb', line 47 def initialize @stack = [] end |
Instance Method Details
#find_scalar(key) ⇒ Object
Find a scalar value, throwing an exception if not found. This method is used when substituting the %xxx% constructs
62 63 64 65 66 67 68 69 |
# File 'lib/rdoc/template.rb', line 62 def find_scalar(key) @stack.reverse_each do |level| if val = level[key] return val unless val.kind_of? Array end end raise "Template error: can't find variable '#{key}'" end |
#lookup(key) ⇒ Object
Lookup any key in the stack of hashes
73 74 75 76 77 78 79 |
# File 'lib/rdoc/template.rb', line 73 def lookup(key) @stack.reverse_each do |level| val = level[key] return val if val end nil end |
#pop ⇒ Object
55 56 57 |
# File 'lib/rdoc/template.rb', line 55 def pop @stack.pop end |
#push(hash) ⇒ Object
51 52 53 |
# File 'lib/rdoc/template.rb', line 51 def push(hash) @stack.push(hash) end |