Class: Stats

Inherits:
Object
  • Object
show all
Defined in:
app/services/stats.rb

Defined Under Namespace

Classes: StatsEntry, StatsValue

Instance Method Summary collapse

Constructor Details

#initialize(stats_map) ⇒ Stats


3
4
5
6
7
8
9
10
11
# File 'app/services/stats.rb', line 3

def initialize(stats_map)
  @stats = []
  dates = stats_map.keys.sort
  dates.each_with_index do |date, index|
    old_date = index == 0 ? nil : dates[index-1]
    @stats << StatsEntry.new(date, stats_map[date], stats_map[old_date])
  end
  @stats.shift
end

Instance Method Details

#average(key, use_changes: false) ⇒ Object


17
18
19
20
21
# File 'app/services/stats.rb', line 17

def average(key, use_changes: false)
  mean(key, use_changes: use_changes) do |values|
    (values.inject(:+).to_f / values.size).round(1)
  end
end

#each(&block) ⇒ Object


13
14
15
# File 'app/services/stats.rb', line 13

def each(&block)
  @stats.each(&block)
end

#median(key, use_changes: false) ⇒ Object


23
24
25
26
27
# File 'app/services/stats.rb', line 23

def median(key, use_changes: false)
  mean(key, use_changes: use_changes) do |values|
    values[values.size/2]
  end
end