public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Refactored NumberHelper API to accept arguments as an options hash [#666 
state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
clemens (author)
Mon Jul 21 11:05:27 -0700 2008
josh (committer)
Mon Jul 21 11:05:27 -0700 2008
commit  0f43de644ea48c1ad11d4bc73307af066bb52870
tree    0aa332d702040889d37049783ebed196e51cab89
parent  ff9f6fcc75526d9fd89be834982dec8624c909c5
...
22
23
24
25
 
26
27
28
...
71
72
73
74
 
75
76
77
78
79
80
81
 
82
83
84
...
118
119
120
121
122
 
 
123
124
125
126
 
 
127
128
129
130
131
132
133
 
 
 
 
 
134
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
137
138
139
 
 
140
141
142
143
144
145
146
 
 
147
148
149
150
151
152
153
154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
156
157
158
159
160
 
161
162
163
 
164
165
166
...
169
170
171
 
 
 
 
 
172
173
174
175
 
 
 
 
 
 
 
 
 
176
177
178
179
180
181
182
 
 
 
 
183
184
185
...
22
23
24
 
25
26
27
28
...
71
72
73
 
74
75
76
77
78
79
80
 
81
82
83
84
...
118
119
120
 
 
121
122
123
124
 
 
125
126
127
128
 
 
 
 
 
129
130
131
132
133
134
 
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
 
151
152
153
154
155
156
157
 
 
158
159
160
161
 
 
 
 
 
 
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
 
183
184
185
 
186
187
188
189
...
192
193
194
195
196
197
198
199
200
 
 
 
201
202
203
204
205
206
207
208
209
210
211
212
 
 
 
 
213
214
215
216
217
218
219
0
@@ -22,7 +22,7 @@ module ActionView
0
       #  number_to_phone(1235551234, :country_code => 1)                    # => +1-123-555-1234
0
       #
0
       #  number_to_phone(1235551234, :country_code => 1, :extension => 1343, :delimiter => ".")
0
-      #  => +1.123.555.1234 x 1343      
0
+      #  => +1.123.555.1234 x 1343
0
       def number_to_phone(number, options = {})
0
         number       = number.to_s.strip unless number.nil?
0
         options      = options.stringify_keys
0
@@ -71,14 +71,14 @@ module ActionView
0
       def number_to_currency(number, options = {})
0
         options  = options.symbolize_keys
0
         defaults = I18n.translate(:'currency.format', :locale => options[:locale]) || {}
0
-                
0
+
0
         precision = options[:precision] || defaults[:precision]
0
         unit      = options[:unit]      || defaults[:unit]
0
         separator = options[:separator] || defaults[:separator]
0
         delimiter = options[:delimiter] || defaults[:delimiter]
0
         format    = options[:format]    || defaults[:format]
0
         separator = '' if precision == 0
0
-        
0
+
0
         begin
0
           parts = number_with_precision(number, precision).split('.')
0
           format.gsub(/%n/, number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s).gsub(/%u/, unit)
0
@@ -118,49 +118,72 @@ module ActionView
0
         end
0
       end
0
 
0
-      # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You
0
-      # can customize the format using optional <em>delimiter</em> and <em>separator</em> parameters.
0
+      # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
0
+      # customize the format in the +options+ hash.
0
       #
0
       # ==== Options
0
-      # * <tt>delimiter</tt>  - Sets the thousands delimiter (defaults to ",").
0
-      # * <tt>separator</tt>  - Sets the separator between the units (defaults to ".").
0
+      # * <tt>:delimiter</tt>  - Sets the thousands delimiter (defaults to ",").
0
+      # * <tt>:separator</tt>  - Sets the separator between the units (defaults to ".").
0
       #
0
       # ==== Examples
0
-      #  number_with_delimiter(12345678)       # => 12,345,678
0
-      #  number_with_delimiter(12345678.05)    # => 12,345,678.05
0
-      #  number_with_delimiter(12345678, ".")  # => 12.345.678
0
-      #
0
-      #  number_with_delimiter(98765432.98, " ", ",")
0
+      #  number_with_delimiter(12345678)                        # => 12,345,678
0
+      #  number_with_delimiter(12345678.05)                     # => 12,345,678.05
0
+      #  number_with_delimiter(12345678, :delimiter => ".")     # => 12.345.678
0
+      #  number_with_delimiter(12345678, :seperator => ",")     # => 12,345,678
0
+      #  number_with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
0
       #  # => 98 765 432,98
0
-      def number_with_delimiter(number, delimiter=",", separator=".")
0
+      #
0
+      # You can still use <tt>number_with_delimiter</tt> with the old API that accepts the
0
+      # +delimiter+ as its optional second and the +separator+ as its
0
+      # optional third parameter:
0
+      #  number_with_delimiter(12345678, " ")                   # => 12 345.678
0
+      #  number_with_delimiter(12345678.05, ".", ",")              # => 12.345.678,05
0
+      def number_with_delimiter(number, *args)
0
+        options = args.extract_options!
0
+        unless args.empty?
0
+          options[:delimiter] = args[0] || ","
0
+          options[:separator] = args[1] || "."
0
+        end
0
+        options.reverse_merge!(:delimiter => ",", :separator => ".")
0
+
0
         begin
0
           parts = number.to_s.split('.')
0
-          parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
0
-          parts.join separator
0
+          parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}")
0
+          parts.join options[:separator]
0
         rescue
0
           number
0
         end
0
       end
0
 
0
-      # Formats a +number+ with the specified level of +precision+ (e.g., 112.32 has a precision of 2). The default
0
-      # level of precision is 3.
0
+      # Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
0
+      # The default level of precision is 3.
0
       #
0
       # ==== Examples
0
-      #  number_with_precision(111.2345)     # => 111.235
0
-      #  number_with_precision(111.2345, 2)  # => 111.23
0
-      #  number_with_precision(13, 5)        # => 13.00000
0
-      #  number_with_precision(389.32314, 0) # => 389
0
-      def number_with_precision(number, precision=3)
0
-        "%01.#{precision}f" % ((Float(number) * (10 ** precision)).round.to_f / 10 ** precision)
0
+      #  number_with_precision(111.2345)                    # => 111.235
0
+      #  number_with_precision(111.2345, :precision => 2)   # => 111.23
0
+      #  number_with_precision(13, :precision => 5)         # => 13.00000
0
+      #  number_with_precision(389.32314, :precision => 0)  # => 389
0
+      #
0
+      # You can still use <tt>number_with_precision</tt> with the old API that accepts the
0
+      # +precision+ as its optional second parameter:
0
+      #   number_with_precision(number_with_precision(111.2345, 2)   # => 111.23
0
+      def number_with_precision(number, *args)
0
+        options = args.extract_options!
0
+        unless args.empty?
0
+          options[:precision] = args[0] || 3
0
+        end
0
+        options.reverse_merge!(:precision => 3)
0
+        "%01.#{options[:precision]}f" %
0
+          ((Float(number) * (10 ** options[:precision])).round.to_f / 10 ** options[:precision])
0
       rescue
0
         number
0
       end
0
 
0
       # Formats the bytes in +size+ into a more understandable representation
0
-      # (e.g., giving it 1500 yields 1.5 KB). This method is useful for 
0
+      # (e.g., giving it 1500 yields 1.5 KB). This method is useful for
0
       # reporting file sizes to users. This method returns nil if
0
       # +size+ cannot be converted into a number. You can change the default
0
-      # precision of 1 using the precision parameter +precision+.
0
+      # precision of 1 using the precision parameter <tt>:precision</tt>.
0
       #
0
       # ==== Examples
0
       #  number_to_human_size(123)           # => 123 Bytes
0
@@ -169,17 +192,28 @@ module ActionView
0
       #  number_to_human_size(1234567)       # => 1.2 MB
0
       #  number_to_human_size(1234567890)    # => 1.1 GB
0
       #  number_to_human_size(1234567890123) # => 1.1 TB
0
+      #  number_to_human_size(1234567, :precision => 2)    # => 1.18 MB
0
+      #  number_to_human_size(483989, :precision => 0)     # => 473 KB
0
+      #
0
+      # You can still use <tt>number_to_human_size</tt> with the old API that accepts the
0
+      # +precision+ as its optional second parameter:
0
       #  number_to_human_size(1234567, 2)    # => 1.18 MB
0
-      #  number_to_human_size(483989, 0)     # => 4 MB
0
-      def number_to_human_size(size, precision=1)
0
-        size = Kernel.Float(size)
0
+      #  number_to_human_size(483989, 0)     # => 473 KB
0
+      def number_to_human_size(size, *args)
0
+        options = args.extract_options!
0
+        unless args.empty?
0
+          options[:precision] = args[0] || 1
0
+        end
0
+        options.reverse_merge!(:precision => 1)
0
+
0
+        size = Float(size)
0
         case
0
           when size.to_i == 1;    "1 Byte"
0
           when size < 1.kilobyte; "%d Bytes" % size
0
-          when size < 1.megabyte; "%.#{precision}f KB"  % (size / 1.0.kilobyte)
0
-          when size < 1.gigabyte; "%.#{precision}f MB"  % (size / 1.0.megabyte)
0
-          when size < 1.terabyte; "%.#{precision}f GB"  % (size / 1.0.gigabyte)
0
-          else                    "%.#{precision}f TB"  % (size / 1.0.terabyte)
0
+          when size < 1.megabyte; "%.#{options[:precision]}f KB"  % (size / 1.0.kilobyte)
0
+          when size < 1.gigabyte; "%.#{options[:precision]}f MB"  % (size / 1.0.megabyte)
0
+          when size < 1.terabyte; "%.#{options[:precision]}f GB"  % (size / 1.0.gigabyte)
0
+          else                    "%.#{options[:precision]}f TB"  % (size / 1.0.terabyte)
0
         end.sub(/([0-9]\.\d*?)0+ /, '\1 ' ).sub(/\. /,' ')
0
       rescue
0
         nil
...
54
55
56
 
 
 
 
 
 
 
57
58
59
 
60
61
62
...
69
70
71
 
 
 
 
 
 
 
 
 
 
 
72
73
74
...
94
95
96
 
 
 
 
 
 
 
 
97
...
54
55
56
57
58
59
60
61
62
63
64
65
 
66
67
68
69
...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
112
113
114
115
116
117
118
119
120
121
122
123
0
@@ -54,9 +54,16 @@ class NumberHelperTest < ActionView::TestCase
0
     assert_nil number_with_delimiter(nil)
0
   end
0
 
0
+  def test_number_with_delimiter_with_options_hash
0
+    assert_equal '12 345 678', number_with_delimiter(12345678, :delimiter => ' ')
0
+    assert_equal '12,345,678-05', number_with_delimiter(12345678.05, :separator => '-')
0
+    assert_equal '12.345.678,05', number_with_delimiter(12345678.05, :separator => ',', :delimiter => '.')
0
+    assert_equal '12.345.678,05', number_with_delimiter(12345678.05, :delimiter => '.', :separator => ',')
0
+  end
0
+
0
   def test_number_with_precision
0
     assert_equal("111.235", number_with_precision(111.2346))
0
-    assert_equal("31.83", number_with_precision(31.825, 2))    
0
+    assert_equal("31.83", number_with_precision(31.825, 2))
0
     assert_equal("111.23", number_with_precision(111.2346, 2))
0
     assert_equal("111.00", number_with_precision(111, 2))
0
     assert_equal("111.235", number_with_precision("111.2346"))
0
@@ -69,6 +76,17 @@ class NumberHelperTest < ActionView::TestCase
0
     assert_nil number_with_precision(nil)
0
   end
0
 
0
+  def test_number_with_precision_with_options_hash
0
+    assert_equal '111.235',     number_with_precision(111.2346)
0
+    assert_equal '31.83',       number_with_precision(31.825, :precision => 2)
0
+    assert_equal '111.23',      number_with_precision(111.2346, :precision => 2)
0
+    assert_equal '111.00',      number_with_precision(111, :precision => 2)
0
+    assert_equal '111.235',     number_with_precision("111.2346")
0
+    assert_equal '31.83',       number_with_precision("31.825", :precision => 2)
0
+    assert_equal '112',         number_with_precision(111.50, :precision => 0)
0
+    assert_equal '1234567892',  number_with_precision(1234567891.50, :precision => 0)
0
+  end
0
+
0
   def test_number_to_human_size
0
     assert_equal '0 Bytes',   number_to_human_size(0)
0
     assert_equal '1 Byte',    number_to_human_size(1)
0
@@ -94,4 +112,12 @@ class NumberHelperTest < ActionView::TestCase
0
     assert_nil number_to_human_size('x')
0
     assert_nil number_to_human_size(nil)
0
   end
0
+
0
+  def test_number_to_human_size_with_options_hash
0
+    assert_equal '1.18 MB',   number_to_human_size(1234567, :precision => 2)
0
+    assert_equal '3 Bytes',   number_to_human_size(3.14159265, :precision => 4)
0
+    assert_equal '1.01 KB',   number_to_human_size(1.0123.kilobytes, :precision => 2)
0
+    assert_equal '1.01 KB',   number_to_human_size(1.0100.kilobytes, :precision => 4)
0
+    assert_equal '10 KB',     number_to_human_size(10.000.kilobytes, :precision => 4)
0
+  end
0
 end

Comments