class Nucleo::Models::MetricPeriodsComparisons

Public Class Methods

new(first_period, second_period) click to toggle source

Returns a Metrics Comparison domain model

@param first_period [Nucleo::Models::MetricPeriod] @param second_period [Nucleo::Models::MetricPeriod]

@return [Nucleo::Models::MetricPeriodComparison]

# File lib/nucleo/models/metric_periods_comparisons.rb, line 14
def initialize(first_period, second_period)
  @first_period  = first_period
  @second_period = second_period
end

Public Instance Methods

each(&block) click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 19
def each(&block)
  internal_collection.each(&block)
end

Private Instance Methods

all_rows() click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 68
def all_rows
  (first_period_rows.to_a + second_period_rows.to_a)
end
first_period() click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 52
def first_period
  @first_period
end
first_period_rows() click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 56
def first_period_rows
  first_period.rows
end
grouped_periods() click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 48
def grouped_periods
  all_rows.group_by(&:dimension_name)
end
internal_collection() click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 24
def internal_collection
  grouped_periods.map do |dimension, periods|
    current_period = periods.find(&:current?)
    prior_period   = periods.find(&:prior?)

    attributes = {
      'dimension'     => dimension,
      'current_value' => 0,
      'prior_value'   => 0,
      'change'        => 0,
    }

    if current_period
      attributes.merge!('current_value' => current_period.value, 'change' => current_period.change)
    end

    if prior_period
      attributes.merge!('prior_value' => prior_period.value)
    end

    Nucleo::Models::MetricPeriodsComparison.new(attributes)
  end
end
second_period() click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 60
def second_period
  @second_period
end
second_period_rows() click to toggle source
# File lib/nucleo/models/metric_periods_comparisons.rb, line 64
def second_period_rows
  second_period.rows
end