summaryrefslogtreecommitdiff
path: root/fake-service/lib/pixelated_service/stats.rb
blob: 3e79b2264ab19ea76b320a90085e155039b9e4f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

module PixelatedService
  module Stats
    class StatsCollector
      include Stats
      def initialize
        stats_init
      end
    end

    attr_reader :stats

    def stats_init
      @stats = {
        total: 0,
        read: 0,
        starred: 0,
        replied: 0
      }
    end

    def stats_added(m)
      @stats[:total] += 1
      stats_status_added(:read, m)    if m.status?(:read)
      stats_status_added(:replied, m) if m.status?(:replied)
      stats_status_added(:starred, m) if m.status?(:starred)
    end

    def stats_removed(m)
      @stats[:total] -= 1
      stats_status_removed(:read, m)    if m.status?(:read)
      stats_status_removed(:replied, m) if m.status?(:replied)
      stats_status_removed(:starred, m) if m.status?(:starred)
    end

    def stats_status_added(s, m)
      @stats[s] += 1
    end

    def stats_status_removed(s, m)
      @stats[s] -= 1
    end

    def each_total_helper(enum)
      if enum.respond_to?(:each_total)
        enum.each_total { |x| yield x }
      else
        enum.each { |x| yield x }
      end
    end

    def with_stats(enum)
      sc = StatsCollector.new
      each_total_helper(enum) do |e|
        sc.stats_added(e)
      end
      [sc.stats, enum]
    end
  end
end