summaryrefslogtreecommitdiff
path: root/fake-service/lib/smail/stats.rb
diff options
context:
space:
mode:
Diffstat (limited to 'fake-service/lib/smail/stats.rb')
-rw-r--r--fake-service/lib/smail/stats.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/fake-service/lib/smail/stats.rb b/fake-service/lib/smail/stats.rb
new file mode 100644
index 00000000..4e0393a4
--- /dev/null
+++ b/fake-service/lib/smail/stats.rb
@@ -0,0 +1,60 @@
+
+module Smail
+ 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