module Medicine::DI

Public Class Methods

new(injections = {}) click to toggle source

Injects dependencies

@param [Array<Object>] args - the last argument must be a Hash

@return [undefined]

@example

register_user = RegisterUser.new(user_repo: double('UserRepo'))

@api public

Calls superclass method
# File lib/medicine.rb, line 33
def initialize(injections = {})
  @injections = Injections.new
  injects(injections)
  super()
end

Private Class Methods

included(base) click to toggle source
# File lib/medicine.rb, line 84
def self.included(base)
  base.extend(ClassMethods)
end
prepended(base) click to toggle source
# File lib/medicine.rb, line 88
def self.prepended(base)
  base.extend(ClassMethods)
end

Public Instance Methods

inject_dependency(name, dependency) click to toggle source

Injects a dependency

@param [Symbol] key @param [Object] dependency

@return [self]

@example

register_user.inject_dependency(:user_repo, double('UserRepo'))

@api public

# File lib/medicine.rb, line 50
def inject_dependency(name, dependency)
  raise DependencyUnknownError, "#{name} has not been declared as a dependency" unless self.class.dependencies.include?(name)
  @injections.set(name, dependency)
  self
end
injections() click to toggle source

Returns injections

@example

register_user.injections

@return [Injections]

@api private

# File lib/medicine.rb, line 78
def injections
  @injections.dup.freeze
end
injects(injections) click to toggle source

Injects dependencies

@params [Hash] injections

@return [self]

@example

register_user.injects(user_repo: double, user_mailer: double)

@api public

# File lib/medicine.rb, line 66
def injects(injections)
  injections.each { |name, dependency| inject_dependency(name, dependency) }
end