Class TemplatePage::Context
In: template.rb
Parent: Object

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)


find_scalar   lookup   new   pop   push  

Public Class methods


    # File template.rb, line 47
47:     def initialize
48:       @stack = []
49:     end

Public Instance methods

Find a scalar value, throwing an exception if not found. This method is used when substituting the %xxx% constructs


    # File template.rb, line 62
62:     def find_scalar(key)
63:       @stack.reverse_each do |level|
64:         if val = level[key]
65:           return val unless val.kind_of? Array
66:         end
67:       end
68:       raise "Template error: can't find variable '#{key}'"
69:     end

Lookup any key in the stack of hashes


    # File template.rb, line 73
73:     def lookup(key)
74:       @stack.reverse_each do |level|
75:         val = level[key]
76:         return val if val
77:       end
78:       nil
79:     end


    # File template.rb, line 55
55:     def pop
56:       @stack.pop
57:     end


    # File template.rb, line 51
51:     def push(hash)
52:       @stack.push(hash)
53:     end