Class: Cadenza::ForNode

Inherits:
Object
  • Object
show all
Includes:
TreeNode
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

Instance Method Summary collapse

Methods included from TreeNode

#to_tree

Constructor Details

#initialize(iterator, iterable, children) ⇒ ForNode

constructs a new Cadenza::ForNode with the given iterator, iterable and child nodes.


20
21
22
23
24
# File 'lib/cadenza/nodes/for_node.rb', line 20

def initialize(iterator, iterable, children)
   @iterator = iterator
   @iterable = iterable
   @children = children
end

Instance Attribute Details

#childrenArray

Returns the list of children associated with this loop.

Returns:

  • (Array)

    the list of children associated with this loop


14
15
16
# File 'lib/cadenza/nodes/for_node.rb', line 14

def children
  @children
end

#iterableVariableNode

Returns the iterable object for the loop.

Returns:


11
12
13
# File 'lib/cadenza/nodes/for_node.rb', line 11

def iterable
  @iterable
end

#iteratorVariableNode

Returns the iterator object for the loop.

Returns:


8
9
10
# File 'lib/cadenza/nodes/for_node.rb', line 8

def iterator
  @iterator
end

Instance Method Details

#==(rhs) ⇒ Boolean

Returns true if the given Cadenza::ForNode is equivalent by value to this node.

Parameters:

Returns:

  • (Boolean)

    true if the given Cadenza::ForNode is equivalent by value to this node.


48
49
50
51
52
# File 'lib/cadenza/nodes/for_node.rb', line 48

def ==(rhs)
   @iterator == rhs.iterator &&
   @iterable == rhs.iterable &&
   @children == rhs.children
end

#implied_globalsArray

Returns of all global variable names defined by this node and it's child nodes.

Returns:

  • (Array)

    of all global variable names defined by this node and it's child nodes.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cadenza/nodes/for_node.rb', line 28

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