Class: Cadenza::InjectNode

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

Overview

The InjectNode is intended to write the given variable into the rendered output by evaluating it in the given Context and passing it through an optional series of FilterNodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, filters = [], parameters = []) ⇒ InjectNode

creates a new Cadenza::InjectNode with the given value, filters and parameters

Parameters:



21
22
23
24
25
# File 'lib/cadenza/nodes/inject_node.rb', line 21

def initialize(value, filters=[], parameters=[])
  @value = value
  @filters = filters
  @parameters = parameters
end

Instance Attribute Details

#filtersArray

Returns a list of FilterNode to evaluate the value with, once the value has itself been evaluated.

Returns:

  • (Array)

    a list of FilterNode to evaluate the value with, once the value has itself been evaluated.



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

def filters
  @filters
end

#parametersArray

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

Returns:



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

def parameters
  @parameters
end

#valueVariableNode|OperationNode|BooleanInverseNode|ConstantNode

Returns the value being evaluated.

Returns:



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

def value
  @value
end

Instance Method Details

#==(rhs) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)

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



29
30
31
32
33
# File 'lib/cadenza/nodes/inject_node.rb', line 29

def ==(rhs)
  self.value == rhs.value and
  self.filters == rhs.filters and
  self.parameters == rhs.parameters
end

#evaluate(context) ⇒ String

Returns the evaluated #value of this node in the given Context with any applicable #parameters after passed through the given #filters.

Parameters:

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cadenza/nodes/inject_node.rb', line 44

def evaluate(context)
  value = @value.eval(context)

  if value.is_a? Proc
    args = parameters.map {|p| p.eval(context) }
    value = value.call(context, *args)
  end

  @filters.each {|filter| value = filter.evaluate(context, value) }

  value
end

#implied_globalsArray

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

Returns:

  • (Array)

    a list of variable names implied to be global by this node



36
37
38
# File 'lib/cadenza/nodes/inject_node.rb', line 36

def implied_globals
  (@value.implied_globals + @filters.map(&:implied_globals).flatten).uniq
end