Module: Injectable::Registry

Extended by:
Registry
Included in:
Registry
Defined in:
lib/injectable/registry.rb

Overview

The registry keeps track of all objects and their dependencies that need to be injected at construction.

Since:

  • 0.0.0

Defined Under Namespace

Classes: NotRegistered

Instance Method Summary collapse

Instance Method Details

#implementation(name) ⇒ Class

Get an implementation for the provided name.

Examples:

Get an implementation.

Injectable::Registry.implementation(:persistable)

Parameters:

  • name (Symbol)

    The name of the implementation.

Returns:

  • (Class)

    The implementing class.

Since:

  • 0.0.2



21
22
23
# File 'lib/injectable/registry.rb', line 21

def implementation(name)
  implementations[name] || raise(NotRegistered.new(name))
end

#register_implementation(name, klass) ⇒ Object

Add an implementing class for a name to the registry.

Examples:

Add an implementation.

Injectable::Registry.register_implementation(
  :persistable, User
)

Parameters:

  • name (Symbol)

    The name of the implementation.

  • klass (Class)

    The implementing class.

Since:

  • 0.0.2



36
37
38
# File 'lib/injectable/registry.rb', line 36

def register_implementation(name, klass)
  implementations[name] = klass
end

#register_signature(klass, dependencies) ⇒ Object

Add a constructor method signature to the registry.

Examples:

Add a signature.

Injectable::Registry.register_signature(
  UserService, [ :user, :user_finder ]
)

Parameters:

  • klass (Class)

    The class to set the constructor signature for.

  • dependencies (Array<Symbol>)

    The dependencies of the constructor.

Since:

  • 0.0.0



52
53
54
# File 'lib/injectable/registry.rb', line 52

def register_signature(klass, dependencies)
  signatures[klass] = dependencies.map { |name| name }
end

#signature(klass) ⇒ Array<Class>

Get the constructor method signature for the provided class.

Examples:

Get the constructor signature.

Injectable::Registry.signature(UserService)

Parameters:

  • klass (Class)

    The class to get the signature for.

Returns:

  • (Array<Class>)

    The constructor signature.

Since:

  • 0.0.0



66
67
68
# File 'lib/injectable/registry.rb', line 66

def signature(klass)
  signatures[klass]
end