Permalink
Browse files

put increment/count methods in von module

  • Loading branch information...
blahed committed Jan 30, 2013
1 parent 9982918 commit c534c3f1779c57f7a3a84a1f70ba5013e965fada
Showing with 73 additions and 6 deletions.
  1. +49 −3 lib/von.rb
  2. +1 −0 lib/von/period.rb
  3. +23 −3 test/von_test.rb
View
@@ -9,6 +9,8 @@
require 'von/version'
module Von
+ PARENT_REGEX = /:?[^:]+\z/
+
def self.connection
@connection ||= config.redis
end
@@ -22,16 +24,60 @@ def self.configure
end
def self.increment(field)
- Counter.increment(field)
+ parents = field.to_s.sub(PARENT_REGEX, '')
+ total = increment_counts_for(field)
+
+ until parents.empty? do
+ increment_counts_for(parents)
+ parents.sub!(PARENT_REGEX, '')
+ end
+
+ total
rescue Redis::BaseError => e
raise e if config.raise_connection_errors
end
+ def self.increment_counts_for(field)
+ counter = Counter.new(field)
+ total = counter.increment
+
+ if config.periods_defined_for_counter?(counter)
+ periods = config.periods[counter.field]
+ PeriodCounter.new(counter.field, periods).increment
+ elsif config.bests_defined_for_counter?(counter)
+ periods = config.bests[counter.field]
+ BestCounter.new(counter.field, periods).increment
+ end
+
+ total
+ end
+
+ # Increment the Redis count for this Counter.
+ # If the key has parents, increment them as well.
+ #
+ # Returns the Integer total for the key
+ # def self.increment(field)
+ # total = Counter.new(field).increment
+ # parents = field.sub(PARENT_REGEX, '')
+ #
+ # until parents.empty? do
+ # Counter.new(parents).increment
+ # parents.sub!(PARENT_REGEX, '')
+ # end
+ #
+ # total
+ # end
+
def self.count(field, period = nil)
- Counter.count(field, period)
+ if period.nil?
+ Counter.new(field).count
+ else
+ periods = config.periods[field.to_sym]
+ PeriodCounter.new(field, periods).count(period)
+ end
rescue Redis::BaseError => e
raise e if config.raise_connection_errors
end
config.init!
-end
+end
View
@@ -32,6 +32,7 @@ def initialize(period, length = nil)
@format = Von.config.send(:"#{@period}_format")
end
+ # TODO: take this out, dirty
def to_s
@period.to_s
end
View
@@ -3,15 +3,35 @@
describe Von do
before :each do
+ Timecop.freeze(Time.local(2013, 01, 01, 01, 01))
Von.config.init!
+ @redis = Redis.new
+ @redis.flushall
end
it "increments a counter and counts it" do
- Redis.new.flushall
-
+ 3.times { Von.increment('foo') }
+ Von.count('foo').must_equal 3
+ end
+
+ it "increments a counter and parent counters and counts them" do
+ 3.times { Von.increment('foo:bar') }
+ Von.count('foo').must_equal 3
+ Von.count('foo:bar').must_equal 3
+ end
+
+ it "increments period counters and counts them" do
+ Von.configure do |config|
+ config.counter 'foo', :monthly => 2
+ end
+
+ Von.increment('foo')
+ Timecop.freeze(Time.local(2013, 02))
Von.increment('foo')
+ Timecop.freeze(Time.local(2013, 03))
Von.increment('foo')
- Von.count('foo').must_equal 2
+
+ Von.count('foo', :monthly).must_equal [{"2013-02"=>1}, {"2013-03"=>1}]
end
it "raises a Redis connection errors if raise_connection_errors is true" do

0 comments on commit c534c3f

Please sign in to comment.