Class: RubyOnAcid::CombinationFactory

Inherits:
Factory
  • Object
show all
Defined in:
lib/rubyonacid/factories/combination.rb

Constant Summary collapse

ADD =

Causes get_unit value of all source_factories to be added together.

:add
SUBTRACT =

Takes the get_unit value of the first of the source_factories and subtracts the get_unit value of all subsequent ones.

:subtract
MULTIPLY =

Causes get_unit value of all source_factories to be multiplied.

:multiply
DIVIDE =

Takes the get_unit value of the first of the source_factories and divides the result by the get_unit value of all subsequent ones.

:divide
CONSTRAIN =

Causes get_unit values above 1 to be truncated at 1 and values below 0 to be truncated at 0.

:constrain
WRAP =

Causes get_unit values above 1 to wrap to 0 and values below 0 to wrap to 1.

:wrap

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Factory

#boolean, #choose, #get, #within

Constructor Details

#initialize(options = {}) ⇒ CombinationFactory

Returns a new instance of CombinationFactory.



27
28
29
30
31
32
# File 'lib/rubyonacid/factories/combination.rb', line 27

def initialize(options = {})
  super
  @source_factories = options[:source_factories] || []
  @operation = options[:operation] || ADD
  @constrain_mode = options[:constrain_mode] || WRAP
end

Instance Attribute Details

#constrain_modeObject

The method get_unit will use to constrain values between 0 and 1.



25
26
27
# File 'lib/rubyonacid/factories/combination.rb', line 25

def constrain_mode
  @constrain_mode
end

#operationObject

The operation get_unit will perform.



23
24
25
# File 'lib/rubyonacid/factories/combination.rb', line 23

def operation
  @operation
end

#source_factoriesObject

An array of factories to be queried by get_unit.



21
22
23
# File 'lib/rubyonacid/factories/combination.rb', line 21

def source_factories
  @source_factories
end

Instance Method Details

#get_unit(key) ⇒ Object

Queries all source_factories with given key and combines their return values with the set operation. Values will be constrained between 0 and 1 with the set constrain_mode.



36
37
38
39
# File 'lib/rubyonacid/factories/combination.rb', line 36

def get_unit(key)
  combined_value = combine(key)
  constrain(combined_value)
end