Class: Cadenza::ForNode

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

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

#childrenArray

Returns the list of children associated with this loop.

Returns:

  • (Array)

    the list of children associated with this loop



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

def children
  @children
end

#iterableVariableNode

Returns the iterable object for the loop.

Returns:



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

def iterable
  @iterable
end

#iteratorVariableNode

Returns the iterator object for the loop.

Returns:



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.

Parameters:

Returns:

  • (Boolean)

    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_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.



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