module Dio

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

@see Dio::ModuleBase @see Dio::ModuleBase::ClassMethods

Constants

VERSION

Public Class Methods

default_injector() click to toggle source

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

@return [Dio::Injector]

# File lib/dio.rb, line 43
def self.default_injector
  injector
end
depends(*_args) click to toggle source

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.

@example

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

http://guides.rubyonrails.org/autoloading_and_reloading_constants.html
# File lib/dio.rb, line 75
def self.depends(*_args); end
use(injector_id, injector = nil) click to toggle source

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

@param injector_id [Symbol] @param injector [Dio::Injector, nil] @return [Dio::ModuleBase] A module extends Dio::ModuleBase.

@example

class Some
  include Dio.use(:another_injector)
  # ...
end
# 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