Class: RubyOnAcid::AttractionFactory

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

Constant Summary collapse

SQUARE_ROOT_OF_TWO =
Math.sqrt(2.0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Factory

#boolean, #choose, #get, #within

Constructor Details

#initialize(source_factory = nil, attractor_factory = nil) ⇒ AttractionFactory

Returns a new instance of AttractionFactory.


15
16
17
18
19
# File 'lib/rubyonacid/factories/attraction.rb', line 15

def initialize(source_factory = nil, attractor_factory = nil)
  super
  @source_factory = source_factory
  @attractor_factory = attractor_factory
end

Instance Attribute Details

#attractor_factoryObject

Values from source_factory will be “pulled” toward values from this factory.


13
14
15
# File 'lib/rubyonacid/factories/attraction.rb', line 13

def attractor_factory
  @attractor_factory
end

#source_factoryObject

Factory to get values from.


10
11
12
# File 'lib/rubyonacid/factories/attraction.rb', line 10

def source_factory
  @source_factory
end

Instance Method Details

#get_unit(key) ⇒ Object

Get a value from the source_factory and a value from the attractor_factory. The source_factory value will be adjusted to be closer to the attractor_factory value. The closer the values are, the greater the adjustment.


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubyonacid/factories/attraction.rb', line 24

def get_unit(key)
  # force = delta * @magnetism / (distance * Jemini::Math::SQUARE_ROOT_OF_TWO)
  value = @source_factory.get_unit(key)
  attractor_value = @attractor_factory.get_unit(key)
  distance = value - attractor_value
  return_value = value - (0.1 / (distance * SQUARE_ROOT_OF_TWO))
  if value > attractor_value and return_value < attractor_value
    return_value = attractor_value
  elsif value < attractor_value and return_value > attractor_value
    return_value = attractor_value
  end
  return_value
end