Class: ActiveSupport::NumberHelper::NumberToHumanSizeConverter
Do not use. This class is for internal use only.
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
NumberConverter
|
|
Instance Chain:
self,
NumberConverter
|
|
Inherits: |
ActiveSupport::NumberHelper::NumberConverter
|
Defined in: | activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb |
Constant Summary
-
STORAGE_UNITS =
# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 8[:byte, :kb, :mb, :gb, :tb, :pb, :eb, :zb]
NumberConverter
- Inherited
Class Attribute Summary
NumberConverter
- Inherited
Class Method Summary
NumberConverter
- Inherited
Instance Attribute Summary
- #smaller_than_base? ⇒ Boolean readonly private
NumberConverter
- Inherited
Instance Method Summary
- #convert
- #base private
- #conversion_format private
- #exponent private
- #storage_unit_key private
- #unit private
NumberConverter
- Inherited
Constructor Details
This class inherits a constructor from ActiveSupport::NumberHelper::NumberConverter
Instance Attribute Details
#smaller_than_base? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 51
def smaller_than_base? number.to_i.abs < base end
Instance Method Details
#base (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 55
def base 1024 end
#conversion_format (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 31
def conversion_format translate_number_value_with_default("human.storage_units.format", locale: [:locale], raise: true) end
#convert
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 13
def convert @number = Float(number) # For backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files. unless .key?(:strip_insignificant_zeros) [:strip_insignificant_zeros] = true end if smaller_than_base? number_to_format = number.to_i.to_s else human_size = number / (base**exponent) number_to_format = NumberToRoundedConverter.convert(human_size, ) end conversion_format.gsub("%n", number_to_format).gsub("%u", unit) end
#exponent (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 44
def exponent max = STORAGE_UNITS.size - 1 exp = (Math.log(number.abs) / Math.log(base)).to_i exp = max if exp > max # avoid overflow for the highest unit exp end
#storage_unit_key (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 39
def storage_unit_key key_end = smaller_than_base? ? "byte" : STORAGE_UNITS[exponent] "human.storage_units.units.#{key_end}" end
#unit (private)
[ GitHub ]# File 'activesupport/lib/active_support/number_helper/number_to_human_size_converter.rb', line 35
def unit translate_number_value_with_default(storage_unit_key, locale: [:locale], count: number.to_i, raise: true) end