Class: ActiveAnalytics::ViewsPerDay::Histogram

Inherits:
Object
  • Object
show all
Defined in:
app/models/active_analytics/views_per_day.rb

Defined Under Namespace

Classes: Bar

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#barsObject (readonly)

Returns the value of attribute bars.



37
38
39
# File 'app/models/active_analytics/views_per_day.rb', line 37

def bars
  @bars
end

#from_dateObject (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_dateObject (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(bars, 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_valueObject



55
56
57
# File 'app/models/active_analytics/views_per_day.rb', line 55

def max_value
  @max_total ||= bars.map(&:value).max
end

#totalObject



59
60
61
# File 'app/models/active_analytics/views_per_day.rb', line 59

def total
  @bars.reduce(0) { |sum, bar| sum += bar.value }
end