# class Puppet::DSL::ResourceAPI

### Constants

FUNCTION_MAP

### Attributes

block[R]
resource[R]
scope[R]

### Public Class Methods

new(resource, scope, block) click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 22
    def initialize(resource, scope, block)
      @scope = scope
      @resource = resource
      @block = block
    end

### Public Instance Methods

call_function(name, args) click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 70
    def call_function(name, args)
      return false unless method = Puppet::Parser::Functions.function(name)
      scope.send(method, *args)
    end

create_resource(type, names, arguments = nil) click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 51
    def create_resource(type, names, arguments = nil)
      names = [names] unless names.is_a?(Array)

      arguments ||= {}
      raise ArgumentError, "Resource arguments must be provided as a hash" unless arguments.is_a?(Hash)

      names.collect do |name|
        resource = Puppet::Parser::Resource.new(type, name, :scope => scope)
        arguments.each do |param, value|
          resource[param] = value
        end

        resource.exported = true if exporting?
        resource.virtual = true if virtualizing?
        scope.compiler.add_resource(scope, resource)
        resource
      end
    end

environment() click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 13
    def environment
      scope.environment
    end

evaluate() click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 17
    def evaluate
      set_instance_variables
      instance_eval(&block)
    end

export(resources = nil, &block) click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 75
    def export(resources = nil, &block)
      if resources
        resources.each { |resource| resource.exported = true }
        return resources
      end
      @exporting = true
      instance_eval(&block)
    ensure
      @exporting = false
    end

method_missing(name, *args) click to toggle source

Try to convert a missing method into a resource type or a function.

Calls superclass method

    # File lib/puppet/dsl/resource_api.rb, line 29
    def method_missing(name, *args)
      raise "MethodMissing loop when searching for #{name} with #{args.inspect}" if searching_for_method?
      @searching_for_method = true
      return create_resource(name, args[0], args[1]) if valid_type?(name)

      name = map_function(name)

      return call_function(name, args) if Puppet::Parser::Functions.function(name)

      super
    ensure
      @searching_for_method = false
    end

set_instance_variables() click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 43
    def set_instance_variables
      resource.eachparam do |param|
        instance_variable_set("@#{param.name}", param.value)
      end
      @title = resource.title
      @name ||= resource.title
    end

valid_type?(name) click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 97
    def valid_type?(name)
      return true if [:class, :node].include?(name)
      return true if Puppet::Type.type(name)
      return(known_resource_types.definition(name) ? true : false)
    end

virtual(resources = nil, &block) click to toggle source

    # File lib/puppet/dsl/resource_api.rb, line 86
    def virtual(resources = nil, &block)
      if resources
        resources.each { |resource| resource.virtual = true }
        return resources
      end
      @virtualizing = true
      instance_eval(&block)
    ensure
      @virtualizing = false
    end