Class: Injectable::Container
- Inherits:
-
Object
- Object
- Injectable::Container
- Defined in:
- lib/injectable/container.rb
Overview
A simple container that can resolve dependencies.
Instance Method Summary collapse
-
#get(name) ⇒ Object
Get an instance of an object from the container with the provided class.
-
#initialize(*objects) ⇒ Container
constructor
Create a new container with the objects needed to resolve dependencies and create new objects.
-
#register_implementation(name, klass) ⇒ Object
Register that instances of klass will perform the given role in this container context.
Constructor Details
#initialize(*objects) ⇒ Container
Create a new container with the objects needed to resolve dependencies and create new objects.
40 41 42 43 44 |
# File 'lib/injectable/container.rb', line 40 def initialize(*objects) objects.each do |object| instantiated_objects[object.class] = object end end |
Instance Method Details
#get(name) ⇒ Object
Get an instance of an object from the container with the provided class.
22 23 24 25 26 27 28 29 |
# File 'lib/injectable/container.rb', line 22 def get(name) klass = implementing_class(name) if instantiated_objects.has_key?(klass) instantiated_objects[klass] else instantiated_objects[klass] = instantiate(klass) end end |
#register_implementation(name, klass) ⇒ Object
Register that instances of klass will perform the given role in this container context.
57 58 59 |
# File 'lib/injectable/container.rb', line 57 def register_implementation(name, klass) implementing_classes[name] = klass end |