Sha256: 228c3c7cd830a906f3a9216d51a44f59060ef8e470f99fdf6fc4da40fd2bdf47

Contents?: true

Size: 1.98 KB

Versions: 26

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Facter
  module Util
    module Facts
      class UnitConverter
        class << self
          def bytes_to_mb(value_in_bytes)
            return unless value_in_bytes

            value_in_bytes = value_in_bytes.to_i

            (value_in_bytes / (1024.0 * 1024.0))
          end

          def hertz_to_human_readable(speed)
            speed = speed.to_i
            return if !speed || speed.zero?

            validated_speed, metric_prefix = determine_metric_prefix(speed)

            format('%<displayed_speed>.2f', displayed_speed: validated_speed.round(2)).to_s + ' ' + metric_prefix + 'Hz'
          end

          def bytes_to_human_readable(bytes)
            return unless bytes
            return bytes.to_s + ' bytes' if bytes < 1024

            number, multiple = determine_exponent(bytes)

            "#{pad_number(number)} #{multiple}"
          end

          private

          def pad_number(number)
            number = number.to_s
            number << '0' if number.split('.').last.length == 1
            number
          end

          def determine_exponent(bytes)
            prefix = %w[KiB MiB GiB TiB PiB EiB]
            exp = (Math.log2(bytes) / 10.0).floor
            converted_number = (100.0 * (bytes / 1024.0**exp)).round / 100.0

            if (converted_number - 1024.0).abs < Float::EPSILON
              exp += 1
              converted_number = 1.00
            end
            multiple = prefix[exp - 1] || 'bytes'

            converted_number = bytes if multiple == 'bytes'
            [converted_number, multiple]
          end

          def determine_metric_prefix(num)
            metric_prefix = { 0 => '', 3 => 'k', 6 => 'M', 9 => 'G', 12 => 'T' }
            power = Math.log10(num).floor
            display_exponent = power - power % 3
            coefficient = power.zero? ? num.to_f : num.fdiv(10**display_exponent)
            [coefficient, metric_prefix[display_exponent]]
          end
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facter-4.5.2 lib/facter/util/facts/unit_converter.rb
facter-4.5.1 lib/facter/util/facts/unit_converter.rb
facter-4.5.0 lib/facter/util/facts/unit_converter.rb
facter-4.4.3 lib/facter/util/facts/unit_converter.rb
facter-4.4.2 lib/facter/util/facts/unit_converter.rb
facter-4.4.1 lib/facter/util/facts/unit_converter.rb
facter-4.4.0 lib/facter/util/facts/unit_converter.rb
facter-4.3.1 lib/facter/util/facts/unit_converter.rb
facter-4.3.0 lib/facter/util/facts/unit_converter.rb
facter-4.2.14 lib/facter/util/facts/unit_converter.rb
facter-4.2.13 lib/facter/util/facts/unit_converter.rb
facter-4.2.12 lib/facter/util/facts/unit_converter.rb
facter-4.2.11 lib/facter/util/facts/unit_converter.rb
facter-4.2.10 lib/facter/util/facts/unit_converter.rb
facter-4.2.9 lib/facter/util/facts/unit_converter.rb
facter-4.2.8 lib/facter/util/facts/unit_converter.rb
facter-4.2.7 lib/facter/util/facts/unit_converter.rb
facter-4.2.6 lib/facter/util/facts/unit_converter.rb
facter-4.2.5 lib/facter/util/facts/unit_converter.rb
facter-4.2.4 lib/facter/util/facts/unit_converter.rb