You can declare requiring class dependencies and inject them using Justdi::Injectable
module.
This module provides dependency
interface helping to declare required dependency and approach describing how to use inject it.
class Orm; end
class Repository
extend Justdi::Injectable
dependency :orm
attr_reader :orm
def initialize(orm)
@orm = orm
end
end
container.register(:orm).use_class(Orm) }
container.resolve(Repository).orm # => #<Orm>
If you want to specify which approach to use for injection use destination
key as a second method argument.
:initializer
(default) - inject dependency into class initializer.
:method
- define method forwarding to resolved dependency
class Orm; end
class Repository
extend Justdi::Injectable
dependency :orm, destination: :method
end
container.register(:orm).use_class(Orm) }
repo = container.resolve(Repository)
repo.orm # => #<Orm>
:class_method
- define class object (static) method forwarding to resolved dependency.
class Orm; end
class Repository
extend Justdi::Injectable
dependency :orm, destination: :method
end
container.register(:orm).use_class(Orm) }
repo = container.resolve(Repository).orm # doesn't affect to original parent class
repo.class.orm # => #<Orm>