Sha256: 7eef7fde824807e0a4065c3ac9bd00284cc1322aa39646ad168619aadba9e2df

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

require "human_bytes/version"
require 'flt'

module HumanBytes
  UNIT_SETS =  [
    {
      B: 1,
      KiB: 2**10,
      MiB: 2**20,
      GiB: 2**30,
      TiB: 2**40,
      PiB: 2**50,
      EiB: 2**60,
      ZiB: 2**70,
      YiB: 2**80
    },
    {
      B: 1,
      KB: 10**3,
      MB: 10**6,
      GB: 10**9,
      TB: 10**12,
      PB: 10**15,
      EB: 10**18,
      ZB: 10**21,
      YB: 10**24,
    }
  ]
  B = 1
  UNIT_SETS.each do |prefix_set|
    prefix_set.each_pair do |prefix,value|
      self.const_set(prefix,value) unless prefix == :B
    end
  end

  ## 
  # places (Integer): number of places after the decimal point
  # i (Boolean): use IT prefixes based on powers of 2 rather than 10

  DEFAULTS = { places: 2, i: true }
  def human_bytes(byte_size, opts={})
    opts = DEFAULTS.merge(opts)
    places = opts[:places]
    i = opts[:i]

    unit_sizes = UNIT_SETS[ i ? 0 : 1]

    last_unit = :B
    unit_sizes.each_pair do |unit,unit_size|
      break if byte_size < unit_size
      last_unit = unit
    end
    last_unit_size = unit_sizes[last_unit]

    qty = (Flt::DecNum(byte_size)/(last_unit_size)).round(places: places)
    "#{qty} #{last_unit}"
  end
  module_function :human_bytes

  module MethodVersion
    def human_bytes(opts={})
      HumanBytes.human_bytes(self, opts)
    end
  end

  def self.monkey_patch!(klass)
    klass.class_eval do
      self.include HumanBytes::MethodVersion
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
human_bytes-0.9.1 lib/human_bytes.rb
human_bytes-0.1.0 lib/human_bytes.rb