Class: TallyCounter::Window

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interval) ⇒ Window

Returns a new instance of Window.

Parameters:

  • seconds (Integer)

    of granularity for the window



12
13
14
# File 'lib/tally_counter.rb', line 12

def initialize(interval)
  @interval = interval
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



9
10
11
# File 'lib/tally_counter.rb', line 9

def interval
  @interval
end

Instance Method Details

#floor(time, offset = 0) ⇒ Object

Returns the floor time for a given window. For example, if the interval is 5 minutes and it is 12:38, the floor would be 12:35. If offset is 1, the floor would be 12:30

Parameters:

  • a (Time)

    time instance, commonly Time.now

  • an (offet)

    integer of windows step back to



22
23
24
25
# File 'lib/tally_counter.rb', line 22

def floor(time, offset = 0)
  epoch_time = time.to_i - interval * offset
  Time.at((epoch_time / interval) * interval)
end