class DI::ServiceContainer
ServiceContainer
is used for registering services. It is also where the objects are stored.
You may register a service by providing a name and a block used to create the service. i.e. set(:your_service_name) {…} method.
Services may be retrieved by asking for them by name i.e get(:your_service_name) method.
Constants
- DuplicateServiceError
Thrown when a duplicate service is registered.
- MissingServiceError
Thrown when a service cannot be located by name.
Public Class Methods
# File lib/di.rb, line 18 def initialize @services = {} @cache = {} end
Public Instance Methods
Resets the cached services
# File lib/di.rb, line 46 def clear_cache! @cache = {} end
Retrieves a service named name
# File lib/di.rb, line 34 def get(name) @cache[name] ||= service_block(name).call(self) end
Return the block that creates the named service. Throw an exception if no service creation block of the given name can be found in the container
# File lib/di.rb, line 41 def service_block(name) @services[name] || fail(MissingServiceError, "No service is registered with the name '#{name}'") end
Registers a service named name
. The block
will be used to create the service on demand. It is recommended that symbols be used as the name of a service.
# File lib/di.rb, line 26 def set(name, &block) if @services[name] fail DuplicateServiceError, "A service already exists with name '#{name}'" end @services[name] = block end