Permalink
Browse files

use a hash for time unit lookup

  • Loading branch information...
1 parent 075a53d commit d427a9142e5d6d67c8db5a3f7dd36041763ae02b @blahed committed Jan 29, 2013
Showing with 15 additions and 20 deletions.
  1. +5 −5 lib/von/config.rb
  2. +10 −15 lib/von/period.rb
View
@@ -23,15 +23,15 @@ def init!
# rescue Redis connection errors
self.raise_connection_errors = false
# 2013
- self.yearly_format = '%Y'
+ self.yearly_format = '%Y'
# 2013-01
- self.monthly_format = '%Y-%m'
+ self.monthly_format = '%Y-%m'
# 2013-01-02
- self.weekly_format = '%Y-%m-%d'
+ self.weekly_format = '%Y-%m-%d'
# 2013-01-02
- self.daily_format = '%Y-%m-%d'
+ self.daily_format = '%Y-%m-%d'
# 2013-01-02 12:00
- self.hourly_format = '%Y-%m-%d %H:00'
+ self.hourly_format = '%Y-%m-%d %H:00'
# 2013-01-02 12:05
self.minutely_format = '%Y-%m-%d %H:%M'
end
View
@@ -1,6 +1,14 @@
module Von
class Period
AVAILABLE_PERIODS = [ :minutely, :hourly, :daily, :weekly, :monthly, :yearly ]
+ TIME_UNITS = {
+ :minutely => :minute,
+ :hourly => :hour,
+ :daily => :day,
+ :weekly => :week,
+ :monthly => :month,
+ :yearly => :year
+ }
attr_reader :counter_key
attr_reader :length
@@ -13,28 +21,15 @@ class Period
# length - length of period
def initialize(counter_key, period, length)
@counter_key = counter_key
- @period = period
+ @period = period.to_sym
@length = length
@format = Von.config.send(:"#{@period}_format")
end
# Returns a Symbol representing the time unit
# for the current period.
def time_unit
- @time_unit ||= case @period
- when :minutely
- :minute
- when :hourly
- :hour
- when :daily
- :day
- when :weekly
- :week
- when :monthly
- :month
- when :yearly
- :year
- end
+ @time_unit ||= TIME_UNITS[@period]
end
# Returns True or False if the period is hourly

0 comments on commit d427a91

Please sign in to comment.