Skip to content

Commit

Permalink
refactor tests to use Von.configure block, add redis connection
Browse files Browse the repository at this point in the history
  • Loading branch information
blahed committed Jan 25, 2013
1 parent db2eac2 commit 33775f7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/von.rb
Expand Up @@ -8,7 +8,7 @@

module Von
def self.connection
@connection ||= Redis.current
@connection ||= config.redis
end

def self.config
Expand Down
16 changes: 12 additions & 4 deletions lib/von/config.rb
Expand Up @@ -27,6 +27,18 @@ def reset!
self.hourly_format = '%Y-%m-%d %H:00'
end

def redis=(arg)
if arg.is_a? Redis
@redis = arg
else
@redis = Redis.new(arg)
end
end

def redis
@redis
end

def counter(field, options = {})
@counter_options[field.to_sym] = options
end
Expand All @@ -35,9 +47,5 @@ def counter_options(field)
@counter_options[field.to_sym] ||= {}
end

def configure(&block)
instance_eval &block
end

end
end
6 changes: 3 additions & 3 deletions lib/von/counter.rb
Expand Up @@ -2,19 +2,19 @@ module Von
class Counter
CHILD_REGEX = /:[^:]+\z/
PARENT_REGEX = /:?[^:]+\z/

# Initialize a new Counter
#
# field - counter field name
def initialize(field)
@field = field.to_sym
end

# Returns options specified in config for this Counter
def options
@options ||= Von.config.counter_options(@field)
end

# Returns periods specified in config for this Counter
def periods
@periods ||= options.select { |k|
Expand Down
19 changes: 13 additions & 6 deletions test/config_test.rb
Expand Up @@ -13,22 +13,29 @@
end

it 'initializes a config and overloads it with a block' do
@config.configure do
self.namespace = 'something'
end
@config.namespace = 'something'

@config.namespace.must_equal 'something'
end

it 'stores counter options per key and retrieves them' do
options = { :monthly => 3, :total => false }

@config.configure do
counter 'bar', options
end
@config.counter 'bar', options

@config.namespace.must_equal 'von'
@config.counter_options('bar').must_equal options
end

it "allows config options to be updated via configure" do
options = { :monthly => 3, :total => false }

Von.configure do |config|
config.counter 'bar', options
end

Von.config.namespace.must_equal 'von'
Von.config.counter_options('bar').must_equal options
end

end
12 changes: 6 additions & 6 deletions test/von_test.rb
Expand Up @@ -36,8 +36,8 @@
end

it "increments a month counter" do
Von.config.configure do
counter 'foo', :monthly => 1
Von.configure do |config|
config.counter 'foo', :monthly => 1
end

Von.increment('foo')
Expand All @@ -51,8 +51,8 @@
end

it "expires counters past the limit" do
Von.config.configure do
counter 'foo', :monthly => 1
Von.configure do |config|
config.counter 'foo', :monthly => 1
end

Von.increment('foo')
Expand All @@ -75,8 +75,8 @@
end

it "gets a count for a time period and 0s missing entries" do
Von.config.configure do
counter 'foo', :monthly => 1, :hourly => 6
Von.configure do |config|
config.counter 'foo', :monthly => 1, :hourly => 6
end

Timecop.freeze(Time.local(2013, 02, 01, 05))
Expand Down

0 comments on commit 33775f7

Please sign in to comment.