Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- 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...
commit 8a11a1291c6099acd1c5065db04bac183c72a223 1 parent b766e0d
Graham Davison authored March 23, 2012
29  lib/duration.rb
@@ -71,6 +71,10 @@ def /(other)
71 71
   def %(other)
72 72
     Duration.new(@total % other.to_i)
73 73
   end
  74
+  
  75
+  def total_minutes
  76
+    @total / 60
  77
+  end
74 78
 
75 79
   # Formats a duration in ISO8601.
76 80
   # @see http://en.wikipedia.org/wiki/ISO_8601#Durations
@@ -145,7 +149,7 @@ def format(format_str)
145 149
       'h'  => @hours,
146 150
       'm'  => @minutes,
147 151
       's'  => @seconds,
148  
-      'tm' => @total / 60,
  152
+      'tm' => Proc.new { total_minutes },
149 153
       't'  => @total,
150 154
       'H'  => @hours.to_s.rjust(2, '0'),
151 155
       'M'  => @minutes.to_s.rjust(2, '0'),
@@ -154,11 +158,13 @@ def format(format_str)
154 158
       '~m' => i18n_for(:minute),
155 159
       '~h' => i18n_for(:hour),
156 160
       '~d' => i18n_for(:day),
157  
-      '~w' => i18n_for(:week)
  161
+      '~w' => i18n_for(:week),
  162
+      '~ts'=> Proc.new { i18n_for(:total) },
  163
+      '~tm'=> Proc.new { i18n_for(:total_minute) }
158 164
     }
159 165
 
160  
-    format_str.gsub(/%?%(w|d|h|m|s|tm?|H|M|S|~(?:s|m|h|d|w))/) do |match|
161  
-      match['%%'] ? match : identifiers[match[1..-1]]
  166
+    format_str.gsub(/%?%(w|d|h|m|s|tm?|H|M|S|~(?:s|m|h|d|w|ts|tm))/) do |match|
  167
+      match['%%'] ? match : (identifiers[match[1..-1]].class == Proc ? identifiers[match[1..-1]].call : identifiers[match[1..-1]])
162 168
     end.gsub('%%', '%')
163 169
   end
164 170
 
@@ -185,8 +191,19 @@ def calculate!
185 191
   end
186 192
 
187 193
   def i18n_for(singular)
188  
-    plural = "#{singular}s"
189  
-    label = send(plural) == 1 ? singular : plural
  194
+    if singular == :total
  195
+      fn_name = :total
  196
+      singular = :second
  197
+      plural = 'seconds'
  198
+    elsif singular.to_s.start_with?('total_')
  199
+      fn_name = "#{singular}s"
  200
+      singular = singular.to_s['total_'.length..-1]
  201
+      plural = "#{singular}s"
  202
+    else
  203
+      plural = "#{singular}s"
  204
+      fn_name = plural
  205
+    end
  206
+    label = send(fn_name) == 1 ? singular : plural
190 207
 
191 208
     I18n.t(label, :scope => :ruby_duration, :default => label.to_s)
192 209
   end
16  test/test_duration.rb
@@ -10,6 +10,7 @@
10 10
     assert_equal 1, d.minutes
11 11
     assert_equal 30, d.seconds
12 12
     assert_equal 90, d.total
  13
+    assert_equal 1, d.total_minutes
13 14
   end
14 15
 
15 16
   it "should initialize given duration in Hash" do
@@ -20,6 +21,7 @@
20 21
     assert_equal 4, d.minutes
21 22
     assert_equal 5, d.seconds
22 23
     assert_equal 788645, d.total
  24
+    assert_equal 13144, d.total_minutes
23 25
   end
24 26
   
25 27
   describe "mathematical operations" do
@@ -63,12 +65,22 @@
63 65
     
64 66
     it "should display total seconds" do
65 67
       d = Duration.new(:hours => 1, :minutes => 15)
66  
-      assert_equal "4500 seconds", d.format("%t %~s")
  68
+      assert_equal "4500 seconds", d.format("%t %~ts")
  69
+    end
  70
+    
  71
+    it "should display total seconds in plural form when needed" do
  72
+      d = Duration.new(:minutes => 1, :seconds => 1)
  73
+      assert_equal "61 seconds", d.format("%t %~ts")
67 74
     end
68 75
     
69 76
     it "should display total minutes" do
70 77
       d = Duration.new(:hours => 1, :minutes => 15)
71  
-      assert_equal "75 minutes", d.format("%tm %~m")
  78
+      assert_equal "75 minutes", d.format("%tm %~tm")
  79
+    end
  80
+    
  81
+    it "should display total minutes in plural form when needed" do
  82
+      d = Duration.new(:hours => 1, :minutes => 1)
  83
+      assert_equal "61 minutes", d.format("%tm %~tm")
72 84
     end
73 85
   end
74 86
   

0 notes on commit 8a11a12

Please sign in to comment.
Something went wrong with that request. Please try again.