Sha256: ba97b10117d14831f8745194bdc00657ec1638ad7867295b158e711a29b01897

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

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(scope)
            retvalue = nil
            found = nil

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

            default = nil

            # Then look for a match in the options.
            @values.each { |obj|
                param = obj.param.safeevaluate(scope)
                if param == paramvalue
                    # we found a matching option
                    retvalue = obj.value.safeevaluate(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)
                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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
puppet-0.13.2 lib/puppet/parser/ast/selector.rb
puppet-0.13.1 lib/puppet/parser/ast/selector.rb
puppet-0.13.0 lib/puppet/parser/ast/selector.rb