Sha256: d1bd41aa94fed3b363b9ef73ac16f0ba5e50668e1f9e25f6f70b3a80a2a056ed

Contents?: true

Size: 1.95 KB

Versions: 8

Compression:

Stored size: 1.95 KB

Contents

require 'puppet/parser/ast/branch'

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)
            
            sensitive = Puppet[:casesensitive]
            
            if ! sensitive and paramvalue.respond_to?(:downcase)
                paramvalue = paramvalue.downcase
            end

            default = nil

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

            # Then look for a match in the options.
            @values.each { |obj|
                param = obj.param.safeevaluate(scope)
                if ! sensitive && param.respond_to?(:downcase)
                    param = param.downcase
                end
                if param == paramvalue
                    # we found a matching option
                    retvalue = obj.value.safeevaluate(scope)
                    found = true
                    break
                elsif obj.param.is_a?(Default)
                    # Store the default, in case it's necessary.
                    default = obj
                end
            }

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

            return retvalue
        end
    end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
puppet-0.24.9 lib/puppet/parser/ast/selector.rb
puppet-0.24.4 lib/puppet/parser/ast/selector.rb
puppet-0.24.2 lib/puppet/parser/ast/selector.rb
puppet-0.24.3 lib/puppet/parser/ast/selector.rb
puppet-0.24.5 lib/puppet/parser/ast/selector.rb
puppet-0.24.6 lib/puppet/parser/ast/selector.rb
puppet-0.24.7 lib/puppet/parser/ast/selector.rb
puppet-0.24.8 lib/puppet/parser/ast/selector.rb