lib/rubyonacid/factories/meta.rb in rubyonacid-0.2.0 vs lib/rubyonacid/factories/meta.rb in rubyonacid-0.3.0
- old
+ new
@@ -1,28 +1,33 @@
require 'rubyonacid/factory'
module RubyOnAcid
+#The MetaFactory assigns factories to requested value types.
class MetaFactory < Factory
+ #An array of Factory objects to assign to keys.
attr_accessor :factory_pool
- attr_accessor :assigned_factories
def initialize(factory_pool = [])
super
@factory_pool = factory_pool
@assigned_factories = {}
end
+ #Assign a factory for subsequent get_unit requests for the given key.
def assign_factory(key, factory)
@assigned_factories[key] = factory
end
+ #Returns the value of get_unit from the Factory assigned to the given key.
+ #When a key is needed that a Factory is not already assigned to, one will be assigned at random from the factory_pool.
def get_unit(key)
@assigned_factories[key] ||= @factory_pool[rand(@factory_pool.length)]
@assigned_factories[key].get_unit(key)
end
+ #Clear all factory assignments.
def reset_assignments
@assigned_factories.clear
end
end
\ No newline at end of file