Class: Cadenza::IfNode
- Inherits:
-
Object
- Object
- Cadenza::IfNode
- Defined in:
- lib/cadenza/nodes/if_node.rb
Overview
Instance Attribute Summary collapse
-
#expression ⇒ OperationNode|BooleanInverseNode
The evaluatable expression used to determine which set of nodes to render.
-
#false_children ⇒ Array
A list of nodes which will be rendered if the #expression evaluates to false in the given Context.
-
#true_children ⇒ Array
A list of nodes which will be rendered if the #expression evaluates to true in the given Context.
Instance Method Summary collapse
-
#==(rhs) ⇒ Boolean
True if the given node is equivalent by value to this node.
-
#evaluate_expression_for_children(context) ⇒ 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.
-
#implied_globals ⇒ Array
A list of variable names implied to be global for this node.
-
#initialize(expression, true_children = [], false_children = []) ⇒ IfNode
constructor
creates a new IfNode with the given evaluatable expression and pair of blocks.
Constructor Details
#initialize(expression, true_children = [], false_children = []) ⇒ IfNode
creates a new Cadenza::IfNode with the given evaluatable expression and pair of blocks.
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
#expression ⇒ OperationNode|BooleanInverseNode
Returns the evaluatable expression used to determine which set of nodes to render.
7 8 9 |
# File 'lib/cadenza/nodes/if_node.rb', line 7 def expression @expression end |
#false_children ⇒ Array
Returns 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_children ⇒ Array
Returns 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.
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.
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_globals ⇒ Array
Returns 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 |