A class for objects which contain the execution space of all the code in a single rendering of a template.
Create a new RenderingScope object with the specified definitions defs. Each key => value pair in defs will become singleton accessors on the resulting object.
# File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 93
93: def initialize( defs={} )
94: @definitions = []
95: self.add_definition_set( defs )
96: end
Add the specified definitions defs to the object.
# File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 112
112: def add_definition_set( defs )
113: # self.log.debug "adding definition set: %p" % [ defs ]
114: @definitions.push( defs )
115:
116: defs.each do |name,val|
117: raise Arrow::ScopeError, "Cannot add a definition with a blank key" if
118: name.to_s.empty?
119: raise Arrow::ScopeError, "Cannot override @definitions" if
120: name == 'definitions'
121: @definitions.last[ name ] = val
122:
123: # Add accessor and ivar for the definition if it doesn't
124: # already have one.
125: unless self.respond_to?( name.to_s.intern )
126: #self.log.debug "Adding accessor for %s" % name
127: (class << self; self; end).instance_eval {
128: attr_accessor name.to_s.intern
129: }
130: else
131: #self.log.debug "Already have an accessor for '#{name}'"
132: end
133:
134: self.instance_variable_set( "@#{name}", defs[name] )
135: end
136: end
Fetch the Binding object for the RenderingScope.
# File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 108
108: def get_binding; binding; end
Override the given definitions defs for the duration of the given block. After the block exits, the original definitions will be restored.
# File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 179
179: def override( defs ) # :yields: receiver
180: begin
181: #self.log.debug "Before adding definitions: %d scope frame/s. Last: %p" %
182: # [ @definitions.nitems, @definitions.last.keys ]
183: self.add_definition_set( defs )
184: #self.log.debug "After adding definitions: %d scope frame/s. Last: %p" %
185: # [ @definitions.nitems, @definitions.last.keys ]
186: yield( self )
187: ensure
188: self.remove_definition_set
189: end
190: end
Remove the specified definitions defs from the object. Using a definition so removed after this point will raise an error.
# File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 141
141: def remove_definition_set
142: #self.log.debug "Removing definition set from stack of %d frames" %
143: # @definitions.nitems
144: defs = @definitions.pop
145: #self.log.debug "Removing defs: %p, %d frames left" %
146: # [ defs, @definitions.nitems ]
147:
148: defs.keys.each do |name|
149: next if name == 'definitions'
150:
151: # If there was already a definition in effect with the same
152: # name in a previous scope, fetch it so we can play with it.
153: previousSet = @definitions.reverse.find {|set| set.key?(name)}
154: #self.log.debug "Found previousSet %p for %s in scope stack of %d frames" %
155: # [ previousSet, name, @definitions.nitems ]
156:
157: # If none of the previous definition sets had a definition
158: # with the same name, remove the accessor and the ivar
159: unless previousSet
160: #self.log.debug "Removing definition '%s' entirely" % name
161: (class << self; self; end).module_eval {
162: remove_method name.to_s.intern
163: }
164: remove_instance_variable( "@#{name}" )
165:
166: # Otherwise just reset the ivar to the previous value
167: else
168: #self.log.debug "Restoring previous def for '%s'" % name
169: self.instance_variable_set( "@#{name}", previousSet[name] )
170: end
171: end
172:
173: end
--- SEC00178
--- ""
---
- methods:
- visibility: public
aref: M000498
name: new
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 93</span>\n\
93: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">initialize</span>( <span class=\"ruby-identifier\">defs</span>={} )\n\
94: <span class=\"ruby-ivar\">@definitions</span> = []\n\
95: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">add_definition_set</span>( <span class=\"ruby-identifier\">defs</span> )\n\
96: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Create a <a href="RenderingScope.html#M000498">new</a> <a
href="RenderingScope.html">RenderingScope</a> object with the specified
definitions <tt>defs</tt>. Each key => value pair in <tt>defs</tt> will
become singleton accessors on the resulting object.
</p>
params: ( defs={} )
category: Class
type: Public
- methods:
- visibility: public
aref: M000500
name: add_definition_set
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 112</span>\n\
112: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">add_definition_set</span>( <span class=\"ruby-identifier\">defs</span> )\n\
113: <span class=\"ruby-comment cmt\"># self.log.debug "adding definition set: %p" % [ defs ]</span>\n\
114: <span class=\"ruby-ivar\">@definitions</span>.<span class=\"ruby-identifier\">push</span>( <span class=\"ruby-identifier\">defs</span> )\n\
115: \n\
116: <span class=\"ruby-identifier\">defs</span>.<span class=\"ruby-identifier\">each</span> <span class=\"ruby-keyword kw\">do</span> <span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">name</span>,<span class=\"ruby-identifier\">val</span><span class=\"ruby-operator\">|</span>\n\
117: <span class=\"ruby-identifier\">raise</span> <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">ScopeError</span>, <span class=\"ruby-value str\">"Cannot add a definition with a blank key"</span> <span class=\"ruby-keyword kw\">if</span>\n\
118: <span class=\"ruby-identifier\">name</span>.<span class=\"ruby-identifier\">to_s</span>.<span class=\"ruby-identifier\">empty?</span>\n\
119: <span class=\"ruby-identifier\">raise</span> <span class=\"ruby-constant\">Arrow</span><span class=\"ruby-operator\">::</span><span class=\"ruby-constant\">ScopeError</span>, <span class=\"ruby-value str\">"Cannot override @definitions"</span> <span class=\"ruby-keyword kw\">if</span>\n\
120: <span class=\"ruby-identifier\">name</span> <span class=\"ruby-operator\">==</span> <span class=\"ruby-value str\">'definitions'</span>\n\
121: <span class=\"ruby-ivar\">@definitions</span>.<span class=\"ruby-identifier\">last</span>[ <span class=\"ruby-identifier\">name</span> ] = <span class=\"ruby-identifier\">val</span>\n\
122: \n\
123: <span class=\"ruby-comment cmt\"># Add accessor and ivar for the definition if it doesn't</span>\n\
124: <span class=\"ruby-comment cmt\"># already have one.</span>\n\
125: <span class=\"ruby-keyword kw\">unless</span> <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">respond_to?</span>( <span class=\"ruby-identifier\">name</span>.<span class=\"ruby-identifier\">to_s</span>.<span class=\"ruby-identifier\">intern</span> )\n\
126: <span class=\"ruby-comment cmt\">#self.log.debug "Adding accessor for %s" % name</span>\n\
127: (<span class=\"ruby-keyword kw\">class</span> <span class=\"ruby-operator\"><<</span> <span class=\"ruby-keyword kw\">self</span>; <span class=\"ruby-keyword kw\">self</span>; <span class=\"ruby-keyword kw\">end</span>).<span class=\"ruby-identifier\">instance_eval</span> {\n\
128: <span class=\"ruby-identifier\">attr_accessor</span> <span class=\"ruby-identifier\">name</span>.<span class=\"ruby-identifier\">to_s</span>.<span class=\"ruby-identifier\">intern</span>\n\
129: }\n\
130: <span class=\"ruby-keyword kw\">else</span>\n\
131: <span class=\"ruby-comment cmt\">#self.log.debug "Already have an accessor for '#{name}'"</span>\n\
132: <span class=\"ruby-keyword kw\">end</span>\n\
133: \n\
134: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">instance_variable_set</span>( <span class=\"ruby-node\">"@#{name}"</span>, <span class=\"ruby-identifier\">defs</span>[<span class=\"ruby-identifier\">name</span>] )\n\
135: <span class=\"ruby-keyword kw\">end</span>\n\
136: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Add the specified definitions <tt>defs</tt> to the object.
</p>
params: ( defs )
- visibility: public
aref: M000499
name: get_binding
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 108</span>\n\
108: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">get_binding</span>; <span class=\"ruby-identifier\">binding</span>; <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Fetch the Binding object for the <a
href="RenderingScope.html">RenderingScope</a>.
</p>
params: ()
- visibility: public
aref: M000502
name: override
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 179</span>\n\
179: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">override</span>( <span class=\"ruby-identifier\">defs</span> ) <span class=\"ruby-comment cmt\"># :yields: receiver</span>\n\
180: <span class=\"ruby-keyword kw\">begin</span>\n\
181: <span class=\"ruby-comment cmt\">#self.log.debug "Before adding definitions: %d scope frame/s. Last: %p" %</span>\n\
182: <span class=\"ruby-comment cmt\"># [ @definitions.nitems, @definitions.last.keys ]</span>\n\
183: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">add_definition_set</span>( <span class=\"ruby-identifier\">defs</span> )\n\
184: <span class=\"ruby-comment cmt\">#self.log.debug "After adding definitions: %d scope frame/s. Last: %p" % </span>\n\
185: <span class=\"ruby-comment cmt\"># [ @definitions.nitems, @definitions.last.keys ]</span>\n\
186: <span class=\"ruby-keyword kw\">yield</span>( <span class=\"ruby-keyword kw\">self</span> )\n\
187: <span class=\"ruby-keyword kw\">ensure</span>\n\
188: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">remove_definition_set</span>\n\
189: <span class=\"ruby-keyword kw\">end</span>\n\
190: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Override the given definitions <tt>defs</tt> for the duration of the given
block. After the block exits, the original definitions will be restored.
</p>
params: ( defs ) {|receiver| ...}
- visibility: public
aref: M000501
name: remove_definition_set
sourcecode: " <span class=\"ruby-comment cmt\"># File /Users/ged/source/ruby/Arrow/lib/arrow/template.rb, line 141</span>\n\
141: <span class=\"ruby-keyword kw\">def</span> <span class=\"ruby-identifier\">remove_definition_set</span>\n\
142: <span class=\"ruby-comment cmt\">#self.log.debug "Removing definition set from stack of %d frames" %</span>\n\
143: <span class=\"ruby-comment cmt\"># @definitions.nitems</span>\n\
144: <span class=\"ruby-identifier\">defs</span> = <span class=\"ruby-ivar\">@definitions</span>.<span class=\"ruby-identifier\">pop</span>\n\
145: <span class=\"ruby-comment cmt\">#self.log.debug "Removing defs: %p, %d frames left" % </span>\n\
146: <span class=\"ruby-comment cmt\"># [ defs, @definitions.nitems ]</span>\n\
147: \n\
148: <span class=\"ruby-identifier\">defs</span>.<span class=\"ruby-identifier\">keys</span>.<span class=\"ruby-identifier\">each</span> <span class=\"ruby-keyword kw\">do</span> <span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">name</span><span class=\"ruby-operator\">|</span>\n\
149: <span class=\"ruby-keyword kw\">next</span> <span class=\"ruby-keyword kw\">if</span> <span class=\"ruby-identifier\">name</span> <span class=\"ruby-operator\">==</span> <span class=\"ruby-value str\">'definitions'</span>\n\
150: \n\
151: <span class=\"ruby-comment cmt\"># If there was already a definition in effect with the same</span>\n\
152: <span class=\"ruby-comment cmt\"># name in a previous scope, fetch it so we can play with it.</span>\n\
153: <span class=\"ruby-identifier\">previousSet</span> = <span class=\"ruby-ivar\">@definitions</span>.<span class=\"ruby-identifier\">reverse</span>.<span class=\"ruby-identifier\">find</span> {<span class=\"ruby-operator\">|</span><span class=\"ruby-identifier\">set</span><span class=\"ruby-operator\">|</span> <span class=\"ruby-identifier\">set</span>.<span class=\"ruby-identifier\">key?</span>(<span class=\"ruby-identifier\">name</span>)}\n\
154: <span class=\"ruby-comment cmt\">#self.log.debug "Found previousSet %p for %s in scope stack of %d frames" %</span>\n\
155: <span class=\"ruby-comment cmt\"># [ previousSet, name, @definitions.nitems ]</span>\n\
156: \n\
157: <span class=\"ruby-comment cmt\"># If none of the previous definition sets had a definition</span>\n\
158: <span class=\"ruby-comment cmt\"># with the same name, remove the accessor and the ivar</span>\n\
159: <span class=\"ruby-keyword kw\">unless</span> <span class=\"ruby-identifier\">previousSet</span>\n\
160: <span class=\"ruby-comment cmt\">#self.log.debug "Removing definition '%s' entirely" % name</span>\n\
161: (<span class=\"ruby-keyword kw\">class</span> <span class=\"ruby-operator\"><<</span> <span class=\"ruby-keyword kw\">self</span>; <span class=\"ruby-keyword kw\">self</span>; <span class=\"ruby-keyword kw\">end</span>).<span class=\"ruby-identifier\">module_eval</span> {\n\
162: <span class=\"ruby-identifier\">remove_method</span> <span class=\"ruby-identifier\">name</span>.<span class=\"ruby-identifier\">to_s</span>.<span class=\"ruby-identifier\">intern</span>\n\
163: }\n\
164: <span class=\"ruby-identifier\">remove_instance_variable</span>( <span class=\"ruby-node\">"@#{name}"</span> )\n\
165: \n\
166: <span class=\"ruby-comment cmt\"># Otherwise just reset the ivar to the previous value</span>\n\
167: <span class=\"ruby-keyword kw\">else</span>\n\
168: <span class=\"ruby-comment cmt\">#self.log.debug "Restoring previous def for '%s'" % name</span>\n\
169: <span class=\"ruby-keyword kw\">self</span>.<span class=\"ruby-identifier\">instance_variable_set</span>( <span class=\"ruby-node\">"@#{name}"</span>, <span class=\"ruby-identifier\">previousSet</span>[<span class=\"ruby-identifier\">name</span>] )\n\
170: <span class=\"ruby-keyword kw\">end</span>\n\
171: <span class=\"ruby-keyword kw\">end</span>\n\
172: \n\
173: <span class=\"ruby-keyword kw\">end</span>"
m_desc: |-
<p>
Remove the specified definitions <tt>defs</tt> from the object. Using a
definition so removed after this point will raise an error.
</p>
params: ()
category: Instance
type: Public
---
Generated with the Darkfish Rdoc Generator.