Class: Puppet::Pops::Binder::Producers::AssistedInjectProducer Private

Inherits:
Producer
  • Object
show all
Defined in:
lib/puppet/pops/binder/producers.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Attribute Summary

Attributes inherited from Producer

#transformer

Instance Method Summary collapse

Constructor Details

#initialize(injector, clazz) ⇒ AssistedInjectProducer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

This initializer has a different signature than all others. Do not use in regular logic.

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.

Raises:

  • (ArgumentError)


474
475
476
477
478
479
480
# File 'lib/puppet/pops/binder/producers.rb', line 474

def initialize(injector, clazz)
  raise ArgumentError, "class must be given" unless clazz.is_a?(Class)

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

Instance Method Details

#produce(scope, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



482
483
484
485
# File 'lib/puppet/pops/binder/producers.rb', line 482

def produce(scope, *args)
  producer(scope, *args) unless @inst
  @inst
end

#producer(scope, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/puppet/pops/binder/producers.rb', line 488

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