human_size 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ce0238ef7558590ab59ffbc0f4d2636bf4193c60ca5e8e3b76c7f01f0a2da9d
4
- data.tar.gz: 19e40d58ab32edb79771be980c812cc3045d86e9f22ff0136c171975dfb771a1
3
+ metadata.gz: 11fdc48e4306cd561cf09deda15f67b526acb8adb0deb28d1ffcf58f507fa77e
4
+ data.tar.gz: f5acea948b763c92736eb551e8de1ac25cd9630bb8cfcc670d55d8270789246e
5
5
  SHA512:
6
- metadata.gz: 579f788092707a21bc8e97f0942114992bf34ea6e1427b956f4e7a97f54b66e1ce9d1028d33ecfd5f1938f21ba5d34f72fc4b01ba5b5d2316d37e4b02630947c
7
- data.tar.gz: 3f6e7898718e7de8c2c7d8089fabd21b640653314f4863b4668fddb4ddc65fae842e8d7260481632135fe1b4298122935a13af419498e91c3c74fecb86986912
6
+ metadata.gz: f809185a8dbca0ad88a031ab084ccbe67080757cf39b42759fe1c6def90e56d3053924ce0de6fdaa2d26ea147043bbe251cd5ac6fc686f72fff0c6f99f2b92e6
7
+ data.tar.gz: 36bc70e14dd5f6dfb069f87fd0f778ab08e2ea16b043ccb49c5dbf3fec233a628aed11d722a1ae8d02134b985935d4947bf7df60a16eb18aa7fe6d1ad7ad145b
data/CHANGELOG.md CHANGED
@@ -1,4 +1,12 @@
1
- ## 1.0.0 (January 17, 2019)
1
+ ## 1.1.0 (January 24, 2019)
2
+
3
+ IMPROVEMENTS:
4
+
5
+ * Move the code inside of a module namespace. ([@TGWolf][])
6
+ * Extend the integer class so that int.human_size works. ([@TGWolf][])
7
+ * Update the documentation. ([@TGWolf][])
8
+
9
+ ## 1.0.0 (January 18, 2019)
2
10
 
3
11
  * Initial Release ([@TGWolf][])
4
12
 
data/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  # Human Size
10
10
 
11
- Stuff
11
+ Convert the number of bytes into a more human readable format.
12
12
 
13
13
  ## Installation
14
14
 
@@ -29,7 +29,13 @@ Or install it yourself as:
29
29
  ## Usage
30
30
 
31
31
  ```ruby
32
+ require 'human_size'
32
33
 
34
+ puts Humansize::Size.human_size(10)
35
+
36
+ # or
37
+
38
+ puts 10.human_size
33
39
  ```
34
40
 
35
41
  ## Development
data/example/example.rb CHANGED
@@ -7,10 +7,8 @@ test_value = [-10, 0, 9, 98, 987, 987_6, 987_65, 987_654, 987_654_3, 987_654_32,
7
7
  printf "\t%20s | %20s | %20s |\n", 'Original', 'Binary', 'Si'
8
8
  printf "\t%20s + %20s + %20s +\n", '-' * 20, '-' * 20, '-' * 20
9
9
 
10
- h = HumanSize.new
11
-
12
10
  test_value.each do |x|
13
- printf "\t%20s | %20s | %20s |\n", x, h.human_size(x), h.human_size(x, false)
11
+ printf "\t%20s | %20s | %20s |\n", x, x.human_size, x.human_size(false)
14
12
  end
15
13
 
16
14
  printf "\t%20s + %20s + %20s +\n", '-' * 20, '-' * 20, '-' * 20
@@ -0,0 +1,8 @@
1
+ #
2
+ # Overload the integer class
3
+ #
4
+ class Integer
5
+ def human_size(a_kilobyte_is_1024_bytes = true)
6
+ HumanSize::Size.human_size(self, a_kilobyte_is_1024_bytes)
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
- class HumanSize
2
- VERSION = '1.0.0'.freeze
1
+ module HumanSize
2
+ VERSION = '1.1.0'.freeze
3
3
  end
data/lib/human_size.rb CHANGED
@@ -1,68 +1,43 @@
1
- require 'human_size/version'
2
-
3
- # -------------------------------------------------------------------------------- #
4
- # Description #
5
- # -------------------------------------------------------------------------------- #
6
- # Display number of bytes in a more human readable format. #
7
- # -------------------------------------------------------------------------------- #
8
-
9
- # -------------------------------------------------------------------------------- #
10
- # Description #
11
- # -------------------------------------------------------------------------------- #
12
- # This is a simple global method to short cut the usage of the class below. #
13
- # -------------------------------------------------------------------------------- #
14
- def human_size(bytes, a_kilobyte_is_1024_bytes = true)
15
- h = HumanSize.new
16
- h.human_size(bytes, a_kilobyte_is_1024_bytes)
17
- end
18
-
19
- # -------------------------------------------------------------------------------- #
20
- # Description #
21
- # -------------------------------------------------------------------------------- #
22
- # This is a static access wrapper to the main HumanDuration class below. #
23
- # -------------------------------------------------------------------------------- #
24
- class HumanSizeStatic
25
- def self.human_size(bytes, a_kilobyte_is_1024_bytes = true)
26
- h = HumanSize.new
27
- h.human_size(bytes, a_kilobyte_is_1024_bytes)
28
- end
29
- end
30
-
31
- # -------------------------------------------------------------------------------- #
32
- # Description #
33
- # -------------------------------------------------------------------------------- #
34
- # This is the main class and the one that does all the real work and is called by #
35
- # the static access class and the direct method access above. #
36
- # -------------------------------------------------------------------------------- #
37
- class HumanSize
38
- def human_size(bytes, a_kilobyte_is_1024_bytes = true)
39
- suffixes_table = {
40
- # Unit prefixes used for SI file sizes.
41
- 'SI' => ['B', 'KB', 'MB', 'GB', 'TB', 'Pb', 'EB', 'ZB', 'YB'],
42
- # Unit prefixes used for binary file sizes.
43
- 'BINARY' => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'Pib', 'EiB', 'ZiB', 'YiB']
44
- }
45
-
46
- return 'unknown' if bytes < 0
47
- return '0.0 B' if bytes.zero?
48
-
49
- units = if a_kilobyte_is_1024_bytes
50
- suffixes_table['BINARY']
51
- else
52
- suffixes_table['SI']
53
- end
54
-
55
- multiple = if a_kilobyte_is_1024_bytes
56
- 1024
57
- else
58
- 1000
59
- end
60
-
61
- exp = (Math.log(bytes) / Math.log(multiple)).to_i
62
- exp = units.length if exp > units.length
63
-
64
- # rubocop:disable Layout/SpaceAroundOperators
65
- format('%<bytes>.1f %<unit>s', bytes: bytes.to_f / multiple ** exp, unit: units[exp])
66
- # rubocop:enable Layout/SpaceAroundOperators
1
+ require_relative 'human_size/version'
2
+ require_relative 'human_size/overloads'
3
+
4
+ #
5
+ # Add docs
6
+ #
7
+ module HumanSize
8
+ #
9
+ # Add docs
10
+ #
11
+ class Size
12
+ def self.human_size(bytes, a_kilobyte_is_1024_bytes = true)
13
+ suffixes_table = {
14
+ # Unit prefixes used for SI file sizes.
15
+ 'SI' => ['B', 'KB', 'MB', 'GB', 'TB', 'Pb', 'EB', 'ZB', 'YB'],
16
+ # Unit prefixes used for binary file sizes.
17
+ 'BINARY' => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'Pib', 'EiB', 'ZiB', 'YiB']
18
+ }
19
+
20
+ return 'unknown' if bytes < 0
21
+ return '0.0 B' if bytes.zero?
22
+
23
+ units = if a_kilobyte_is_1024_bytes
24
+ suffixes_table['BINARY']
25
+ else
26
+ suffixes_table['SI']
27
+ end
28
+
29
+ multiple = if a_kilobyte_is_1024_bytes
30
+ 1024
31
+ else
32
+ 1000
33
+ end
34
+
35
+ exp = (Math.log(bytes) / Math.log(multiple)).to_i
36
+ exp = units.length if exp > units.length
37
+
38
+ # rubocop:disable Layout/SpaceAroundOperators
39
+ format('%<bytes>.1f %<unit>s', bytes: bytes.to_f / multiple ** exp, unit: units[exp])
40
+ # rubocop:enable Layout/SpaceAroundOperators
41
+ end
67
42
  end
68
43
  end
@@ -7,99 +7,66 @@ RSpec.describe HumanSize do
7
7
  expect(HumanSize::VERSION).not_to be nil
8
8
  end
9
9
 
10
- context 'Class method access' do
10
+ context 'Class Accessor Method' do
11
11
  before(:each) do
12
- @h = HumanSize.new
12
+ @hs = HumanSize::Size
13
13
  end
14
14
 
15
15
  context 'Binary (1024 bytes per Megabyte)) mode' do
16
16
  it 'says unknown for bytes < 0' do
17
- expect(@h.human_size(-1)).to eql('unknown')
17
+ expect(@hs.human_size(-1)).to eql('unknown')
18
18
  end
19
19
 
20
20
  it 'says 0.0 B for 0 bytes' do
21
- expect(@h.human_size(0)).to eql('0.0 B')
21
+ expect(@hs.human_size(0)).to eql('0.0 B')
22
22
  end
23
23
 
24
24
  it 'says 941.9 MiB for 987654321 bytes' do
25
- expect(@h.human_size(987_654_321)).to eql('941.9 MiB')
25
+ expect(@hs.human_size(987_654_321)).to eql('941.9 MiB')
26
26
  end
27
27
  end
28
28
 
29
29
  context 'SI (1000 bytes per Megabyte) mode' do
30
30
  it 'says unknown for bytes < 0' do
31
- expect(@h.human_size(-1, false)).to eql('unknown')
31
+ expect(@hs.human_size(-1, false)).to eql('unknown')
32
32
  end
33
33
 
34
34
  it 'says 0.0 B for 0 bytes' do
35
- expect(@h.human_size(0, false)).to eql('0.0 B')
35
+ expect(@hs.human_size(0, false)).to eql('0.0 B')
36
36
  end
37
37
 
38
38
  it 'says 987.7 MB for 987654321 bytes' do
39
- expect(@h.human_size(987_654_321, false)).to eql('987.7 MB')
39
+ expect(@hs.human_size(987_654_321, false)).to eql('987.7 MB')
40
40
  end
41
41
  end
42
42
  end
43
43
 
44
- context 'Static method access' do
45
- before(:each) do
46
- @h = HumanSizeStatic
47
- end
48
-
44
+ context 'String Accessor Method' do
49
45
  context 'Binary (1024 bytes per Megabyte)) mode' do
50
46
  it 'says unknown for bytes < 0' do
51
- expect(@h.human_size(-1)).to eql('unknown')
47
+ expect(-1.human_size).to eql('unknown')
52
48
  end
53
49
 
54
50
  it 'says 0.0 B for 0 bytes' do
55
- expect(@h.human_size(0)).to eql('0.0 B')
51
+ expect(0.human_size).to eql('0.0 B')
56
52
  end
57
53
 
58
54
  it 'says 941.9 MiB for 987654321 bytes' do
59
- expect(@h.human_size(987_654_321)).to eql('941.9 MiB')
55
+ expect(987_654_321.human_size).to eql('941.9 MiB')
60
56
  end
61
57
  end
62
58
 
63
59
  context 'SI (1000 bytes per Megabyte)) mode' do
64
60
  it 'says unknown for bytes < 0' do
65
- expect(@h.human_size(-1, false)).to eql('unknown')
66
- end
67
-
68
- it 'says 0.0 B for 0 bytes' do
69
- expect(@h.human_size(0, false)).to eql('0.0 B')
70
- end
71
-
72
- it 'says 987.7 MB for 987654321 bytes' do
73
- expect(@h.human_size(987_654_321, false)).to eql('987.7 MB')
74
- end
75
- end
76
- end
77
-
78
- context 'Direct method access' do
79
- context 'Binary (1024 bytes per Megabyte)) mode' do
80
- it 'says unknown for bytes < 0' do
81
- expect(human_size(-1)).to eql('unknown')
82
- end
83
-
84
- it 'says 0.0 B for 0 bytes' do
85
- expect(human_size(0)).to eql('0.0 B')
86
- end
87
-
88
- it 'says 941.9 MiB for 987654321 bytes' do
89
- expect(human_size(987_654_321)).to eql('941.9 MiB')
90
- end
91
- end
92
- context 'SI (1000 bytes per Megabyte)) mode' do
93
- it 'says unknown for bytes < 0' do
94
- expect(human_size(-1, false)).to eql('unknown')
61
+ expect(-1.human_size(false)).to eql('unknown')
95
62
  end
96
63
 
97
64
  it 'says 0.0 B for 0 bytes' do
98
- expect(human_size(0, false)).to eql('0.0 B')
65
+ expect(0.human_size(false)).to eql('0.0 B')
99
66
  end
100
67
 
101
68
  it 'says 987.7 MB for 987654321 bytes' do
102
- expect(human_size(987_654_321, false)).to eql('987.7 MB')
69
+ expect(987_654_321.human_size(false)).to eql('987.7 MB')
103
70
  end
104
71
  end
105
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_size
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Gurney aka Wolf
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-18 00:00:00.000000000 Z
11
+ date: 2019-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,7 @@ files:
76
76
  - example/example.rb
77
77
  - human_size.gemspec
78
78
  - lib/human_size.rb
79
+ - lib/human_size/overloads.rb
79
80
  - lib/human_size/version.rb
80
81
  - spec/human_size_spec.rb
81
82
  - spec/spec_helper.rb