Class: ActiveAnalytics::ViewsPerDay::Histogram
- Inherits:
-
Object
- Object
- ActiveAnalytics::ViewsPerDay::Histogram
- Defined in:
- app/models/active_analytics/views_per_day.rb
Defined Under Namespace
Classes: Bar
Instance Attribute Summary collapse
-
#bars ⇒ Object
readonly
Returns the value of attribute bars.
-
#from_date ⇒ Object
readonly
Returns the value of attribute from_date.
-
#to_date ⇒ Object
readonly
Returns the value of attribute to_date.
Instance Method Summary collapse
- #fill_missing_days(bars, from, to) ⇒ Object
-
#initialize(scope, from_date, to_date) ⇒ Histogram
constructor
A new instance of Histogram.
- #max_value ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(scope, from_date, to_date) ⇒ Histogram
Returns a new instance of Histogram.
39 40 41 42 |
# File 'app/models/active_analytics/views_per_day.rb', line 39 def initialize(scope, from_date, to_date) @bars = scope.map { |day| Bar.new(day.day, day.total, self) } fill_missing_days(@bars, Date.parse(from_date.to_s), Date.parse(to_date.to_s)) end |
Instance Attribute Details
#bars ⇒ Object (readonly)
Returns the value of attribute bars.
37 38 39 |
# File 'app/models/active_analytics/views_per_day.rb', line 37 def @bars end |
#from_date ⇒ Object (readonly)
Returns the value of attribute from_date.
37 38 39 |
# File 'app/models/active_analytics/views_per_day.rb', line 37 def from_date @from_date end |
#to_date ⇒ Object (readonly)
Returns the value of attribute to_date.
37 38 39 |
# File 'app/models/active_analytics/views_per_day.rb', line 37 def to_date @to_date end |
Instance Method Details
#fill_missing_days(bars, from, to) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/active_analytics/views_per_day.rb', line 44 def fill_missing_days(, from, to) i = 0 while (day = from + i) <= to if !@bars[i] || @bars[i].label != day @bars.insert(i, Bar.new(day, 0, self)) end i += 1 end @bars end |
#max_value ⇒ Object
55 56 57 |
# File 'app/models/active_analytics/views_per_day.rb', line 55 def max_value @max_total ||= .map(&:value).max end |
#total ⇒ Object
59 60 61 |
# File 'app/models/active_analytics/views_per_day.rb', line 59 def total @bars.reduce(0) { |sum, | sum += .value } end |