Class: Bucketize::DailyData

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ce-bucketize/daily_data.rb

Overview

An Enumerable that will aggregate hourly data to daily data.

Author: ahmed.seddiq Version: 1.0

Instance Method Summary collapse

Constructor Details

#initialize(hourly_data, aggregate_method) ⇒ DailyData

Creates a new instance of this Enumerable

hourly_data - the hourly data source. (an Enumerable) aggregated_method - the method that will be called on the items from the

source and summed up in the result value.


15
16
17
18
# File 'lib/ce-bucketize/daily_data.rb', line 15

def initialize (hourly_data, aggregate_method)
  @hourly_data = hourly_data
  @aggregate_method = aggregate_method
end

Instance Method Details

#each {|date, day_value| ... } ⇒ Object

Yields:

  • (date, day_value)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ce-bucketize/daily_data.rb', line 20

def each(&block)
  return enum_for(:each) if block.nil?

  date = @hourly_data.first.hour_start
  day_value = 0
  day = date.day
  @hourly_data.each do |h_data|
    value = h_data.public_send(@aggregate_method)
    if h_data.hour_start.day == day
      day_value += value
    else
      yield date, day_value
      date = h_data.hour_start
      day_value = value
      day = date.day
    end
  end
  # the last
  yield date, day_value
end