Sha256: f658e1f780ae06efd8f18864bf86f3d3047a34bb09d78d45ada95bdf64c31dfc

Contents?: true

Size: 904 Bytes

Versions: 1

Compression:

Stored size: 904 Bytes

Contents

require 'rubyonacid/factory'

module RubyOnAcid

#The MetaFactory assigns its source factories to requested value types.
class MetaFactory < Factory
  
  #Takes a hash with all keys supported by Factory.
  def initialize(options = {})
    super
    @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 pool of source factories.
  def get_unit(key)
    @assigned_factories[key] ||= source_factories[rand(source_factories.length)]
    @assigned_factories[key].get_unit(key)
  end
  
  #Clear all factory assignments.
  def reset_assignments
    @assigned_factories.clear
  end

end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyonacid-0.4.0 lib/rubyonacid/factories/meta.rb