Skip to content

Commit

Permalink
period attribute -> name
Browse files Browse the repository at this point in the history
  • Loading branch information
blahed committed Jan 31, 2013
1 parent cbc7202 commit dfd09e5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/von/counters/period.rb
Expand Up @@ -46,7 +46,7 @@ def count(period)

counts = []
this_period = nil
_period = @periods.select { |p| p.period == period }.first
_period = @periods.select { |p| p.name == period }.first

_period.length.times do |i|
this_period = _period.prev(i)
Expand Down
24 changes: 12 additions & 12 deletions lib/von/period.rb
Expand Up @@ -11,7 +11,7 @@ class Period
AVAILABLE_PERIODS = PERIOD_MAPPING.keys
AVAILABLE_TIME_UNITS = PERIOD_MAPPING.values

attr_reader :period
attr_reader :name
attr_reader :length
attr_reader :format

Expand All @@ -20,37 +20,37 @@ class Period
# period - the time period one of AVAILABLE_PERIODS
# length - length of period
def initialize(period, length = nil)
period = period.to_sym
if AVAILABLE_PERIODS.include?(period)
@period = period
elsif AVAILABLE_TIME_UNITS.include?(period)
@period = PERIOD_MAPPING.invert[period]
name = period.to_sym
if AVAILABLE_PERIODS.include?(name)
@name = name
elsif AVAILABLE_TIME_UNITS.include?(name)
@name = PERIOD_MAPPING.invert[name]
else
raise ArgumentError, "`#{period}' is not a valid period"
end
@length = length
@format = Von.config.send(:"#{@period}_format")
@format = Von.config.send(:"#{@name}_format")
end

# TODO: take this out, dirty
def to_s
@period.to_s
@name.to_s
end

# Returns a Symbol representing the time unit
# for the current period.
def time_unit
@time_unit ||= PERIOD_MAPPING[@period]
@time_unit ||= PERIOD_MAPPING[@name]
end

# Returns True or False if the period is hourly
def hours?
@period == :hourly
@name == :hourly
end

# Returns True or False if the period is minutely
def minutes?
@period == :minutely
@name == :minutely
end

def beginning(time)
Expand All @@ -68,7 +68,7 @@ def prev(unit = 1)
def timestamp
beginning(Time.now).strftime(format)
end

def self.unit_to_period(time_unit)
PERIOD_MAPPING.invert[time_unit]
end
Expand Down
10 changes: 5 additions & 5 deletions test/config_test.rb
Expand Up @@ -24,9 +24,9 @@
end

Von.config.periods[:bar].length.must_equal 2
Von.config.periods[:bar].first.period.must_equal :monthly
Von.config.periods[:bar].first.name.must_equal :monthly
Von.config.periods[:bar].first.length.must_equal 3
Von.config.periods[:bar].last.period.must_equal :daily
Von.config.periods[:bar].last.name.must_equal :daily
Von.config.periods[:bar].last.length.must_equal 6
end

Expand All @@ -37,11 +37,11 @@
end

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

end

0 comments on commit dfd09e5

Please sign in to comment.