Sha256: dedff014fd171ca90a3773bb5026a01d5a4fabaeb1054b83e62c9dcefcc69697

Contents?: true

Size: 1.9 KB

Versions: 50

Compression:

Stored size: 1.9 KB

Contents

module ActiveSupport
  module NumberHelper
    class NumberToHumanSizeConverter < NumberConverter #:nodoc:
      STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb, :pb, :eb]

      self.namespace      = :human
      self.validate_float = true

      def convert
        if opts.key?(:prefix)
          ActiveSupport::Deprecation.warn('The :prefix option of `number_to_human_size` is deprecated and will be removed in Rails 5.1 with no replacement.')
        end

        @number = Float(number)

        # for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
        unless options.key?(:strip_insignificant_zeros)
          options[: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, options)
        end
        conversion_format.gsub('%n'.freeze, number_to_format).gsub('%u'.freeze, unit)
      end

      private

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

        def unit
          translate_number_value_with_default(storage_unit_key, :locale => options[:locale], :count => number.to_i, :raise => true)
        end

        def storage_unit_key
          key_end = smaller_than_base? ? 'byte' : STORAGE_UNITS[exponent]
          "human.storage_units.units.#{key_end}"
        end

        def exponent
          max = STORAGE_UNITS.size - 1
          exp = (Math.log(number) / Math.log(base)).to_i
          exp = max if exp > max # avoid overflow for the highest unit
          exp
        end

        def smaller_than_base?
          number.to_i < base
        end

        def base
          opts[:prefix] == :si ? 1000 : 1024
        end
    end
  end
end

Version data entries

50 entries across 48 versions & 8 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/activesupport-5.0.7.1/lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.7.2 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.7.1 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.7 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.6 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.6.rc1 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.5 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.5.rc2 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.5.rc1 lib/active_support/number_helper/number_to_human_size_converter.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.4 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.4.rc1 lib/active_support/number_helper/number_to_human_size_converter.rb
activesupport-5.0.3 lib/active_support/number_helper/number_to_human_size_converter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb
lazy_record-0.2.1 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb
lazy_record-0.2.0 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb
lazy_record-0.1.9 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb
lazy_record-0.1.8 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb
lazy_record-0.1.7 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_human_size_converter.rb