Class: ArAggregateByInterval::QueryResult

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

Constant Summary collapse

VALID_HASH_ARGS =
{
  date_values_hash: [Hash],

  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(hash_args) ⇒ QueryResult

Returns a new instance of QueryResult.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ar_aggregate_by_interval/query_result.rb', line 20

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

  @dates_values_hash = hash_args[:date_values_hash]
  @date_iterator_method = Utils::DATE_ITERATOR_METHOD_MAP[hash_args[:interval]]

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

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

Instance Method Details

#valuesObject



42
43
44
# File 'lib/ar_aggregate_by_interval/query_result.rb', line 42

def values
  @values ||= values_and_dates.collect { |hash| hash[:value] }
end

#values_and_datesObject



33
34
35
36
37
38
39
40
# File 'lib/ar_aggregate_by_interval/query_result.rb', line 33

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