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

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

Constant Summary

Constants inherited from Puppet::Parser::AST

AST

Instance Attribute Summary collapse

Attributes inherited from Branch

#children, #pin

Attributes inherited from Puppet::Parser::AST

#parent, #scope

Attributes included from Util::Docs

#doc, #nodoc

Attributes included from FileCollection::Lookup

#file_index, #line

Instance Method Summary collapse

Methods inherited from Branch

#each

Methods inherited from Puppet::Parser::AST

associates_doc, #evaluate_match, #inspect, #parsefail, #parsewrap, #safeevaluate, settor?, #use_docs

Methods included from Util::Docs

#desc, #dochook, #doctable, #nodoc?, #pad, scrub

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, #exceptwrap, #fail

Methods included from FileCollection::Lookup

#file, #file=, #file_collection

Constructor Details

#initialize(hash = {}) ⇒ CollExpr

Returns a new instance of CollExpr.

Raises:

  • (ArgumentError)


80
81
82
83
84
# File 'lib/puppet/parser/ast/collexpr.rb', line 80

def initialize(hash = {})
  super

  raise ArgumentError, "Invalid operator #{@oper}" unless %w{== != and or}.include?(@oper)
end

Instance Attribute Details

#formObject

Returns the value of attribute form.



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

def form
  @form
end

#operObject

Returns the value of attribute oper.



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

def oper
  @oper
end

#parensObject

Returns the value of attribute parens.



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

def parens
  @parens
end

#test1Object

Returns the value of attribute test1.



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

def test1
  @test1
end

#test2Object

Returns the value of attribute test2.



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

def test2
  @test2
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#evaluate(scope) ⇒ Object

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



12
13
14
15
16
17
18
19
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet/parser/ast/collexpr.rb', line 12

def evaluate(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
  str1, code1 = @test1.safeevaluate scope
  str2, 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 str1 == "tag"
        resource.tagged?(str2)
      else
        if resource[str1].is_a?(Array)
          resource[str1].include?(str2)
        else
          resource[str1] == str2
        end
      end
    when "!="; resource[str1] != str2
    end
  end

  # Now build up the rails conditions code
  if self.parens and self.form == :exported
    Puppet.warning "Parentheses are ignored in Rails searches"
  end

  case @oper
  when "and", "or"
    if form == :exported
      raise Puppet::ParseError, "Puppet does not currently support collecting exported resources with more than one condition"
    end
    oper = @oper.upcase
  when "=="; oper = "="
  else
    oper = @oper
  end

  if oper == "=" or oper == "!="
    # Add the rails association info where necessary
    case str1
    when "title"
      str = "title #{oper} '#{str2}'"
    when "tag"
      str = "puppet_tags.name #{oper} '#{str2}'"
    else
      str = "param_values.value #{oper} '#{str2}' and param_names.name = '#{str1}'"
    end
  else
    str = "(#{str1}) #{oper} (#{str2})"
  end

  return str, code
end