class Puppet::Pops::Binder::Producers::AssistedInjectProducer

This type of producer should only be created by the Injector.

@api private

Public Instance Methods

produce(scope, *args) click to toggle source
# File lib/puppet/pops/binder/producers.rb, line 479
def produce(scope, *args)
  producer(scope, *args) unless @inst
  @inst
end
producer(scope, *args) click to toggle source

@api private

# File lib/puppet/pops/binder/producers.rb, line 485
def producer(scope, *args)
  @inst = nil
  # A class :inject method wins over an instance :initialize if it is present, unless a more specific zero args
  # constructor exists. (i.e do not pick :inject from superclass if class has a zero args constructor).
  #
  if @clazz.respond_to?(:inject)
    inject_method = @clazz.method(:inject)
    initialize_method = @clazz.instance_method(:initialize)
    if inject_method.owner <= initialize_method.owner || initialize_method.arity != 0
      @inst = @clazz.inject(@injector, scope, nil, *args)
    end
  end
  if @inst.nil?
    unless args.empty?
      raise ArgumentError, "Assisted Inject can not pass arguments to no-args constructor when there is no class inject method."
    end
    @inst = @clazz.new()
  end
  self
end

Public Class Methods

new(injector, clazz) click to toggle source

An Assisted Inject Producer is created when a lookup is made of a type that is not bound. It does not support a transformer lambda. @note This initializer has a different signature than all others. Do not use in regular logic. @api private

# File lib/puppet/pops/binder/producers.rb, line 471
def initialize(injector, clazz)
  raise ArgumentError, "class must be given" unless clazz.is_a?(Class)

  @injector = injector
  @clazz = clazz
  @inst = nil
end