Class: Cadenza::VariableNode
- Inherits:
-
Object
- Object
- Cadenza::VariableNode
- Defined in:
- lib/cadenza/nodes/variable_node.rb
Overview
The VariableNode holds a variable name (identifier) which it can render the value of given a Context with the name defined in it’s variable stack.
Instance Attribute Summary collapse
-
#identifier ⇒ String
The name given to this variable.
-
#parameters ⇒ Array
A list of Node objects passed to the #value for use in a functional variable.
Instance Method Summary collapse
-
#==(rhs) ⇒ Boolean
If the given VariableNode is equivalent by value to this node.
-
#eval(context) ⇒ Object
Looks up and returns the value of this variable in the given Context.
-
#implied_globals ⇒ Array
A list of names which are implied to be global variables from this node.
-
#initialize(identifier, parameters = []) ⇒ VariableNode
constructor
creates a new VariableNode with the name given.
Constructor Details
#initialize(identifier, parameters = []) ⇒ VariableNode
creates a new Cadenza::VariableNode with the name given.
15 16 17 18 |
# File 'lib/cadenza/nodes/variable_node.rb', line 15 def initialize(identifier, parameters=[]) @identifier = identifier @parameters = parameters end |
Instance Attribute Details
#identifier ⇒ String
Returns the name given to this variable.
6 7 8 |
# File 'lib/cadenza/nodes/variable_node.rb', line 6 def identifier @identifier end |
#parameters ⇒ Array
Returns a list of Node objects passed to the #value for use in a functional variable. See Context::FunctionalVariables#define_functional_variable.
10 11 12 |
# File 'lib/cadenza/nodes/variable_node.rb', line 10 def parameters @parameters end |
Instance Method Details
#==(rhs) ⇒ Boolean
Returns if the given Cadenza::VariableNode is equivalent by value to this node.
43 44 45 |
# File 'lib/cadenza/nodes/variable_node.rb', line 43 def ==(rhs) self.identifier == rhs.identifier && self.parameters == rhs.parameters end |
#eval(context) ⇒ Object
Returns looks up and returns the value of this variable in the given Context.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cadenza/nodes/variable_node.rb', line 29 def eval(context) value = context.lookup(@identifier) if value.is_a? Proc args = parameters.map {|p| p.eval(context) } value = value.call(context, *args) end value end |
#implied_globals ⇒ Array
Returns a list of names which are implied to be global variables from this node.
22 23 24 |
# File 'lib/cadenza/nodes/variable_node.rb', line 22 def implied_globals ([self.identifier] + @parameters.map(&:implied_globals).flatten).uniq end |