bytesize 0.1.0 → 0.1.1
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 +4 -4
- data/.gitignore +12 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +1 -1
- data/Rakefile +6 -0
- data/bytesize.gemspec +56 -0
- data/lib/.DS_Store +0 -0
- data/lib/bytesize.rb +66 -58
- data/lib/bytesize/units.rb +10 -6
- data/lib/bytesize/version.rb +1 -1
- metadata +27 -32
- data/spec/bytesize_spec.rb +0 -30
- data/spec/iecbytesize_spec.rb +0 -30
- data/spec/shared_examples.rb +0 -408
- data/spec/test_enum.rb +0 -86
- data/spec/value_class_helper.rb +0 -363
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a1ec6caf50e4b92ce400618733fce6e0c63337decb18851bf2adcb613d31088
|
4
|
+
data.tar.gz: 8793a89bdcd3571a44871619adf2cd78b059a564cb21546eee3b2932131b23ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02fcb8ef3c111c7100c5c66e29aed886b4785fd0cb504ce07d319e828716280b0e61476959fc8a99c031fb4712e29d77079a1b2efc33572ecce92272a16fbf0a
|
7
|
+
data.tar.gz: a5778c3b14f6a8e4c3cc72b42b67329bfc6c225a7738891c6cc9fd2fdceee14b2b0f10baed9568a5643f0c9f1fe3ae66b1d9a53231e273db7d267808de5c9c8a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
CHANGED
data/Rakefile
ADDED
data/bytesize.gemspec
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
|
2
|
+
#!/usr/bin/env ruby -w
|
3
|
+
# -*- encoding: utf-8 -*-
|
4
|
+
#
|
5
|
+
# ByteSize
|
6
|
+
#
|
7
|
+
# bytesize.gemspec
|
8
|
+
#
|
9
|
+
# © 2018-2021 Adam Hunt
|
10
|
+
# Created: 2018-03-24
|
11
|
+
# Modified: 2021-05-13
|
12
|
+
#
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
require 'pathname'
|
17
|
+
require 'fileutils'
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
gem_root = Pathname.new(__dir__).expand_path
|
22
|
+
lib = (gem_root + 'lib')
|
23
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib.to_s)
|
24
|
+
|
25
|
+
require_relative 'lib/bytesize/version'
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
Gem::Specification.new do |gem|
|
30
|
+
gem.name = 'bytesize'
|
31
|
+
gem.version = ByteSize::VERSION
|
32
|
+
gem.summary = 'A simple Ruby object that stores a size in bytes, while providing human-readible string output.'
|
33
|
+
gem.description = 'ByteSize is a simple Ruby object that stores a size in bytes, while providing human-readible string output with apropriate SI or IEC suffixes. Ample convenience methods are also supplied for quick shorthand.'
|
34
|
+
gem.author = 'Adam Hunt'
|
35
|
+
gem.email = 'bytesize@adamhunt.com'
|
36
|
+
gem.homepage = 'http://github.com/ajmihunt/bytesize'
|
37
|
+
gem.license = 'MIT'
|
38
|
+
|
39
|
+
# Specify which files should be added to the gem when it is released.
|
40
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
41
|
+
gem.files = Dir.chdir(gem_root) do
|
42
|
+
`git ls-files -z`.split("\x0").reject{|f| f.match(%r{^(test|spec|features)/}) }
|
43
|
+
end
|
44
|
+
|
45
|
+
gem.executables = []
|
46
|
+
gem.require_paths = ['lib']
|
47
|
+
gem.test_files = Dir.glob('{test,spec,features}/**/*')
|
48
|
+
|
49
|
+
gem.required_ruby_version = '>= 2.1.0'
|
50
|
+
|
51
|
+
gem.add_development_dependency 'bundler', '>= 2.1.0'
|
52
|
+
gem.add_development_dependency 'rake', '>= 12.3.3'
|
53
|
+
gem.add_development_dependency 'rspec', '>= 3.10.0'
|
54
|
+
end
|
55
|
+
|
56
|
+
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/bytesize.rb
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
#
|
7
7
|
# © 2018 Adam Hunt
|
8
8
|
# Created: 2018-01-17
|
9
|
-
# Modified: 2018-04-
|
9
|
+
# Modified: 2018-04-09
|
10
10
|
#
|
11
11
|
|
12
12
|
|
@@ -73,7 +73,6 @@ class ByteSize
|
|
73
73
|
# :stopdoc:
|
74
74
|
SI_BASE = 1000
|
75
75
|
SI_ORDERS_OF_MAGNITUDE = {
|
76
|
-
bytes: 1,
|
77
76
|
kB: SI_BASE,
|
78
77
|
MB: SI_BASE**2,
|
79
78
|
GB: SI_BASE**3,
|
@@ -82,11 +81,11 @@ class ByteSize
|
|
82
81
|
EB: SI_BASE**6,
|
83
82
|
ZB: SI_BASE**7,
|
84
83
|
YB: SI_BASE**8
|
85
|
-
}
|
84
|
+
}
|
85
|
+
SI_UNIT_SYMBOLS = SI_ORDERS_OF_MAGNITUDE.keys.freeze
|
86
86
|
|
87
87
|
IEC_BASE = 1024
|
88
88
|
IEC_ORDERS_OF_MAGNITUDE = {
|
89
|
-
bytes: 1,
|
90
89
|
KiB: IEC_BASE,
|
91
90
|
MiB: IEC_BASE**2,
|
92
91
|
GiB: IEC_BASE**3,
|
@@ -95,26 +94,41 @@ class ByteSize
|
|
95
94
|
EiB: IEC_BASE**6,
|
96
95
|
ZiB: IEC_BASE**7,
|
97
96
|
YiB: IEC_BASE**8
|
98
|
-
}
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
97
|
+
}
|
98
|
+
IEC_UNIT_SYMBOLS = IEC_ORDERS_OF_MAGNITUDE.keys.freeze
|
99
|
+
|
100
|
+
ALL_ORDERS_OF_MAGNITUDE = SI_ORDERS_OF_MAGNITUDE.merge(IEC_ORDERS_OF_MAGNITUDE).freeze
|
101
|
+
|
102
|
+
# Must be after ALL_ORDERS_OF_MAGNITUDE
|
103
|
+
SI_ORDERS_OF_MAGNITUDE[:bytes] = 1
|
104
|
+
SI_ORDERS_OF_MAGNITUDE.freeze
|
105
|
+
|
106
|
+
IEC_ORDERS_OF_MAGNITUDE[:bytes] = 1
|
107
|
+
IEC_ORDERS_OF_MAGNITUDE.freeze
|
108
|
+
# :startdoc:
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
ALL_UNIT_SYMBOLS = ALL_ORDERS_OF_MAGNITUDE.keys.freeze
|
103
113
|
|
114
|
+
|
115
|
+
|
116
|
+
BASE = SI_BASE
|
117
|
+
UNIT_SYMBOLS = SI_UNIT_SYMBOLS
|
104
118
|
ORDERS_OF_MAGNITUDE = SI_ORDERS_OF_MAGNITUDE
|
105
|
-
# :startdoc:
|
106
119
|
|
107
120
|
|
108
121
|
|
109
122
|
# :stopdoc:
|
110
123
|
# Creates convenience methods for all unit symbols
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
124
|
+
ALL_ORDERS_OF_MAGNITUDE.each do |s,om|
|
125
|
+
m = s.downcase
|
126
|
+
define_singleton_method(m) do |val|
|
127
|
+
raise( TypeError, "expected #{Numeric}, got #{val.class}" ) unless val.is_a?(Numeric)
|
128
|
+
self.new(( val * om ).round)
|
115
129
|
end
|
116
|
-
define_method(:"to_#{
|
117
|
-
bytes / Float(
|
130
|
+
define_method(:"to_#{m}") do
|
131
|
+
bytes / Float(om)
|
118
132
|
end
|
119
133
|
end
|
120
134
|
# :startdoc:
|
@@ -123,8 +137,8 @@ class ByteSize
|
|
123
137
|
|
124
138
|
# :stopdoc:
|
125
139
|
BYTES_REGEX = /\A\s*(\-?[0-9]+)\s*(bytes)?\s*\z/.freeze
|
126
|
-
SI_REGEX
|
127
|
-
IEC_REGEX
|
140
|
+
SI_REGEX = /\A\s*(\-?[0-9]+(\.[0-9]+)?)\s*(#{ SI_UNIT_SYMBOLS.join('|') })\s*\z/i.freeze
|
141
|
+
IEC_REGEX = /\A\s*(\-?[0-9]+(\.[0-9]+)?)\s*(#{ IEC_UNIT_SYMBOLS.join('|') })\s*\z/i.freeze
|
128
142
|
# :startdoc:
|
129
143
|
|
130
144
|
|
@@ -310,8 +324,7 @@ class ByteSize
|
|
310
324
|
# {ByteSize}[ByteSize.html] or {IECByteSize}[IECByteSize.html] depending on it's unit symbol.
|
311
325
|
#
|
312
326
|
def self.parse( val )
|
313
|
-
|
314
|
-
when String
|
327
|
+
if val.is_a?(String)
|
315
328
|
if m = val.match(BYTES_REGEX)
|
316
329
|
ByteSize.new( m[1].to_i )
|
317
330
|
elsif m = val.match(SI_REGEX)
|
@@ -322,7 +335,7 @@ class ByteSize
|
|
322
335
|
raise( ArgumentError, "invalid #{ByteSize} or #{IECByteSize} string: #{val.inspect}" )
|
323
336
|
end
|
324
337
|
else
|
325
|
-
raise( TypeError, "expected #{String}, got #{
|
338
|
+
raise( TypeError, "expected #{String}, got #{val.class}" )
|
326
339
|
end
|
327
340
|
end
|
328
341
|
|
@@ -330,32 +343,27 @@ class ByteSize
|
|
330
343
|
|
331
344
|
# :stopdoc:
|
332
345
|
def self.new( val )
|
333
|
-
|
334
|
-
super( val.bytes )
|
335
|
-
else
|
336
|
-
case val
|
337
|
-
|
338
|
-
when Integer
|
339
|
-
super( val )
|
340
|
-
|
341
|
-
when ByteSize
|
342
|
-
super( val.bytes )
|
343
|
-
|
344
|
-
when String
|
345
|
-
if m = val.match(BYTES_REGEX)
|
346
|
-
super( m[1].to_i )
|
347
|
-
elsif m = val.match(SI_REGEX)
|
348
|
-
self.send( m[3].downcase.to_sym, m[2].nil? ? m[1].to_i : m[1].to_f )
|
349
|
-
elsif m = val.match(IEC_REGEX)
|
350
|
-
self.send( m[3].downcase.to_sym, m[2].nil? ? m[1].to_i : m[1].to_f )
|
351
|
-
else
|
352
|
-
raise( ArgumentError, "invalid #{self} string: #{val.inspect}" )
|
353
|
-
end
|
346
|
+
case val
|
354
347
|
|
348
|
+
when ByteSize
|
349
|
+
super( val.bytes )
|
350
|
+
|
351
|
+
when Integer
|
352
|
+
super( val )
|
353
|
+
|
354
|
+
when String
|
355
|
+
if m = val.match(BYTES_REGEX)
|
356
|
+
super( m[1].to_i )
|
357
|
+
elsif m = val.match(SI_REGEX)
|
358
|
+
self.send( m[3].downcase.to_sym, m[2].nil? ? m[1].to_i : m[1].to_f )
|
359
|
+
elsif m = val.match(IEC_REGEX)
|
360
|
+
self.send( m[3].downcase.to_sym, m[2].nil? ? m[1].to_i : m[1].to_f )
|
355
361
|
else
|
356
|
-
raise(
|
357
|
-
|
362
|
+
raise( ArgumentError, "invalid #{self} string: #{val.inspect}" )
|
358
363
|
end
|
364
|
+
|
365
|
+
else
|
366
|
+
raise( TypeError, "no implicit conversion of #{val.class} into #{self}" )
|
359
367
|
end
|
360
368
|
end
|
361
369
|
# :startdoc:
|
@@ -371,9 +379,7 @@ class ByteSize
|
|
371
379
|
# or {String}[https://ruby-doc.org/core/String.html].
|
372
380
|
#
|
373
381
|
def initialize( bytes )
|
374
|
-
raise( TypeError, "expected #{Integer}, got #{bytes.class}" ) unless bytes.is_a?(Integer)
|
375
382
|
@bytes = bytes
|
376
|
-
freeze
|
377
383
|
end
|
378
384
|
|
379
385
|
|
@@ -547,12 +553,12 @@ class ByteSize
|
|
547
553
|
def <( other )
|
548
554
|
case other
|
549
555
|
|
550
|
-
when Numeric
|
551
|
-
bytes < other
|
552
|
-
|
553
556
|
when ByteSize
|
554
557
|
bytes < other.bytes
|
555
558
|
|
559
|
+
when Numeric
|
560
|
+
bytes < other
|
561
|
+
|
556
562
|
else
|
557
563
|
raise( ArgumentError, "comparison of #{self.class} with #{other.inspect} failed" )
|
558
564
|
end
|
@@ -569,12 +575,12 @@ class ByteSize
|
|
569
575
|
def <=( other )
|
570
576
|
case other
|
571
577
|
|
572
|
-
when Numeric
|
573
|
-
bytes <= other
|
574
|
-
|
575
578
|
when ByteSize
|
576
579
|
bytes <= other.bytes
|
577
580
|
|
581
|
+
when Numeric
|
582
|
+
bytes <= other
|
583
|
+
|
578
584
|
else
|
579
585
|
raise( ArgumentError, "comparison of #{self.class} with #{other.inspect} failed" )
|
580
586
|
end
|
@@ -591,12 +597,12 @@ class ByteSize
|
|
591
597
|
def >( other )
|
592
598
|
case other
|
593
599
|
|
594
|
-
when Numeric
|
595
|
-
bytes > other
|
596
|
-
|
597
600
|
when ByteSize
|
598
601
|
bytes > other.bytes
|
599
602
|
|
603
|
+
when Numeric
|
604
|
+
bytes > other
|
605
|
+
|
600
606
|
else
|
601
607
|
raise( ArgumentError, "comparison of #{self.class} with #{other.inspect} failed" )
|
602
608
|
end
|
@@ -613,12 +619,12 @@ class ByteSize
|
|
613
619
|
def >=( other )
|
614
620
|
case other
|
615
621
|
|
616
|
-
when Numeric
|
617
|
-
bytes >= other
|
618
|
-
|
619
622
|
when ByteSize
|
620
623
|
bytes >= other.bytes
|
621
624
|
|
625
|
+
when Numeric
|
626
|
+
bytes >= other
|
627
|
+
|
622
628
|
else
|
623
629
|
raise( ArgumentError, "comparison of #{self.class} with #{other.inspect} failed" )
|
624
630
|
end
|
@@ -1099,6 +1105,8 @@ end
|
|
1099
1105
|
class IECByteSize < ByteSize
|
1100
1106
|
|
1101
1107
|
# :stopdoc:
|
1108
|
+
BASE = IEC_BASE
|
1109
|
+
UNIT_SYMBOLS = IEC_UNIT_SYMBOLS
|
1102
1110
|
ORDERS_OF_MAGNITUDE = IEC_ORDERS_OF_MAGNITUDE
|
1103
1111
|
# :startdoc:
|
1104
1112
|
|
data/lib/bytesize/units.rb
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
#
|
7
7
|
# © 2018 Adam Hunt
|
8
8
|
# Created: 2018-01-18
|
9
|
-
# Modified: 2018-
|
9
|
+
# Modified: 2018-04-08
|
10
10
|
#
|
11
11
|
|
12
12
|
|
@@ -32,7 +32,9 @@ require 'bytesize'
|
|
32
32
|
#
|
33
33
|
# 42.gib.to_mib #=> 43008.0
|
34
34
|
#
|
35
|
-
# 22.gib.to_si #=>
|
35
|
+
# 22.gib.to_si #=> (23.62 GB)
|
36
|
+
#
|
37
|
+
# 16.gb.to_iec #=> (14.9 GiB)
|
36
38
|
#
|
37
39
|
class Numeric
|
38
40
|
|
@@ -40,14 +42,16 @@ class Numeric
|
|
40
42
|
|
41
43
|
# :stopdoc:
|
42
44
|
ByteSize::SI_ORDERS_OF_MAGNITUDE.keys.each do |k|
|
43
|
-
|
44
|
-
|
45
|
+
m = k.downcase
|
46
|
+
define_method(m) do
|
47
|
+
ByteSize.send( m, self )
|
45
48
|
end
|
46
49
|
end
|
47
50
|
|
48
51
|
IECByteSize::UNIT_SYMBOLS.keys.reject{|k| k == :bytes }.each do |k|
|
49
|
-
|
50
|
-
|
52
|
+
m = k.downcase
|
53
|
+
define_method(m) do
|
54
|
+
IECByteSize.send( m, self )
|
51
55
|
end
|
52
56
|
end
|
53
57
|
# :startdoc:
|
data/lib/bytesize/version.rb
CHANGED
metadata
CHANGED
@@ -1,81 +1,82 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bytesize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Hunt
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.1.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.1.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 12.3.3
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 12.3.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.10.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.10.0
|
55
55
|
description: ByteSize is a simple Ruby object that stores a size in bytes, while providing
|
56
56
|
human-readible string output with apropriate SI or IEC suffixes. Ample convenience
|
57
57
|
methods are also supplied for quick shorthand.
|
58
|
-
email: bytesize@adamhunt.
|
58
|
+
email: bytesize@adamhunt.com
|
59
59
|
executables: []
|
60
60
|
extensions: []
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
63
66
|
- LICENSE.txt
|
64
67
|
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bytesize.gemspec
|
70
|
+
- lib/.DS_Store
|
65
71
|
- lib/bytesize.rb
|
66
72
|
- lib/bytesize/activerecord.rb
|
67
73
|
- lib/bytesize/units.rb
|
68
74
|
- lib/bytesize/version.rb
|
69
|
-
- spec/bytesize_spec.rb
|
70
|
-
- spec/iecbytesize_spec.rb
|
71
|
-
- spec/shared_examples.rb
|
72
|
-
- spec/test_enum.rb
|
73
|
-
- spec/value_class_helper.rb
|
74
75
|
homepage: http://github.com/ajmihunt/bytesize
|
75
76
|
licenses:
|
76
77
|
- MIT
|
77
78
|
metadata: {}
|
78
|
-
post_install_message:
|
79
|
+
post_install_message:
|
79
80
|
rdoc_options: []
|
80
81
|
require_paths:
|
81
82
|
- lib
|
@@ -90,15 +91,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
91
|
- !ruby/object:Gem::Version
|
91
92
|
version: '0'
|
92
93
|
requirements: []
|
93
|
-
|
94
|
-
|
95
|
-
signing_key:
|
94
|
+
rubygems_version: 3.2.9
|
95
|
+
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: A simple Ruby object that stores a size in bytes, while providing human-readible
|
98
|
-
string output
|
99
|
-
test_files:
|
100
|
-
- spec/test_enum.rb
|
101
|
-
- spec/value_class_helper.rb
|
102
|
-
- spec/shared_examples.rb
|
103
|
-
- spec/bytesize_spec.rb
|
104
|
-
- spec/iecbytesize_spec.rb
|
98
|
+
string output.
|
99
|
+
test_files: []
|