Class: Payload::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/payload/factory.rb

Overview

Returned by Container#[] for Container#factory definitions.

Used to add definitions from #new to the Container and then run the factory definition block to obtain an instance.

Instance Method Summary collapse

Constructor Details

#initialize(container, block, decorators) ⇒ Factory

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.

Used internally by Payload::FactoryResolver.



14
15
16
17
18
# File 'lib/payload/factory.rb', line 14

def initialize(container, block, decorators)
  @container = container
  @block = block
  @decorators = decorators
end

Instance Method Details

#apply(*arguments, **keywords) ⇒ PartialInstance

Return a new factory with some of the arguments provided. Remaining arguments can be provided by invoking #new on the returned instance. Chaining #apply is also possible. This can be useful for returning a factory where some of the dependencies are provided by the container, and the remainder are provided at runtime.

Examples:

container = Payload::Container.new.
  factory(:form) { |container, model, attributes|
    Form.new(model, attributes)
  }.
  service(:user_form) { |container| container[:form].apply(User) }
user_form = container[:user_form].new(username: "smith")

Parameters:

  • arguments (Array)

    positional arguments to be passed to the factory

  • keywords (Hash)

    keyword arguments to be passed to the factory

Returns:



46
47
48
# File 'lib/payload/factory.rb', line 46

def apply(*arguments, **keywords)
  PartialInstance.new(self).apply(*arguments, **keywords)
end

#new(*arguments) ⇒ Object

Returns the instance defined by the factory definition block.

Parameters:

  • arguments (Array)

    additional arguments to pass to the factory definition block.

Returns:

  • the instance defined by the factory definition block.

See Also:



24
25
26
27
# File 'lib/payload/factory.rb', line 24

def new(*arguments)
  base = @block.call(@container, *arguments)
  @decorators.decorate(base, @container, *arguments)
end