Class: ArAggregateByInterval::QueryResult

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

Constant Summary collapse

VALID_HASH_ARGS =
{
  ar_result: -> (v) { v.class.name == 'ActiveRecord::Relation' },

  # hash with 1 key where the key is a date column and value is the column being aggegated
  ar_result_select_col_mapping: -> (v) { v.is_a?(Hash) && v.size == 1 },

  from: [Date, DateTime, Time, ActiveSupport::TimeWithZone],
  to: [Date, DateTime, Time, ActiveSupport::TimeWithZone],

  interval: -> (v) { Utils.ruby_strftime_map.keys.include?(v) }
}

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ QueryResult

Returns a new instance of QueryResult.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ar_aggregate_by_interval/query_result.rb', line 23

def initialize(args)
  ClassyHash.validate(args, VALID_HASH_ARGS)

  @dates_values_hash = Utils.ar_to_hash(args[:ar_result], args[:ar_result_select_col_mapping])
  @date_iterator_method = Utils::DATE_ITERATOR_METHOD_MAP[args[:interval]]

  # strformat to match the format out of the database
  @strftime_format = Utils.ruby_strftime_map[args[:interval]]

  @from = args[:from]
  @to = args[:to]
end

Instance Method Details

#values_and_datesObject



36
37
38
39
40
41
42
43
# File 'lib/ar_aggregate_by_interval/query_result.rb', line 36

def values_and_dates
  @values_and_dates ||= array_of_dates.collect do |date, formatted_date|
    {
      date: date,
      value: Utils.to_f_or_i_or_s(@dates_values_hash[formatted_date] || 0)
    }
  end
end