Class: RubyOnAcid::SineFactory

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

Overview

Produces a “wave” pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Factory

#boolean, #choose, #get, #within

Constructor Details

#initialize(interval = 0.1) ⇒ SineFactory

Returns a new instance of SineFactory.



11
12
13
14
15
# File 'lib/rubyonacid/factories/sine.rb', line 11

def initialize(interval = 0.1)
  super
  @counters = {}
  @interval = interval
end

Instance Attribute Details

#intervalObject

Counters used to calculate sine values will be incremented by this amount with each query.



9
10
11
# File 'lib/rubyonacid/factories/sine.rb', line 9

def interval
  @interval
end

Instance Method Details

#get_unit(key) ⇒ Object

Increment counter for key and get its sine, then scale it between 0 and 1.



18
19
20
21
22
# File 'lib/rubyonacid/factories/sine.rb', line 18

def get_unit(key)
  @counters[key] ||= 0
  @counters[key] += @interval
  (Math.sin(@counters[key]) + 1) / 2
end