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
Changes from 3 commits
1094acd
acaa085
19f29fa
154ef0f
f4fe2a3
6bc5d19
ceff74e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
|
@@ -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 | ||
|
@@ -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 | ||
# | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.