Skip to content

Commit

Permalink
support for alternate units
Browse files Browse the repository at this point in the history
  • Loading branch information
S. Brent Faulkner committed Feb 22, 2010
1 parent 510beeb commit b20527b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
45 changes: 28 additions & 17 deletions lib/elapsed_time/numeric.rb
Expand Up @@ -6,34 +6,45 @@ module NumericMethods
# => "20 minutes and 34 seconds"
# 123456.to_elapsed_seconds
# => "1 day, 10 hours, 17 minutes and 36 seconds"
def to_elapsed_seconds
return '' if zero?
def to_elapsed_time(options = {})
unit = options[:unit] || :second
units = {:day => :day, :hour => :hour, :minute => :minute, :second => :second}
units.merge!(options[:units]) if options[:units]

return options[:include_zero] ? enumerate(unit, true) : '' if zero?

minutes,seconds = divmod(60)
hours,minutes = minutes.divmod(60)
days,hours = hours.divmod(24)
[ days.enumerate(:day), hours.enumerate(:hour), minutes.enumerate(:minute), seconds.enumerate(:second) ].compact.to_sentence :last_word_connector => I18n.translate('support.array.two_words_connector')
end

def to_elapsed_minutes
return '' if zero?
minutes.to_elapsed_seconds
[
days.enumerate(units[:day], options[:include_zero]),
hours.enumerate(units[:hour], options[:include_zero]),
minutes.enumerate(units[:minute], options[:include_zero]),
seconds.enumerate(units[:second], options[:include_zero])
].compact.to_sentence :last_word_connector => I18n.translate('support.array.two_words_connector')
end
alias_method :to_elapsed_seconds, :to_elapsed_time

def to_elapsed_hours
return '' if zero?
hours.to_elapsed_seconds
def to_elapsed_minutes(options = {})
minutes.to_elapsed_time(options.merge(:unit => :minute))
end

def to_elapsed_days
return '' if zero?
days.to_elapsed_seconds
def to_elapsed_hours(options = {})
hours.to_elapsed_time(options.merge(:unit => :hour))
end

# alias for legacy compatability
alias_method :to_elapsed_time, :to_elapsed_seconds
def to_elapsed_days(options = {})
days.to_elapsed_time(options.merge(:unit => :day))
end

def enumerate(type)
I18n.translate(type, :scope => 'datetime.units', :count => self) unless zero?
def enumerate(type, include_zero)
return if zero? && !include_zero
if type.is_a?(Symbol)
I18n.translate(type, :scope => 'datetime.units', :count => self)
else
"#{self} #{type}"
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions test/numeric_test.rb
Expand Up @@ -36,4 +36,24 @@ def test_zero_elapsed_hours
def test_zero_elapsed_days
assert_equal '', 0.to_elapsed_days
end

def test_to_elapsed_time_with_alternate_units
assert_equal '1 day, 10 hr, 17 min and 36 sec', 123456.to_elapsed_time(:units => {:day => 'day', :hour => 'hr', :minute => 'min', :second => 'sec'})
end

def test_zero_elapsed_seconds_including_zero
assert_equal '0 seconds', 0.to_elapsed_seconds(:include_zero => true)
end

def test_zero_elapsed_minutes_including_zero
assert_equal '0 minutes', 0.to_elapsed_minutes(:include_zero => true)
end

def test_zero_elapsed_hours_including_zero
assert_equal '0 hours', 0.to_elapsed_hours(:include_zero => true)
end

def test_zero_elapsed_days_including_zero
assert_equal '0 days', 0.to_elapsed_days(:include_zero => true)
end
end

0 comments on commit b20527b

Please sign in to comment.