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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3aa26ab3dee5c630d19eda1edc877cc12d251ed719347b1af722cbb8b66d387b
4
- data.tar.gz: 2a5f2ff76d9e7c1bf2dbcca25cec46cf77b642f4e3c4ea4e7189bd520f7a8aa9
3
+ metadata.gz: 3a1ec6caf50e4b92ce400618733fce6e0c63337decb18851bf2adcb613d31088
4
+ data.tar.gz: 8793a89bdcd3571a44871619adf2cd78b059a564cb21546eee3b2932131b23ab
5
5
  SHA512:
6
- metadata.gz: '0096603896d44f87d44c048c6b8da3b89469103f73e0336ce113f4f2025cf7664ec986706ce8129bb89b454d70c959bf6d2190be11a7ee6c8d4f60a092caf221'
7
- data.tar.gz: 9b8a3fdf9331eff6158efa41b40bab085a3ebc0968309a1d8e25d2e867a0bd629d73f6c24fd87f7f4159fe367231044a0d9d77f8b166672c9b77cd175423619f
6
+ metadata.gz: 02fcb8ef3c111c7100c5c66e29aed886b4785fd0cb504ce07d319e828716280b0e61476959fc8a99c031fb4712e29d77079a1b2efc33572ecce92272a16fbf0a
7
+ data.tar.gz: a5778c3b14f6a8e4c3cc72b42b67329bfc6c225a7738891c6cc9fd2fdceee14b2b0f10baed9568a5643f0c9f1fe3ae66b1d9a53231e273db7d267808de5c9c8a
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bytesize.gemspec
6
+ gemspec
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright © 2018 Adam Hunt
3
+ Copyright © 2018-2021 Adam Hunt
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
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-08
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
- }.freeze
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
- }.freeze
99
-
100
- UNIT_SYMBOLS = SI_ORDERS_OF_MAGNITUDE.merge(IEC_ORDERS_OF_MAGNITUDE)
101
- UNIT_SYMBOLS.delete(:bytes)
102
- UNIT_SYMBOLS.freeze
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
- UNIT_SYMBOLS.each do |k,v|
112
- define_singleton_method(k.downcase) do |n|
113
- raise( TypeError, "expected #{Numeric}, got #{n.class}" ) unless n.is_a?(Numeric)
114
- self.new( (n*v).round )
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_#{k.downcase}") do
117
- bytes / Float(v)
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 = /\A\s*(\-?[0-9]+(\.[0-9]+)?)\s*(#{ SI_ORDERS_OF_MAGNITUDE.keys.reject{|k| k == :bytes }.join('|') })\s*\z/i.freeze
127
- IEC_REGEX = /\A\s*(\-?[0-9]+(\.[0-9]+)?)\s*(#{ IEC_ORDERS_OF_MAGNITUDE.keys.reject{|k| k == :bytes }.join('|') })\s*\z/i.freeze
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
- case val
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 #{str.class}" )
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
- if val.class == self
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( TypeError, "no implicit conversion of #{val.class} into #{self}" )
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
 
@@ -6,7 +6,7 @@
6
6
  #
7
7
  # © 2018 Adam Hunt
8
8
  # Created: 2018-01-18
9
- # Modified: 2018-01-18
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
- define_method(k.downcase) do
44
- ByteSize.send( k.downcase, self )
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
- define_method(k.downcase) do
50
- IECByteSize.send( k.downcase, self )
52
+ m = k.downcase
53
+ define_method(m) do
54
+ IECByteSize.send( m, self )
51
55
  end
52
56
  end
53
57
  # :startdoc:
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
  class ByteSize
15
- VERSION = "0.1.0".freeze
15
+ VERSION = '0.1.1'.freeze
16
16
  end
17
17
 
18
18
 
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.0
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: 2018-04-08 00:00:00.000000000 Z
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: '1.16'
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: '1.16'
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: '10.0'
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: '10.0'
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: '3.0'
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: '3.0'
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.name
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
- rubyforge_project:
94
- rubygems_version: 2.7.3
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: []