summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast/selector.rb
blob: 3adf0261c334efce60c61500474198614d5fbd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
class Puppet::Parser::AST
    # The inline conditional operator.  Unlike CaseStatement, which executes
    # code, we just return a value.
    class Selector < AST::Branch
        attr_accessor :param, :values

        def each
            [@param,@values].each { |child| yield child }
        end

        # Find the value that corresponds with the test.
        def evaluate(hash)
            scope = hash[:scope]
            retvalue = nil
            found = nil

            # Get our parameter.
            paramvalue = @param.safeevaluate(:scope => scope)

            default = nil

            @values = [@values] unless @values.instance_of? AST::ASTArray

            # Then look for a match in the options.
            @values.each { |obj|
                param = obj.param.safeevaluate(:scope => scope)
                if param == paramvalue
                    # we found a matching option
                    retvalue = obj.value.safeevaluate(:scope => scope)
                    found = true
                    break
                elsif obj.param.is_a?(Default)
                    default = obj
                end
            }

            # Unless we found something, look for the default.
            unless found
                if default
                    retvalue = default.value.safeevaluate(:scope => scope)
                else
                    error = Puppet::ParseError.new(
                        "No value for selector param '%s'" % paramvalue
                    )
                    error.line = self.line
                    error.file = self.file
                    raise error
                end
            end

            return retvalue
        end

        def tree(indent = 0)
            return [
                @param.tree(indent + 1),
                ((@@indline * indent) + self.typewrap(self.pin)),
                @values.tree(indent + 1)
            ].join("\n")
        end
    end

end