Module: Dio

Defined in:
lib/dio.rb,
lib/dio/equip.rb,
lib/dio/rails.rb,
lib/dio/state.rb,
lib/dio/version.rb,
lib/dio/injector.rb,
lib/dio/container.rb,
lib/dio/module_base.rb,
lib/dio/rails/model.rb,
lib/dio/load_context.rb,
lib/dio/injector_store.rb,
lib/dio/loader_factory.rb,
lib/dio/rails/controller.rb

Overview

Dio provides DI functionality. Note that most of the methods this module provides are defined at ModuleBase.

Defined Under Namespace

Modules: Equip, ModuleBase, Rails Classes: Container, Injector, InjectorStore, LoadContext, LoaderFactory, State

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.default_injectorDio::Injector

Returns a default Injector. By default all dependencies are registered and loaded via this injector. Its injector ID is ‘:default`.

Returns:



43
44
45
# File 'lib/dio.rb', line 43

def self.default_injector
  injector
end

.depends(*_args) ⇒ Object

Currently this method does nothing. This method is intended to be used in the following situation.

  • You use a dependencies provider class

  • The provider class is autoloadable (you can’t ‘require` it explicitly)

In that case, you need to autoload the provider class before using dependencies it provides. You can use this noop method for that purpose.

Examples:

class MyProvider
  include Dio

  provide :foo { Foo.new }
end

# In another file. You can autoload MyProvider naturally like this.
Dio.depends MyProvider

class UsersController < ApplicationController
  include Dio::Rails::Controller

  inject do |dio|
    @foo = dio.load(:foo)
  end
end

See Also:



75
# File 'lib/dio.rb', line 75

def self.depends(*_args); end

.use(injector_id, injector = nil) ⇒ Dio::ModuleBase

Creates a new Dio module with the specified Injector. You can use several injectors using this method.

Examples:

class Some
  include Dio.use(:another_injector)
  # ...
end

Parameters:

  • injector_id (Symbol)
  • injector (Dio::Injector, nil) (defaults to: nil)

Returns:



29
30
31
32
33
34
35
36
# File 'lib/dio.rb', line 29

def self.use(injector_id, injector = nil)
  Equip.equip_dio(
    injector_id: injector_id,
    state: @state,
    base_module: Module.new,
    injector: injector,
  )
end