Class: Meerstats::LazyMetric
- Inherits:
-
Object
- Object
- Meerstats::LazyMetric
- Defined in:
- lib/meerstats/lazy_metric.rb
Defined Under Namespace
Classes: InvalidMetricTypeException
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #compute(days_ago: 1) ⇒ Object
-
#initialize(name, scope:, type:) ⇒ LazyMetric
constructor
A new instance of LazyMetric.
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
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/meerstats/lazy_metric.rb', line 5 def name @name end |
#scope ⇒ Object
Returns the value of attribute scope.
5 6 7 |
# File 'lib/meerstats/lazy_metric.rb', line 5 def scope @scope end |
#type ⇒ Object
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 |