class Meerstats::LazyMetric

Attributes

name[RW]
scope[RW]
type[RW]

Public Class Methods

new(name, scope:, type:) click to toggle source
# File lib/meerstats/lazy_metric.rb, line 7
def initialize(name, scope:, type:)
  raise InvalidMetricTypeException unless ['daily', 'cumulative'].include?(type)

  self.name = name
  self.scope = scope
  self.type = type
end

Public Instance Methods

compute(days_ago: 1) click to toggle source
# File lib/meerstats/lazy_metric.rb, line 15
def compute(days_ago: 1)
  computed_scope = scope.call

  if type == 'daily'
    computed_scope = computed_scope.where("date(created_at) = date(?)", days_ago.to_i.days.ago)
  elsif type == 'cumulative'
    computed_scope = computed_scope.where("date(created_at) <= date(?)", days_ago.to_i.days.ago)
  end

  computed_scope.count
rescue
  0
end