Module: Dry

Defined in:
lib/dry/auto_inject.rb,
lib/dry/auto_inject/builder.rb,
lib/dry/auto_inject/version.rb,
lib/dry/auto_inject/injector.rb,
lib/dry/auto_inject/strategies.rb,
lib/dry/auto_inject/dependency_map.rb,
lib/dry/auto_inject/strategies/args.rb,
lib/dry/auto_inject/strategies/hash.rb,
lib/dry/auto_inject/method_parameters.rb,
lib/dry/auto_inject/strategies/kwargs.rb,
lib/dry/auto_inject/strategies/constructor.rb

Defined Under Namespace

Modules: AutoInject

Class Method Summary collapse

Class Method Details

.AutoInject(container, options = {}) ⇒ Proc

Configure an auto-injection module

Examples:

module MyApp
  # set up your container
  container = Dry::Core::Container.new

  container.register(:data_store, -> { DataStore.new })
  container.register(:user_repository, -> { container[:data_store][:users] })
  container.register(:persist_user, -> { PersistUser.new })

  # set up your auto-injection function
  AutoInject = Dry::AutoInject(container)

  # define your injection function
  def self.Inject(*keys)
    AutoInject[*keys]
  end
end

# then simply include it in your class providing which dependencies should be
# injected automatically from the configured container
class PersistUser
  include MyApp::Inject(:user_repository)

  def call(user)
    user_repository << user
  end
end

persist_user = container[:persist_user]

persist_user.call(name: 'Jane')

Returns:

  • (Proc)

    calling the returned proc builds an auto-injection module



61
62
63
# File 'lib/dry/auto_inject.rb', line 61

def self.AutoInject(container, options = {})
  AutoInject::Builder.new(container, options)
end