Class: Cadenza::IfNode

Inherits:
Object
  • Object
show all
Defined in:
lib/cadenza/nodes/if_node.rb

Overview

The IfNode is a structure for rendering one of it’s two given blocks based on the evaluation of an expression in the current Context.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression, true_children = [], false_children = []) ⇒ IfNode

creates a new Cadenza::IfNode with the given evaluatable expression and pair of blocks.

Parameters:

  • expression (OperationNode|BooleanInverseNode)

    the expression to evaluate

  • true_children (Array) (defaults to: [])

    a list of Node objects which will be rendered if the #expression evaluates to true in the given Context.

  • false_children (Array) (defaults to: [])

    a list of Node objects which will be rendered if the #expression evaluates to false in the given Context.



24
25
26
27
28
# File 'lib/cadenza/nodes/if_node.rb', line 24

def initialize(expression, true_children=[], false_children=[])
   @expression = expression
   @true_children = true_children
   @false_children = false_children
end

Instance Attribute Details

#expressionOperationNode|BooleanInverseNode

Returns the evaluatable expression used to determine which set of nodes to render.

Returns:



7
8
9
# File 'lib/cadenza/nodes/if_node.rb', line 7

def expression
  @expression
end

#false_childrenArray

Returns A list of nodes which will be rendered if the #expression evaluates to false in the given Context.

Returns:

  • (Array)

    A list of nodes which will be rendered if the #expression evaluates to false in the given Context.



15
16
17
# File 'lib/cadenza/nodes/if_node.rb', line 15

def false_children
  @false_children
end

#true_childrenArray

Returns A list of nodes which will be rendered if the #expression evaluates to true in the given Context.

Returns:

  • (Array)

    A list of nodes which will be rendered if the #expression evaluates to true in the given Context.



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

def true_children
  @true_children
end

Instance Method Details

#==(rhs) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)

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



62
63
64
65
66
# File 'lib/cadenza/nodes/if_node.rb', line 62

def ==(rhs)
   @expression == rhs.expression and
   @true_children == rhs.true_children and
   @false_children == rhs.false_children
end

#evaluate_expression_for_children(context) ⇒ Array

Returns evalutes the expression in the given context and returns a list of nodes which should be rendered based on the result of that evaluation.

Parameters:

Returns:

  • (Array)

    evalutes the expression in the given context and returns a list of nodes which should be rendered based on the result of that evaluation.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cadenza/nodes/if_node.rb', line 39

def evaluate_expression_for_children(context)
   value = @expression.eval(context)

   if value == true
      return @true_children

   elsif value == false
      return @false_children

   elsif value.is_a?(String)
      return value.length == 0 || value =~ /\s+/ ? @false_children : @true_children

   elsif value.is_a?(Float) or value.is_a?(Fixnum)
      return value == 0 ? @false_children : @true_children

   else
      return !!value ? @true_children : @false_children
      
   end
end

#implied_globalsArray

Returns a list of variable names implied to be global for this node.

Returns:

  • (Array)

    a list of variable names implied to be global for this node.



31
32
33
# File 'lib/cadenza/nodes/if_node.rb', line 31

def implied_globals
   (@expression.implied_globals + true_children.map(&:implied_globals).flatten + false_children.map(&:implied_globals).flatten).uniq
end