Class: Meerstats::LazyMetric

Inherits:
Object
  • Object
show all
Defined in:
lib/meerstats/lazy_metric.rb

Defined Under Namespace

Classes: InvalidMetricTypeException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, scope:, type:) ⇒ LazyMetric

Returns a new instance of LazyMetric.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/meerstats/lazy_metric.rb', line 5

def name
  @name
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/meerstats/lazy_metric.rb', line 5

def scope
  @scope
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/meerstats/lazy_metric.rb', line 5

def type
  @type
end

Instance Method Details

#compute(days_ago: 1) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# 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
end