Skip to content

Commit

Permalink
add "currents" to config
Browse files Browse the repository at this point in the history
  • Loading branch information
blahed committed Feb 1, 2013
1 parent 0cd7239 commit ed05f81
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
18 changes: 13 additions & 5 deletions lib/von/config.rb
Expand Up @@ -13,13 +13,15 @@ module Config
attr_accessor :hourly_format
attr_accessor :minutely_format

attr_reader :periods
attr_reader :bests
attr_reader :periods
attr_reader :bests
attr_reader :currents

def init!
@periods = {}
@bests = {}
@totals = {}
@periods = {}
@bests = {}
@currents = {}
@totals = {}
# all keys are prefixed with this namespace
self.namespace = 'von'
# rescue Redis connection errors
Expand Down Expand Up @@ -63,6 +65,7 @@ def counter(field, options = {})
options.each do |option, value|
set_period(field, option, value) if Period.exists?(option)
set_best(field, value) if option == :best
set_current(field, value) if option == :current
end
end

Expand Down Expand Up @@ -92,6 +95,11 @@ def set_best(field, time_unit)
}
end

def set_current(field, time_unit)
@currents[field] = [ time_unit ].flatten.map { |u|
Period.new(u)
}
end

end
end
18 changes: 16 additions & 2 deletions test/config_test.rb
Expand Up @@ -18,7 +18,7 @@
@config.namespace.must_equal 'something'
end

it "allows periods to be set via counter method" do
it "sets periods via counter method" do
Von.configure do |config|
config.counter 'bar', :monthly => 3, :daily => 6
end
Expand All @@ -30,7 +30,7 @@
Von.config.periods[:bar].last.length.must_equal 6
end

it "allows bests to be set via counter method" do
it "sets bests via counter method" do
Von.configure do |config|
config.counter 'bar', :best => :day
config.counter 'foo', :best => [ :month, :year ]
Expand All @@ -44,4 +44,18 @@
Von.config.bests[:foo].last.name.must_equal :yearly
end

it "sets currents via counter method" do
Von.configure do |config|
config.counter 'bar', :current => :day
config.counter 'foo', :current => [ :month, :year ]
end

Von.config.currents[:bar].first.must_be_instance_of Von::Period
Von.config.currents[:bar].first.name.must_equal :daily
Von.config.currents[:foo].first.must_be_instance_of Von::Period
Von.config.currents[:foo].first.name.must_equal :monthly
Von.config.currents[:foo].last.must_be_instance_of Von::Period
Von.config.currents[:foo].last.name.must_equal :yearly
end

end

0 comments on commit ed05f81

Please sign in to comment.