Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added :prefix => :iec option #7835

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions actionpack/test/template/number_helper_test.rb
Expand Up @@ -179,13 +179,24 @@ def test_number_to_human_size_with_si_prefix
assert_equal '3 Bytes', number_to_human_size(3.14159265, :prefix => :si)
assert_equal '123 Bytes', number_to_human_size(123.0, :prefix => :si)
assert_equal '123 Bytes', number_to_human_size(123, :prefix => :si)
assert_equal '1.23 KB', number_to_human_size(1234, :prefix => :si)
assert_equal '12.3 KB', number_to_human_size(12345, :prefix => :si)
assert_equal '1.23 kB', number_to_human_size(1234, :prefix => :si)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use 1.9 hash syntax, thanks :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on 1.9 hash syntax.

assert_equal '12.3 kB', number_to_human_size(12345, :prefix => :si)
assert_equal '1.23 MB', number_to_human_size(1234567, :prefix => :si)
assert_equal '1.23 GB', number_to_human_size(1234567890, :prefix => :si)
assert_equal '1.23 TB', number_to_human_size(1234567890123, :prefix => :si)
end

def test_number_to_human_size_with_iec_prefix
assert_equal '3 Bytes', number_to_human_size(3.14159265, :prefix => :iec)
assert_equal '123 Bytes', number_to_human_size(123.0, :prefix => :iec)
assert_equal '123 Bytes', number_to_human_size(123, :prefix => :iec)
assert_equal '1.21 KiB', number_to_human_size(1234, :prefix => :iec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the indentation

assert_equal '12.1 KiB', number_to_human_size(12345, :prefix => :iec)
assert_equal '1.18 MiB', number_to_human_size(1234567, :prefix => :iec)
assert_equal '1.15 GiB', number_to_human_size(1234567890, :prefix => :iec)
assert_equal '1.12 TiB', number_to_human_size(1234567890123, :prefix => :iec)
end

def test_number_to_human_size_with_options_hash
assert_equal '1.2 MB', number_to_human_size(1234567, :precision => 2)
assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4)
Expand Down
16 changes: 16 additions & 0 deletions activesupport/lib/active_support/locale/en.yml
Expand Up @@ -106,6 +106,22 @@ en:
mb: "MB"
gb: "GB"
tb: "TB"
si_units:
byte:
one: "Byte"
other: "Bytes"
kb: "kB"
mb: "MB"
gb: "GB"
tb: "TB"
iec_units:
byte:
one: "Byte"
other: "Bytes"
kb: "KiB"
mb: "MiB"
gb: "GiB"
tb: "TiB"
# Used in NumberHelper.number_to_human()
decimal_units:
format: "%n %u"
Expand Down
37 changes: 33 additions & 4 deletions activesupport/lib/active_support/number_helper.rb
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you added this?

require 'active_support/core_ext/big_decimal/conversions'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/hash/keys'
Expand Down Expand Up @@ -74,6 +75,20 @@ module NumberHelper
mb: "MB",
gb: "GB",
tb: "TB"
},
iec_units: {
byte: "Bytes",
kb: "KiB",
mb: "MiB",
gb: "GiB",
tb: "TiB"
},
si_units: {
byte: "Bytes",
kb: "kB",
mb: "MB",
gb: "GB",
tb: "TB"
}
},
# Used in number_to_human
Expand Down Expand Up @@ -399,7 +414,8 @@ def number_to_rounded(number, options = {})
# insignificant zeros after the decimal separator (defaults to
# +true+)
# * <tt>:prefix</tt> - If +:si+ formats the number using the SI
# prefix (defaults to :binary)
# prefix. If +:iec+ formats the numbers using the IEC prefix
# (defaults to :customary).
#
# ==== Examples
#
Expand Down Expand Up @@ -432,10 +448,23 @@ def number_to_human_size(number, options = {})

storage_units_format = translate_number_value_with_default('human.storage_units.format', :locale => options[:locale], :raise => true)

base = options[:prefix] == :si ? 1000 : 1024
options[:prefix] ||= :customary
case options[:prefix]
when :si
base = 1000
system_key = "si_units"
when :iec
base = 1024
system_key = "iec_units"
when :customary
base = 1024
system_key = "units"
else
raise ArgumentError, ":prefix must be :customary, :si, or :iec"
end

if number.to_i < base
unit = translate_number_value_with_default('human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true)
unit = translate_number_value_with_default("human.storage_units.#{system_key}.byte", :locale => options[:locale], :count => number.to_i, :raise => true)
storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit)
else
max_exp = STORAGE_UNITS.size - 1
Expand All @@ -444,7 +473,7 @@ def number_to_human_size(number, options = {})
number /= base ** exponent

unit_key = STORAGE_UNITS[exponent]
unit = translate_number_value_with_default("human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true)
unit = translate_number_value_with_default("human.storage_units.#{system_key}.#{unit_key}", :locale => options[:locale], :count => number, :raise => true)

formatted_number = self.number_to_rounded(number, options)
storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
Expand Down
15 changes: 13 additions & 2 deletions activesupport/test/core_ext/numeric_ext_test.rb
Expand Up @@ -344,13 +344,24 @@ def test_to_s__human_size_with_si_prefix
assert_equal '3 Bytes', 3.14159265.to_s(:human_size, :prefix => :si)
assert_equal '123 Bytes', 123.0.to_s(:human_size, :prefix => :si)
assert_equal '123 Bytes', 123.to_s(:human_size, :prefix => :si)
assert_equal '1.23 KB', 1234.to_s(:human_size, :prefix => :si)
assert_equal '12.3 KB', 12345.to_s(:human_size, :prefix => :si)
assert_equal '1.23 kB', 1234.to_s(:human_size, :prefix => :si)
assert_equal '12.3 kB', 12345.to_s(:human_size, :prefix => :si)
assert_equal '1.23 MB', 1234567.to_s(:human_size, :prefix => :si)
assert_equal '1.23 GB', 1234567890.to_s(:human_size, :prefix => :si)
assert_equal '1.23 TB', 1234567890123.to_s(:human_size, :prefix => :si)
end

def test_to_s__human_size_with_iec_prefix
assert_equal '3 Bytes', 3.14159265.to_s(:human_size, :prefix => :iec)
assert_equal '123 Bytes', 123.0.to_s(:human_size, :prefix => :iec)
assert_equal '123 Bytes', 123.to_s(:human_size, :prefix => :iec)
assert_equal '1.21 KiB', 1234.to_s(:human_size, :prefix => :iec)
assert_equal '12.1 KiB', 12345.to_s(:human_size, :prefix => :iec)
assert_equal '1.18 MiB', 1234567.to_s(:human_size, :prefix => :iec)
assert_equal '1.15 GiB', 1234567890.to_s(:human_size, :prefix => :iec)
assert_equal '1.12 TiB', 1234567890123.to_s(:human_size, :prefix => :iec)
end

def test_to_s__human_size_with_options_hash
assert_equal '1.2 MB', 1234567.to_s(:human_size, :precision => 2)
assert_equal '3 Bytes', 3.14159265.to_s(:human_size, :precision => 4)
Expand Down
25 changes: 23 additions & 2 deletions activesupport/test/number_helper_test.rb
Expand Up @@ -209,14 +209,35 @@ def test_number_to_human_size_with_si_prefix
assert_equal '3 Bytes', number_helper.number_to_human_size(3.14159265, :prefix => :si)
assert_equal '123 Bytes', number_helper.number_to_human_size(123.0, :prefix => :si)
assert_equal '123 Bytes', number_helper.number_to_human_size(123, :prefix => :si)
assert_equal '1.23 KB', number_helper.number_to_human_size(1234, :prefix => :si)
assert_equal '12.3 KB', number_helper.number_to_human_size(12345, :prefix => :si)
assert_equal '1.23 kB', number_helper.number_to_human_size(1234, :prefix => :si)
assert_equal '12.3 kB', number_helper.number_to_human_size(12345, :prefix => :si)
assert_equal '1.23 MB', number_helper.number_to_human_size(1234567, :prefix => :si)
assert_equal '1.23 GB', number_helper.number_to_human_size(1234567890, :prefix => :si)
assert_equal '1.23 TB', number_helper.number_to_human_size(1234567890123, :prefix => :si)
end
end

def test_number_to_human_size_with_iec_prefix
[@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper|
assert_equal '3 Bytes', number_helper.number_to_human_size(3.14159265, :prefix => :iec)
assert_equal '123 Bytes', number_helper.number_to_human_size(123.0, :prefix => :iec)
assert_equal '123 Bytes', number_helper.number_to_human_size(123, :prefix => :iec)
assert_equal '1.21 KiB', number_helper.number_to_human_size(1234, :prefix => :iec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the indentation

assert_equal '12.1 KiB', number_helper.number_to_human_size(12345, :prefix => :iec)
assert_equal '1.18 MiB', number_helper.number_to_human_size(1234567, :prefix => :iec)
assert_equal '1.15 GiB', number_helper.number_to_human_size(1234567890, :prefix => :iec)
assert_equal '1.12 TiB', number_helper.number_to_human_size(1234567890123, :prefix => :iec)
end
end

def test_number_to_human_size_with_invalid_prefix
[@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper|
assert_raise ArgumentError do
number_helper.number_to_human_size(1, :prefix => :unknown_prefix)
end
end
end

def test_number_to_human_size_with_options_hash
[@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper|
assert_equal '1.2 MB', number_helper.number_to_human_size(1234567, :precision => 2)
Expand Down