summaryrefslogtreecommitdiff
path: root/fake-service/lib/smail/stats.rb
diff options
context:
space:
mode:
authorOla Bini <ola.bini@gmail.com>2014-07-31 19:35:40 -0300
committerOla Bini <ola.bini@gmail.com>2014-07-31 19:35:40 -0300
commite54e5ee931b3991cbb5e427e7e5d27b3f6c75e6e (patch)
tree1e0da33d22874c0ea5576818fe45958611ebda29 /fake-service/lib/smail/stats.rb
parent04cf441c5ae18400c6b4865b0b37a71718dc9d46 (diff)
Add fake-service
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