class Puppet::Pops::Binder::Producers::InstantiatingProducer

@api public

Attributes

init_args[R]

@api public

the_class[R]

@api public

Protected Instance Methods

internal_produce(scope) click to toggle source

Performs initialization the same way as Assisted Inject does (but handle arguments to constructor) @api private

# File lib/puppet/pops/binder/producers.rb, line 234
def internal_produce(scope)
  result = nil
  # A class :inject method wins over an instance :initialize if it is present, unless a more specific
  # constructor exists. (i.e do not pick :inject from superclass if class has a constructor).
  #
  if the_class.respond_to?(:inject)
    inject_method = the_class.method(:inject)
    initialize_method = the_class.instance_method(:initialize)
    if inject_method.owner <= initialize_method.owner
      result = the_class.inject(injector, scope, binding, *init_args)
    end
  end
  if result.nil?
    result = the_class.new(*init_args)
  end
  result
end

Public Class Methods

new(injector, binding, scope, options) click to toggle source

@param injector [Puppet::Pops::Binder::Injector] The injector where the lookup originates @param binding [Puppet::Pops::Binder::Bindings::Binding, nil] The binding using this producer @param scope [Puppet::Parser::Scope] The scope to use for evaluation @option options [Puppet::Pops::Model::LambdaExpression] :transformer (nil) a transformer of produced value @option options [String] :class_name The name of the class to create instance of @option options [Array<Object>] :#init_args ([]) Optional arguments to class constructor @api public

# File lib/puppet/pops/binder/producers.rb, line 215
def initialize(injector, binding, scope, options)
  # Better do this, even if a transformation of a created instance is kind of an odd thing to do, one can imagine
  # sending it to a function for further detailing.
  #
  super
  class_name = options[:class_name]
  raise ArgumentError, "Option 'class_name' must be given for an InstantiatingProducer" unless class_name
  # get class by name
  @the_class = Puppet::Pops::Types::ClassLoader.provide(class_name)
  @init_args = options[:init_args] || []
  raise ArgumentError, "Can not load the class #{class_name} specified in binding named: '#{binding.name}'" unless @the_class
end