Class: Cadenza::ForNode
- Inherits:
-
Object
- Object
- Cadenza::ForNode
- Defined in:
- lib/cadenza/nodes/for_node.rb
Overview
The ForNode describe a loop in the template code. The loop should iterate over all the elements of the iterable and render its children each time.
Constant Summary collapse
- MAGIC_LOCALS =
%w(forloop.counter forloop.counter0 forloop.first forloop.last)
Instance Attribute Summary collapse
-
#children ⇒ Array
The list of children associated with this loop.
-
#iterable ⇒ VariableNode
The iterable object for the loop.
-
#iterator ⇒ VariableNode
The iterator object for the loop.
Instance Method Summary collapse
-
#==(rhs) ⇒ Boolean
True if the given ForNode is equivalent by value to this node.
-
#implied_globals ⇒ Array
Of all global variable names defined by this node and it’s child nodes.
-
#initialize(iterator, iterable, children) ⇒ ForNode
constructor
constructs a new ForNode with the given iterator, iterable and child nodes.
Constructor Details
#initialize(iterator, iterable, children) ⇒ ForNode
constructs a new Cadenza::ForNode with the given iterator, iterable and child nodes.
18 19 20 21 22 |
# File 'lib/cadenza/nodes/for_node.rb', line 18 def initialize(iterator, iterable, children) @iterator = iterator @iterable = iterable @children = children end |
Instance Attribute Details
#children ⇒ Array
Returns the list of children associated with this loop.
12 13 14 |
# File 'lib/cadenza/nodes/for_node.rb', line 12 def children @children end |
#iterable ⇒ VariableNode
Returns the iterable object for the loop.
9 10 11 |
# File 'lib/cadenza/nodes/for_node.rb', line 9 def iterable @iterable end |
#iterator ⇒ VariableNode
Returns the iterator object for the loop.
6 7 8 |
# File 'lib/cadenza/nodes/for_node.rb', line 6 def iterator @iterator end |
Instance Method Details
#==(rhs) ⇒ Boolean
Returns true if the given Cadenza::ForNode is equivalent by value to this node.
46 47 48 49 50 |
# File 'lib/cadenza/nodes/for_node.rb', line 46 def ==(rhs) @iterator == rhs.iterator and @iterable == rhs.iterable and @children == rhs.children end |
#implied_globals ⇒ Array
Returns of all global variable names defined by this node and it’s child nodes.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cadenza/nodes/for_node.rb', line 26 def implied_globals iterable_globals = @iterable.implied_globals iterator_globals = @iterator.implied_globals iterator_regex = Regexp.new("^#{@iterator.identifier}[\.](.+)$") all_children_globals = @children.map(&:implied_globals).flatten children_globals = all_children_globals.reject {|x| x =~ iterator_regex } iterator_children_globals = all_children_globals.select {|x| x =~ iterator_regex }.map do |identifier| "#{iterable.identifier}.#{iterator_regex.match(identifier)[1]}" end (iterable_globals | children_globals | iterator_children_globals) - MAGIC_LOCALS - iterator_globals end |