Class: Puppet::Parser::AST::CollExpr

Inherits:
Branch show all
Defined in:
lib/puppet/parser/ast/collexpr.rb

Instance Attribute Summary collapse

Attributes inherited from Branch

#children, #pin

Instance Method Summary collapse

Methods inherited from Branch

#each

Constructor Details

#initialize(hash = {}) ⇒ CollExpr

Returns a new instance of CollExpr.

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
# File 'lib/puppet/parser/ast/collexpr.rb', line 101

def initialize(hash = {})
  super
  if Puppet[:parser] == "future"
    @@compare_operator ||= Puppet::Pops::Evaluator::CompareOperator.new
  end
  raise ArgumentError, "Invalid operator #{@oper}" unless %w{== != and or}.include?(@oper)
end

Instance Attribute Details

#formObject



9
10
11
# File 'lib/puppet/parser/ast/collexpr.rb', line 9

def form
  @form
end

#operObject



9
10
11
# File 'lib/puppet/parser/ast/collexpr.rb', line 9

def oper
  @oper
end

#parensObject



9
10
11
# File 'lib/puppet/parser/ast/collexpr.rb', line 9

def parens
  @parens
end

#test1Object



9
10
11
# File 'lib/puppet/parser/ast/collexpr.rb', line 9

def test1
  @test1
end

#test2Object



9
10
11
# File 'lib/puppet/parser/ast/collexpr.rb', line 9

def test2
  @test2
end

#typeObject



9
10
11
# File 'lib/puppet/parser/ast/collexpr.rb', line 9

def type
  @type
end

Instance Method Details

#evaluate(scope) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/puppet/parser/ast/collexpr.rb', line 11

def evaluate(scope)
  if Puppet[:parser] == 'future'
    evaluate4x(scope)
  else
    evaluate3x(scope)
  end
end

#evaluate3x(scope) ⇒ Object

We return an object that does a late-binding evaluation.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/parser/ast/collexpr.rb', line 20

def evaluate3x(scope)
  # Make sure our contained expressions have all the info they need.
  [@test1, @test2].each do |t|
    if t.is_a?(self.class)
      t.form ||= self.form
      t.type ||= self.type
    end
  end

  # The code is only used for virtual lookups
  match1, code1 = @test1.safeevaluate scope
  match2, code2 = @test2.safeevaluate scope

  # First build up the virtual code.
  # If we're a conjunction operator, then we're calling code.  I did
  # some speed comparisons, and it's at least twice as fast doing these
  # case statements as doing an eval here.
  code = proc do |resource|
    case @oper
    when "and"; code1.call(resource) and code2.call(resource)
    when "or"; code1.call(resource) or code2.call(resource)
    when "=="
      if match1 == "tag"
        resource.tagged?(match2)
      else
        if resource[match1].is_a?(Array)
          resource[match1].include?(match2)
        else
          resource[match1] == match2
        end
      end
    when "!="; resource[match1] != match2
    end
  end

  match = [match1, @oper, match2]
  return match, code
end

#evaluate4x(scope) ⇒ Object

Late binding evaluation of a collect expression (as done in 3x), but with proper Puppet Language semantics for equals and include



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/puppet/parser/ast/collexpr.rb', line 62

def evaluate4x(scope)
  # Make sure our contained expressions have all the info they need.
  [@test1, @test2].each do |t|
    if t.is_a?(self.class)
      t.form ||= self.form
      t.type ||= self.type
    end
  end

  # The code is only used for virtual lookups
  match1, code1 = @test1.safeevaluate scope
  match2, code2 = @test2.safeevaluate scope

  # First build up the virtual code.
  # If we're a conjunction operator, then we're calling code.  I did
  # some speed comparisons, and it's at least twice as fast doing these
  # case statements as doing an eval here.
  code = proc do |resource|
    case @oper
    when "and"; code1.call(resource) and code2.call(resource)
    when "or"; code1.call(resource) or code2.call(resource)
    when "=="
      if match1 == "tag"
        resource.tagged?(match2)
      else
        if resource[match1].is_a?(Array)
          @@compare_operator.include?(resource[match1], match2, scope)
        else
          @@compare_operator.equals(resource[match1], match2)
        end
      end
    when "!="; ! @@compare_operator.equals(resource[match1], match2)
    end
  end

  match = [match1, @oper, match2]
  return match, code
end