Class: RubyOnAcid::Factory
- Inherits:
-
Object
- Object
- RubyOnAcid::Factory
- Defined in:
- lib/rubyonacid/factory.rb
Overview
The parent class for all other Factories. Should not normally be instantiated directly.
Direct Known Subclasses
AttractionFactory, CombinationFactory, ConstantFactory, FlashFactory, IncrementFactory, InputFactory, LoopFactory, MetaFactory, RandomFactory, RandomWalkFactory, RepeatFactory, RindaFactory, RoundingFactory, SineFactory, SkipFactory
Instance Method Summary collapse
-
#boolean(key) ⇒ Object
Returns true if get_unit(key) returns greater than 0.5.
-
#choose(key, *choices) ⇒ Object
Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to an index within the given list of choices.
-
#get(key, options = {}) ⇒ Object
Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to be between given minimum and maximum.
-
#initialize(*args) ⇒ Factory
constructor
A new instance of Factory.
-
#within(key, minimum, maximum) ⇒ Object
Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to be between given minimum and maximum.
Constructor Details
#initialize(*args) ⇒ Factory
Returns a new instance of Factory.
7 8 9 10 |
# File 'lib/rubyonacid/factory.rb', line 7 def initialize(*args) @minimums = {} @maximums = {} end |
Instance Method Details
#boolean(key) ⇒ Object
Returns true if get_unit(key) returns greater than 0.5.
25 26 27 |
# File 'lib/rubyonacid/factory.rb', line 25 def boolean(key) get_unit(key) >= 0.5 end |
#choose(key, *choices) ⇒ Object
Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to an index within the given list of choices. choices can be an array or an argument list of arbitrary size.
31 32 33 34 35 36 |
# File 'lib/rubyonacid/factory.rb', line 31 def choose(key, *choices) all_choices = choices.flatten index = (get_unit(key) * all_choices.length).floor index = all_choices.length - 1 if index > all_choices.length - 1 all_choices[index] end |
#get(key, options = {}) ⇒ Object
Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to be between given minimum and maximum.
18 19 20 21 22 |
# File 'lib/rubyonacid/factory.rb', line 18 def get(key, = {}) @minimums[key] = ([:min] || @minimums[key] || 0.0) @maximums[key] = ([:max] || @maximums[key] || (@minimums[key] > 1.0 ? @minimums[key] + 1.0 : 1.0)) (get_unit(key) * (@maximums[key] - @minimums[key])) + @minimums[key] end |
#within(key, minimum, maximum) ⇒ Object
Calls get_unit with key to get value between 0.0 and 1.0, then converts that value to be between given minimum and maximum.
13 14 15 |
# File 'lib/rubyonacid/factory.rb', line 13 def within(key, minimum, maximum) get_unit(key) * (maximum - minimum) + minimum end |