Class: RubyOnAcid::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyonacid/factory.rb

Overview

The parent class for all other Factories. Should not normally be instantiated directly.

Instance Method Summary collapse

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, options = {})
  @minimums[key] = (options[:min] || @minimums[key] || 0.0)
  @maximums[key] = (options[: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