better_bytes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/better_bytes.rb +44 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d93b833b1d4610524a85beccca26107013b9f585
4
+ data.tar.gz: 8d474e53bb3d4ed0b4a558c64cdef6e97f728ee1
5
+ SHA512:
6
+ metadata.gz: 7c504ce8ad19e38331b5e0509491e43ff59ad7959eabfa1c6f41833c7e51577eb313991e9fb0e973b912ba7e41857be7d30abfa09b58d8ab1a7feef89b25d387
7
+ data.tar.gz: 48eaeccf7ccc1be614283152f4e8f22b736ccbfb078c2fede57a3abb4beae152d5cad07d8ec5f45b1b22437b2e5ac06c869a2043c8e9d211d4eaaaf5b5487c9b
@@ -0,0 +1,44 @@
1
+ class BetterBytes
2
+ BINARY = [%w{B KiB MiB GiB TiB PiB EiB ZiB YiB}, 1024]
3
+ DECIMAL = [%w{B KB MB GB TB PB EB ZB YB}, 1000]
4
+
5
+ def self.humanize(val, precision: 0.1, units: :binary)
6
+ val.is_a? String and val = dehumanize(val)
7
+
8
+ if units.is_a? Symbol
9
+ abbrev, mult = units == :binary ? BINARY : DECIMAL
10
+ t = [1,'B']
11
+ abbrev.each_with_index do |a,i|
12
+ m = mult ** i
13
+ val < m and break
14
+ t = [m,a]
15
+ end
16
+ elsif i = BINARY[0].index(units)
17
+ t = [BINARY[1] ** i, units]
18
+ elsif i = DECIMAL[0].index(units)
19
+ t = [DECIMAL[1] ** i, units]
20
+ else
21
+ raise "Unknown units: #{units}"
22
+ end
23
+
24
+ return "%#{precision}f %s" % [val.to_f / t[0], t[1]]
25
+ end
26
+
27
+ def self.dehumanize(str)
28
+ m = str.match(%r{(\d*(\.\d+)?)\s*(\w{0,3})}) or raise "Unable to dehumanize: #{str}"
29
+
30
+ v = m[2] ? m[1].to_f : m[1].to_i
31
+
32
+ abbrev, mult = m[3][1] == ?i ? BINARY : DECIMAL
33
+
34
+ unless exp = abbrev.index(m[3])
35
+ if m[3] == nil || m[3].empty?
36
+ exp = 0
37
+ else
38
+ raise "Unknown unit: #{m[3]} (extracted from #{str})"
39
+ end
40
+ end
41
+
42
+ return v * (mult ** exp)
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_bytes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brad Robel-Forrest
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Convert or create human readable byte strings in either binary (1024)
14
+ or decimal (1000) unit abbreviations.
15
+ email: brad@bitpony.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/better_bytes.rb
21
+ homepage: https://github.com/bradrf/better_bytes#readme
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.8
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Bytes for human consumption.
45
+ test_files: []