Module: Kibi

Extended by:
Units
Defined in:
lib/kibi/kibi.rb,
lib/kibi/units.rb

Defined Under Namespace

Modules: Units

Class Method Summary collapse

Methods included from Units

exbibyte, exbibytes, gibibyte, gibibytes, kibibyte, kibibytes, mebibyte, mebibytes, pebibyte, pebibytes, tebibyte, tebibytes, yobibyte, yobibytes, zebibyte, zebibytes

Class Method Details

.bytes_to(bytes, unit) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kibi/kibi.rb', line 39

def bytes_to bytes, unit
  case unit
  when :k, :kb, :kibibytes, :kilobytes
    bytes.to_f / kibibyte
  when :m, :mb, :mebibytes, :megabytes
    bytes.to_f / mebibyte
  when :g, :gb, :gibibytes, :gigabytes
    bytes.to_f / gibibyte
  when :t, :tb, :tebibytes, :terabytes
    bytes.to_f / tebibyte
  when :p, :pb, :pebibytes, :petabytes
    bytes.to_f / pebibyte
  when :e, :eb, :exbibytes, :exabytes
    bytes.to_f / exbibyte
  when :z, :zb, :zebibytes, :zettabytes
    bytes.to_f / zebibyte
  when :y, :yb, :yobibytes, :yottabytes
    bytes.to_f / yobibyte
  end
end

.humanize(bytes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kibi/kibi.rb', line 8

def humanize bytes
  case
  when bytes >= yobibyte
    [bytes.to_f / yobibyte, :yb]
  when bytes >= zebibyte
    [bytes.to_f / zebibyte, :zb]
  when bytes >= exbibyte
    [bytes.to_f / exbibyte, :eb]
  when bytes >= pebibyte
    [bytes.to_f / pebibyte, :pb]
  when bytes >= tebibyte
    [bytes.to_f / tebibyte, :tb]
  when bytes >= gibibyte
    [bytes.to_f / gibibyte, :gb]
  when bytes >= mebibyte
    [bytes.to_f / mebibyte, :mb]
  when bytes >= kibibyte
    [bytes.to_f / kibibyte, :kb]
  else
    [bytes, :b]
  end
end

.humanize_file_size(path) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/kibi/kibi.rb', line 31

def humanize_file_size path
  p = Pathname.new File.expand_path(path)
  raise "File #{path} does not exist" unless p.exist?
  raise "Path must be a file (#{path} is a directory)" if p.directory?

  humanize p.size
end