Module: Dio::ModuleBase::ClassMethods

Defined in:
lib/dio/module_base.rb

Overview

ClassMethods defines class methods for classes using Dio.

Instance Method Summary collapse

Instance Method Details

#__dio_injection_proc__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.



119
120
121
# File 'lib/dio/module_base.rb', line 119

def __dio_injection_proc__
  @__dio_injection_proc__
end

#inject {|Dio::LoaderFactory::Loader| ... } ⇒ Object

Defines a block to load dependencies from Dio. A given block is evaluated in each instance context of the class.



114
115
116
# File 'lib/dio/module_base.rb', line 114

def inject(&injector)
  @__dio_injection_proc__ = injector
end

#injectable(subkey = nil) { ... } ⇒ Object

Declares the class is an injectable via Dio. You can define a factory block.

Parameters:

  • subkey (Symbol, nil) (defaults to: nil)

Yields:

  • passed arguments when loading



95
96
97
98
99
# File 'lib/dio/module_base.rb', line 95

def injectable(subkey = nil, &block)
  key = subkey ? [self, subkey] : self
  factory = block || ->(*args) { new(*args) }
  __dio_injector__.register(key, &factory)
end

#provide(key) { ... } ⇒ Object

Registers a factory of a dependency.

Parameters:

  • key (Symbol)

Yields:

  • passed arguments when loading



105
106
107
108
# File 'lib/dio/module_base.rb', line 105

def provide(key, &factory)
  raise "You must define a factory of #{key}" unless block_given?
  __dio_injector__.register(key, &factory)
end