Permalink
Browse files

- fixed bug with total seconds where, e.g. 61 seconds would display a…

…s 61 second

- now displays correct singular/plural for total minutes
  • Loading branch information...
1 parent b766e0d commit 8a11a1291c6099acd1c5065db04bac183c72a223 @gdavison gdavison committed Mar 24, 2012
Showing with 37 additions and 8 deletions.
  1. +23 −6 lib/duration.rb
  2. +14 −2 test/test_duration.rb
View
@@ -71,6 +71,10 @@ def /(other)
def %(other)
Duration.new(@total % other.to_i)
end
+
+ def total_minutes
+ @total / 60
+ end
# Formats a duration in ISO8601.
# @see http://en.wikipedia.org/wiki/ISO_8601#Durations
@@ -145,7 +149,7 @@ def format(format_str)
'h' => @hours,
'm' => @minutes,
's' => @seconds,
- 'tm' => @total / 60,
+ 'tm' => Proc.new { total_minutes },
't' => @total,
'H' => @hours.to_s.rjust(2, '0'),
'M' => @minutes.to_s.rjust(2, '0'),
@@ -154,11 +158,13 @@ def format(format_str)
'~m' => i18n_for(:minute),
'~h' => i18n_for(:hour),
'~d' => i18n_for(:day),
- '~w' => i18n_for(:week)
+ '~w' => i18n_for(:week),
+ '~ts'=> Proc.new { i18n_for(:total) },
+ '~tm'=> Proc.new { i18n_for(:total_minute) }
}
- format_str.gsub(/%?%(w|d|h|m|s|tm?|H|M|S|~(?:s|m|h|d|w))/) do |match|
- match['%%'] ? match : identifiers[match[1..-1]]
+ format_str.gsub(/%?%(w|d|h|m|s|tm?|H|M|S|~(?:s|m|h|d|w|ts|tm))/) do |match|
+ match['%%'] ? match : (identifiers[match[1..-1]].class == Proc ? identifiers[match[1..-1]].call : identifiers[match[1..-1]])
end.gsub('%%', '%')
end
@@ -185,8 +191,19 @@ def calculate!
end
def i18n_for(singular)
- plural = "#{singular}s"
- label = send(plural) == 1 ? singular : plural
+ if singular == :total
+ fn_name = :total
+ singular = :second
+ plural = 'seconds'
+ elsif singular.to_s.start_with?('total_')
+ fn_name = "#{singular}s"
+ singular = singular.to_s['total_'.length..-1]
+ plural = "#{singular}s"
+ else
+ plural = "#{singular}s"
+ fn_name = plural
+ end
+ label = send(fn_name) == 1 ? singular : plural
I18n.t(label, :scope => :ruby_duration, :default => label.to_s)
end
View
@@ -10,6 +10,7 @@
assert_equal 1, d.minutes
assert_equal 30, d.seconds
assert_equal 90, d.total
+ assert_equal 1, d.total_minutes
end
it "should initialize given duration in Hash" do
@@ -20,6 +21,7 @@
assert_equal 4, d.minutes
assert_equal 5, d.seconds
assert_equal 788645, d.total
+ assert_equal 13144, d.total_minutes
end
describe "mathematical operations" do
@@ -63,12 +65,22 @@
it "should display total seconds" do
d = Duration.new(:hours => 1, :minutes => 15)
- assert_equal "4500 seconds", d.format("%t %~s")
+ assert_equal "4500 seconds", d.format("%t %~ts")
+ end
+
+ it "should display total seconds in plural form when needed" do
+ d = Duration.new(:minutes => 1, :seconds => 1)
+ assert_equal "61 seconds", d.format("%t %~ts")
end
it "should display total minutes" do
d = Duration.new(:hours => 1, :minutes => 15)
- assert_equal "75 minutes", d.format("%tm %~m")
+ assert_equal "75 minutes", d.format("%tm %~tm")
+ end
+
+ it "should display total minutes in plural form when needed" do
+ d = Duration.new(:hours => 1, :minutes => 1)
+ assert_equal "61 minutes", d.format("%tm %~tm")
end
end

0 comments on commit 8a11a12

Please sign in to comment.