Class: RubyOnAcid::InputFactory
- Defined in:
- lib/rubyonacid/factories/input.rb
Overview
Allows values to be assigned from an external source.
Instance Attribute Summary collapse
-
#default_factory ⇒ Object
A factory to pull values from in the event no values are stored for a given key.
Instance Method Summary collapse
-
#clear_assigned_keys ⇒ Object
Clear all value queue key assignments.
-
#clear_input_values ⇒ Object
Clears all stored input values for all keys.
-
#get_unit(key) ⇒ Object
Retrieves the next stored value for the given key.
-
#initialize ⇒ InputFactory
constructor
A new instance of InputFactory.
-
#put(key, value) ⇒ Object
Store a value for the given key.
Methods inherited from Factory
#boolean, #choose, #get, #within
Constructor Details
#initialize ⇒ InputFactory
Returns a new instance of InputFactory.
11 12 13 14 15 16 17 |
# File 'lib/rubyonacid/factories/input.rb', line 11 def initialize super @input_values = {} @key_assignments = {} @largest_seen_values = {} @smallest_seen_values = {} end |
Instance Attribute Details
#default_factory ⇒ Object
A factory to pull values from in the event no values are stored for a given key.
9 10 11 |
# File 'lib/rubyonacid/factories/input.rb', line 9 def default_factory @default_factory end |
Instance Method Details
#clear_assigned_keys ⇒ Object
Clear all value queue key assignments.
50 51 52 |
# File 'lib/rubyonacid/factories/input.rb', line 50 def clear_assigned_keys @assigned_keys = {} end |
#clear_input_values ⇒ Object
Clears all stored input values for all keys.
45 46 47 |
# File 'lib/rubyonacid/factories/input.rb', line 45 def clear_input_values @input_values = {} end |
#get_unit(key) ⇒ Object
Retrieves the next stored value for the given key. The key that values are pulled from will not necessarily be the same as that passed to put() - value queue keys are assigned to get_unit() keys at random.
21 22 23 24 25 26 27 28 |
# File 'lib/rubyonacid/factories/input.rb', line 21 def get_unit(key) current_key = assigned_key(key) if @input_values[current_key] and @input_values[current_key].length > 0 return scale(current_key, @input_values[current_key].shift) || default_value(key) else return default_value(key) end end |
#put(key, value) ⇒ Object
Store a value for the given key. Values will be scaled to the range 0 to 1 - the largest value yet seen will be scaled to 1.0, the smallest yet seen to 0.0.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rubyonacid/factories/input.rb', line 32 def put(key, value) value = value.to_f @input_values[key] ||= [] @input_values[key] << value @smallest_seen_values[key] ||= 0.0 if @largest_seen_values[key] == nil or @smallest_seen_values[key] > @largest_seen_values[key] @largest_seen_values[key] = @smallest_seen_values[key] + 1.0 end @smallest_seen_values[key] = value if value < @smallest_seen_values[key] @largest_seen_values[key] = value if value > @largest_seen_values[key] end |