Skip to content

Add total times #2

Merged
merged 5 commits into from Apr 10, 2012
View
31 Gemfile.lock
@@ -1,31 +0,0 @@
-PATH
- remote: .
- specs:
- ruby-duration (2.0.1)
- activesupport (>= 3.0.0)
- i18n
-
-GEM
- remote: http://rubygems.org/
- specs:
- activesupport (3.0.9)
- bluecloth (2.0.9)
- i18n (0.5.0)
- minitest (1.7.2)
- rake (0.8.7)
- simplecov (0.3.6)
- simplecov-html (>= 0.3.7)
- simplecov-html (0.3.8)
- yard (0.6.1)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- bluecloth (>= 0.3.5)
- bundler (>= 1.0.0)
- minitest
- rake
- ruby-duration!
- simplecov (>= 0.3.5)
- yard
View
69 lib/duration.rb
@@ -71,6 +71,10 @@ def /(other)
def %(other)
Duration.new(@total % other.to_i)
end
+
+ %w(minutes hours days).each do |meth|
+ define_method("total_#{meth}") { @total / MULTIPLES[meth.to_sym] }
+ end
# Formats a duration in ISO8601.
# @see http://en.wikipedia.org/wiki/ISO_8601#Durations
@@ -101,20 +105,28 @@ def present?
# Format a duration into a human-readable string.
#
- # %w => weeks
- # %d => days
- # %h => hours
- # %m => minutes
- # %s => seconds
- # %t => total seconds
- # %H => zero-padded hours
- # %M => zero-padded minutes
- # %S => zero-padded seconds
- # %~s => locale-dependent "seconds" terminology
- # %~m => locale-dependent "minutes" terminology
- # %~h => locale-dependent "hours" terminology
- # %~d => locale-dependent "days" terminology
- # %~w => locale-dependent "weeks" terminology
+ # %w => weeks
+ # %d => days
+ # %h => hours
+ # %m => minutes
+ # %s => seconds
+ # %td => total days
+ # %th => total hours
+ # %tm => total minutes
+ # %ts => total seconds
+ # %t => total seconds
+ # %H => zero-padded hours
+ # %M => zero-padded minutes
+ # %S => zero-padded seconds
+ # %~s => locale-dependent "seconds" terminology
+ # %~m => locale-dependent "minutes" terminology
+ # %~h => locale-dependent "hours" terminology
+ # %~d => locale-dependent "days" terminology
+ # %~w => locale-dependent "weeks" terminology
+ # %tdu => total days with locale-dependent unit
+ # %thu => total hours with locale-dependent unit
+ # %tmu => total minutes with locale-dependent unit
+ # %tsu => total seconds with locale-dependent unit
#
# You can also use the I18n support.
# The %~s, %~m, %~h, %~d and %~w can be translated with I18n.
@@ -144,6 +156,10 @@ def format(format_str)
'h' => @hours,
'm' => @minutes,
's' => @seconds,
+ 'td' => Proc.new { total_days },
+ 'th' => Proc.new { total_hours },
+ 'tm' => Proc.new { total_minutes },
+ 'ts' => @total,
't' => @total,
'H' => @hours.to_s.rjust(2, '0'),
'M' => @minutes.to_s.rjust(2, '0'),
@@ -152,11 +168,15 @@ def format(format_str)
'~m' => i18n_for(:minute),
'~h' => i18n_for(:hour),
'~d' => i18n_for(:day),
- '~w' => i18n_for(:week)
+ '~w' => i18n_for(:week),
+ 'tdu'=> Proc.new { "#{total_days} #{i18n_for(:total_day)}"},
+ 'thu'=> Proc.new { "#{total_hours} #{i18n_for(:total_hour)}"},
+ 'tmu'=> Proc.new { "#{total_minutes} #{i18n_for(:total_minute)}"},
+ 'tsu'=> Proc.new { "#{total} #{i18n_for(:total)}"}
}
- format_str.gsub(/%?%(w|d|h|m|s|t|H|M|S|~(?:s|m|h|d|w))/) do |match|
- match['%%'] ? match : identifiers[match[1..-1]]
+ format_str.gsub(/%?%(w|d|h|m|s|t([dhms]u?)?|H|M|S|~(?:s|m|h|d|w))/) do |match|
+ match['%%'] ? match : (identifiers[match[1..-1]].class == Proc ? identifiers[match[1..-1]].call : identifiers[match[1..-1]])
end.gsub('%%', '%')
end
@@ -183,8 +203,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
79 test/test_duration.rb
@@ -10,16 +10,22 @@
assert_equal 1, d.minutes
assert_equal 30, d.seconds
assert_equal 90, d.total
+ assert_equal 1, d.total_minutes
+ assert_equal 0, d.total_hours
+ assert_equal 0, d.total_days
end
it "should initialize given duration in Hash" do
d = Duration.new(:weeks => 1, :days => 2, :hours => 3, :minutes => 4, :seconds => 5)
- assert_equal 1, d.weeks
- assert_equal 2, d.days
- assert_equal 3, d.hours
- assert_equal 4, d.minutes
- assert_equal 5, d.seconds
+ assert_equal 1, d.weeks
+ assert_equal 2, d.days
+ assert_equal 3, d.hours
+ assert_equal 4, d.minutes
+ assert_equal 5, d.seconds
assert_equal 788645, d.total
+ assert_equal 13144, d.total_minutes
+ assert_equal 219, d.total_hours
+ assert_equal 9, d.total_days
end
describe "mathematical operations" do
@@ -29,22 +35,22 @@
assert_equal Duration.new(15), Duration.new(10) + Duration.new(5)
end
- it "sould -" do
+ it "should -" do
assert_equal Duration.new(5), Duration.new(10) - 5
assert_equal Duration.new(5), Duration.new(10) - Duration.new(5)
end
- it "sould *" do
+ it "should *" do
assert_equal Duration.new(20), Duration.new(10) * 2
assert_equal Duration.new(20), Duration.new(10) * Duration.new(2)
end
- it "sould /" do
+ it "should /" do
assert_equal Duration.new(5), Duration.new(10) / 2
assert_equal Duration.new(5), Duration.new(10) / Duration.new(2)
end
- it "sould %" do
+ it "should %" do
assert_equal Duration.new(1), Duration.new(10) % 3
assert_equal Duration.new(1), Duration.new(10) % Duration.new(3)
end
@@ -60,6 +66,61 @@
d = Duration.new(:weeks => 1, :days => 1, :hours => 1, :minutes => 1, :seconds => 1)
assert_equal "1 week 1 day 1 hour 1 minute 1 second", d.format("%w %~w %d %~d %h %~h %m %~m %s %~s")
end
+
+ it "should display total seconds" do
+ d = Duration.new(:hours => 1, :minutes => 15)
+ assert_equal "4500 seconds", d.format("%tsu")
+ 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("%tsu")
+ end
+
+ it "should display total minutes as number" do
+ d = Duration.new(:hours => 1, :minutes => 15)
+ assert_equal "75", d.format("%tm")
+ end
+
+ it 'should display total minutes with unit' do
+ d = Duration.new(:hours => 1, :minutes => 15)
+ assert_equal "75 minutes", d.format("%tmu")
+ 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("%tmu")
+ end
+
+ it "should display total hours as number" do
+ d = Duration.new(:days => 2, :hours => 1)
+ assert_equal "49", d.format("%th")
+ end
+
+ it 'should display total hours with unit' do
+ d = Duration.new(:days => 2, :hours => 2)
+ assert_equal "50 hours", d.format("%thu")
+ end
+
+ it "should display total hours in plural form when needed" do
+ d = Duration.new(:days => 1, :hours => 1)
+ assert_equal "25 hours", d.format("%thu")
+ end
+
+ it "should display total days as number" do
+ d = Duration.new(:weeks => 1, :days => 3)
+ assert_equal "10", d.format("%td")
+ end
+
+ it 'should display total days with unit' do
+ d = Duration.new(:weeks => 1, :days => 2)
+ assert_equal "9 days", d.format("%tdu")
+ end
+
+ it "should display total days in plural form when needed" do
+ d = Duration.new(:weeks => 1, :days => 1)
+ assert_equal "8 days", d.format("%tdu")
+ end
end
describe "#iso_6801" do
Something went wrong with that request. Please try again.