Class: Cadenza::VariableNode

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

Instance Method Summary collapse

Constructor Details

#initialize(identifier, parameters = []) ⇒ VariableNode

creates a new Cadenza::VariableNode with the name given.

Parameters:



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

#identifierString

Returns the name given to this variable.

Returns:

  • (String)

    the name given to this variable



6
7
8
# File 'lib/cadenza/nodes/variable_node.rb', line 6

def identifier
  @identifier
end

#parametersArray

Returns a list of Node objects passed to the #value for use in a functional variable. See Context::FunctionalVariables#define_functional_variable.

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:

  • (Object)

    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_globalsArray

Returns a list of names which are implied to be global variables from this node.

Returns:

  • (Array)

    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