class Chef::Provider::Humanize

Public Class Methods

new(size) click to toggle source
# File lib/garcon/chef/provider/house_keeping.rb, line 182
def initialize(size)
  @units = { b: 1, kb: 1024**1, mb: 1024**2, gb: 1024**3, tb: 1024**4 }
  @size_int = size.partition(/\D{1,2}/).at(0).to_i
  unit = size.partition(/\D{1,2}/).at(1).to_s.downcase
  case
  when unit.match(/[kmgtpe]{1}/)
    @size_unit = unit.concat('b')
  when unit.match(/[kmgtpe]{1}b/)
    @size_unit = unit
  else
    @size_unit = 'b'
  end
end

Public Instance Methods

from_size(places = 1) click to toggle source
# File lib/garcon/chef/provider/house_keeping.rb, line 203
def from_size(places = 1)
  unit_val = @units[@size_unit.to_s.downcase.to_sym]
  sprintf("%.#{places}f", @size_int * unit_val)
end
to_size(unit, places = 1) click to toggle source
# File lib/garcon/chef/provider/house_keeping.rb, line 196
def to_size(unit, places = 1)
  unit_val = @units[unit.to_s.downcase.to_sym]
  bytes    = @size_int * @units[@size_unit.to_sym]
  size     = bytes.to_f / unit_val.to_f
  sprintf("%.#{places}f", size).to_f
end