Class: Dokkit::Factory
- Inherits:
-
Object
- Object
- Dokkit::Factory
- Defined in:
- lib/dokkit/factory/factory.rb
Instance Attribute Summary collapse
-
#instances ⇒ Object
readonly
Return stored instances.
-
#methods ⇒ Object
readonly
Return stored factory method.
Instance Method Summary collapse
-
#add(key, &factory_method) ⇒ Object
Add a factory method associated with the given class of objects.
-
#get(key, *params) ⇒ Object
Construct an instance for the given class.
-
#initialize {|_self| ... } ⇒ Factory
constructor
Initialize a Factory object.
Constructor Details
#initialize {|_self| ... } ⇒ Factory
Initialize a Factory object. Configuration is made passing a code block.
20 21 22 23 |
# File 'lib/dokkit/factory/factory.rb', line 20 def initialize @instances, @methods = { }, { } yield self if block_given? end |
Instance Attribute Details
#instances ⇒ Object (readonly)
Return stored instances.
13 14 15 |
# File 'lib/dokkit/factory/factory.rb', line 13 def instances @instances end |
#methods ⇒ Object (readonly)
Return stored factory method.
16 17 18 |
# File 'lib/dokkit/factory/factory.rb', line 16 def methods @methods end |
Instance Method Details
#add(key, &factory_method) ⇒ Object
Add a factory method associated with the given class of objects. factory_method
is a class/method pair.
Example:
factory.add(:bar) { Bar.new } # add a method that
# instantiates Bar object
33 34 35 |
# File 'lib/dokkit/factory/factory.rb', line 33 def add(key, &factory_method) @methods[key] = factory_method end |
#get(key, *params) ⇒ Object
Construct an instance for the given class. Note that, if an instance of the same class has been already instantiated for a given parameters set, then no new instance will be created but the first one will be returned.
41 42 43 |
# File 'lib/dokkit/factory/factory.rb', line 41 def get(key, *params) get_instance(key, *params) || store_instance(key, *params) end |