Class: Bucketize::TariffedHourlyValues

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

Overview

An Enumerable for tariffed hourly consumption values. This collection will be initialized by the input HourlyValues and the TariffRule(s). It will provide the values after applying the given TariffRule(s). If no rule was matched , the corresponding hourly value will be returned.

Author: ahmed.seddiq Version: 1.0

Instance Method Summary collapse

Constructor Details

#initialize(hourly_values, tariff_rules) ⇒ TariffedHourlyValues

Initializes this instance with the given hourly values and tariff rules.

hourly_values - the Collection of hourly values on which the tariff rules

will be applied.

tariff_rules - the Array of TariffRules to be used for aggregation.



21
22
23
24
# File 'lib/ce-bucketize/tariffed_hourly_values.rb', line 21

def initialize(hourly_values, tariff_rules)
  @hourly_values = hourly_values
  @tariff_rules = tariff_rules
end

Instance Method Details

#each(&block) ⇒ Object

The “each” method required by the Enumerable mixin. It yields the HourCost instances after applying the given TariffRule(s).

Yields HourCost instances representing the tariffed hourly cost of the data.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ce-bucketize/tariffed_hourly_values.rb', line 30

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

  total_kwh = 0
  @hourly_values.each do |h_value|
    total_kwh += h_value.value
    @tariff_rules.each do |rule|
      if rule.applies_to? h_value.hour_start, total_kwh
        h_value.value = h_value.value * rule.tariff
        break
      end
    end
    yield h_value
  end
end