bytesize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby -w
2
+ #
3
+ # ByteSize
4
+ #
5
+ # ActiveRecord Type
6
+ #
7
+ # © 2018 Adam Hunt
8
+ # Created: 2018-01-24
9
+ # Modified: 2018-01-24
10
+ #
11
+
12
+
13
+
14
+ require 'bytesize'
15
+
16
+
17
+
18
+ class ByteSize
19
+ class ActiveRecordType < ActiveRecord::Type::Value
20
+
21
+ def cast( value )
22
+ value.nil? ? nil : ByteSize.new(value)
23
+ end
24
+
25
+ def deserialize( value )
26
+ value.nil? ? nil : ByteSize.new(value)
27
+ end
28
+
29
+ def serialize( value )
30
+ value.nil? ? nil : value.to_i
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+ ActiveRecord::Type.register( :bytesize, ByteSize::ActiveRecordType )
37
+
38
+ module ActiveRecord
39
+ module ConnectionAdapters
40
+ module MySQL
41
+ module ColumnMethods
42
+
43
+ def bytesize( *args, **options )
44
+ args.each{|name| column( name, :bigint, options.merge(unsigned:true) )}
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,227 @@
1
+ #!/usr/bin/env ruby -w
2
+ #
3
+ # ByteSize
4
+ #
5
+ # Unit-based Numeric convenience methods
6
+ #
7
+ # © 2018 Adam Hunt
8
+ # Created: 2018-01-18
9
+ # Modified: 2018-01-18
10
+ #
11
+
12
+
13
+
14
+ require 'bytesize'
15
+
16
+
17
+
18
+ # Requiring <code>bytesize/units</code> adds convenience methods to
19
+ # {Numeric}[https://ruby-doc.org/stdlib/libdoc/pathname/rdoc/Numeric.html]
20
+ # for easy creation of {ByteSize}[ByteSize.html] and {IECByteSize}[IECByteSize.html] values.
21
+ #
22
+ #
23
+ # === Examples of use
24
+ #
25
+ # require 'bytesize/units'
26
+ #
27
+ # 1.21.gib #=> (1.21 GiB)
28
+ #
29
+ # 3.tb #=> (3 TB)
30
+ #
31
+ # 100.gb #=> (100 GB)
32
+ #
33
+ # 42.gib.to_mib #=> 43008.0
34
+ #
35
+ # 22.gib.to_si #=>
36
+ #
37
+ class Numeric
38
+
39
+
40
+
41
+ # :stopdoc:
42
+ ByteSize::SI_ORDERS_OF_MAGNITUDE.keys.each do |k|
43
+ define_method(k.downcase) do
44
+ ByteSize.send( k.downcase, self )
45
+ end
46
+ end
47
+
48
+ IECByteSize::UNIT_SYMBOLS.keys.reject{|k| k == :bytes }.each do |k|
49
+ define_method(k.downcase) do
50
+ IECByteSize.send( k.downcase, self )
51
+ end
52
+ end
53
+ # :startdoc:
54
+
55
+
56
+
57
+ ##
58
+ # :call-seq:
59
+ # bytes -> bytesize
60
+ #
61
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of bytes.
62
+ #
63
+ # :method: bytes
64
+
65
+
66
+
67
+ ##
68
+ # :call-seq:
69
+ # kb -> bytesize
70
+ #
71
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of kilobytes.
72
+ #
73
+ # :method: kb
74
+
75
+
76
+
77
+ ##
78
+ # :call-seq:
79
+ # mb -> bytesize
80
+ #
81
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of megabytes.
82
+ #
83
+ # :method: mb
84
+
85
+
86
+
87
+ ##
88
+ # :call-seq:
89
+ # gb -> bytesize
90
+ #
91
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of gigabytes.
92
+ #
93
+ # :method: gb
94
+
95
+
96
+
97
+ ##
98
+ # :call-seq:
99
+ # tb -> bytesize
100
+ #
101
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of terabytes.
102
+ #
103
+ # :method: tb
104
+
105
+
106
+
107
+ ##
108
+ # :call-seq:
109
+ # pb -> bytesize
110
+ #
111
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of petabytes.
112
+ #
113
+ # :method: pb
114
+
115
+
116
+
117
+ ##
118
+ # :call-seq:
119
+ # eb -> bytesize
120
+ #
121
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of exabytes.
122
+ #
123
+ # :method: eb
124
+
125
+
126
+
127
+ ##
128
+ # :call-seq:
129
+ # zb -> bytesize
130
+ #
131
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of zettabytes.
132
+ #
133
+ # :method: zb
134
+
135
+
136
+
137
+ ##
138
+ # :call-seq:
139
+ # yb -> bytesize
140
+ #
141
+ # Returns a new instance of {ByteSize}[ByteSize.html] representing <em>self</em> number of yottabytes.
142
+ #
143
+ # :method: yb
144
+
145
+
146
+
147
+ ##
148
+ # :call-seq:
149
+ # kib -> bytesize
150
+ #
151
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of kibibytes.
152
+ #
153
+ # :method: kib
154
+
155
+
156
+
157
+ ##
158
+ # :call-seq:
159
+ # mib -> bytesize
160
+ #
161
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of mebibytes.
162
+ #
163
+ # :method: mib
164
+
165
+
166
+
167
+ ##
168
+ # :call-seq:
169
+ # gib -> bytesize
170
+ #
171
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of gibibytes.
172
+ #
173
+ # :method: gib
174
+
175
+
176
+
177
+ ##
178
+ # :call-seq:
179
+ # tib -> bytesize
180
+ #
181
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of tebibytes.
182
+ #
183
+ # :method: tib
184
+
185
+
186
+
187
+ ##
188
+ # :call-seq:
189
+ # pib -> bytesize
190
+ #
191
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of pebibytes.
192
+ #
193
+ # :method: pib
194
+
195
+
196
+
197
+ ##
198
+ # :call-seq:
199
+ # eib -> bytesize
200
+ #
201
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of exbibytes.
202
+ #
203
+ # :method: eib
204
+
205
+
206
+
207
+ ##
208
+ # :call-seq:
209
+ # zib -> bytesize
210
+ #
211
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of zebibytes.
212
+ #
213
+ # :method: zib
214
+
215
+
216
+
217
+ ##
218
+ # :call-seq:
219
+ # yib -> bytesize
220
+ #
221
+ # Returns a new instance of {IECByteSize}[IECByteSize.html] representing <em>self</em> number of yobibytes.
222
+ #
223
+ # :method: yib
224
+
225
+
226
+
227
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby -w
2
+ #
3
+ # ByteSize
4
+ #
5
+ # version.rb
6
+ #
7
+ # © 2018 Adam Hunt
8
+ # Created: 2018-03-24
9
+ # Modified: 2018-03-24
10
+ #
11
+
12
+
13
+
14
+ class ByteSize
15
+ VERSION = "0.1.0".freeze
16
+ end
17
+
18
+
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby -w
2
+ #
3
+ # ByteSize
4
+ #
5
+ # Spec
6
+ #
7
+ # © 2018 Adam Hunt
8
+ # Created: 2018-04-07
9
+ # Modified: 2018-04-07
10
+ #
11
+
12
+
13
+
14
+ require 'rspec'
15
+ require 'test_enum'
16
+ require 'shared_examples'
17
+
18
+ require 'bytesize'
19
+
20
+
21
+
22
+ describe ByteSize do
23
+ TestEnum.new( base:1000 ).each do |value|
24
+
25
+ it_behaves_like "a byte size", value
26
+
27
+ #include_examples "compares to an instance of equal value correctly", new_class:IECByteSize
28
+
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby -w
2
+ #
3
+ # ByteSize
4
+ #
5
+ # Spec
6
+ #
7
+ # © 2018 Adam Hunt
8
+ # Created: 2018-04-07
9
+ # Modified: 2018-04-07
10
+ #
11
+
12
+
13
+
14
+ require 'rspec'
15
+ require 'test_enum'
16
+ require 'shared_examples'
17
+
18
+ require 'bytesize'
19
+
20
+
21
+
22
+ describe IECByteSize do
23
+ TestEnum.new( base:1024 ).each do |value|
24
+
25
+ include_examples "a byte size", value
26
+
27
+ #include_examples "compares to an instance of equal value correctly", new_class:ByteSize
28
+
29
+ end
30
+ end
@@ -0,0 +1,408 @@
1
+ #!/usr/bin/env ruby -w
2
+ #
3
+ # ByteSize
4
+ #
5
+ # Shared Examples
6
+ #
7
+ # © 2018 Adam Hunt
8
+ # Created: 2018-04-07
9
+ # Modified: 2018-04-07
10
+ #
11
+
12
+
13
+
14
+ require 'value_class_helper'
15
+
16
+
17
+
18
+ shared_examples "a byte size" do |value|
19
+ context "and" do
20
+
21
+ let(:instance){ described_class.new(value) }
22
+
23
+ let(:i){ instance.to_i }
24
+
25
+
26
+ ###
27
+ # Initialization
28
+ ###
29
+
30
+ context "initialized from an integer" do
31
+ it "returns an instance of the correct class" do
32
+ expect(instance.class).to be(described_class)
33
+ end
34
+ it "stores and returns the correct value" do
35
+ expect(i).to eq(value)
36
+ end
37
+ end
38
+
39
+
40
+ ###
41
+ # Type Conversion
42
+ ###
43
+
44
+ describe "#to_i" do
45
+ it "returns an integer" do
46
+ expect(i.class).to be(Integer)
47
+ end
48
+ end
49
+
50
+
51
+ ###
52
+ # Unit Conversion
53
+ ###
54
+
55
+ describe "#bytes" do
56
+ let(:v){ instance.bytes }
57
+ it "returns the correct value in bytes" do
58
+ expect(v).to eq(value)
59
+ end
60
+ it "returns an integer" do
61
+ expect(v.class).to eq(Integer)
62
+ end
63
+ end
64
+
65
+ describe "#to_bytes" do
66
+ let(:v){ instance.to_bytes }
67
+ it "returns the correct value in bytes" do
68
+ expect(v).to eq(value)
69
+ end
70
+ it "returns an integer" do
71
+ expect(v.class).to eq(Integer)
72
+ end
73
+ end
74
+
75
+ describe "#to_kb" do
76
+ let(:v){ instance.to_kb }
77
+ it "returns the correct value in kilobytes" do
78
+ expect(v).to be( value.to_f / 1000 )
79
+ end
80
+ it "returns an float" do
81
+ expect(v.class).to eq(Float)
82
+ end
83
+ end
84
+
85
+ describe "#to_kib" do
86
+ let(:v){ instance.to_kib }
87
+ it "returns the correct value in kibibyte" do
88
+ expect(v).to be( value.to_f / 1024 )
89
+ end
90
+ it "returns an float" do
91
+ expect(v.class).to eq(Float)
92
+ end
93
+ end
94
+
95
+ describe "#to_mb" do
96
+ let(:v){ instance.to_mb }
97
+ it "returns the correct value in megabytes" do
98
+ expect(v).to be( value.to_f / 1000000 )
99
+ end
100
+ it "returns an float" do
101
+ expect(v.class).to eq(Float)
102
+ end
103
+ end
104
+
105
+ describe "#to_mib" do
106
+ let(:v){ instance.to_mib }
107
+ it "returns the correct value in mebibytes" do
108
+ expect(v).to be( value.to_f / 1048576 )
109
+ end
110
+ it "returns an float" do
111
+ expect(v.class).to eq(Float)
112
+ end
113
+ end
114
+
115
+ describe "#to_gb" do
116
+ let(:v){ instance.to_gb }
117
+ it "returns the correct value in gigabytes" do
118
+ expect(v).to be( value.to_f / 1000000000 )
119
+ end
120
+ it "returns an float" do
121
+ expect(v.class).to eq(Float)
122
+ end
123
+ end
124
+
125
+ describe "#to_gib" do
126
+ let(:v){ instance.to_gib }
127
+ it "returns the correct value in gibibytes" do
128
+ expect(v).to be( value.to_f / 1073741824 )
129
+ end
130
+ it "returns an float" do
131
+ expect(v.class).to eq(Float)
132
+ end
133
+ end
134
+
135
+ describe "#to_tb" do
136
+ let(:v){ instance.to_tb }
137
+ it "returns the correct value in terabytes" do
138
+ expect(v).to be( value.to_f / 1000000000000 )
139
+ end
140
+ it "returns an float" do
141
+ expect(v.class).to eq(Float)
142
+ end
143
+ end
144
+
145
+ describe "#to_tib" do
146
+ let(:v){ instance.to_tib }
147
+ it "returns the correct value in tebibytes" do
148
+ expect(v).to be( value.to_f / 1099511627776 )
149
+ end
150
+ it "returns an float" do
151
+ expect(v.class).to eq(Float)
152
+ end
153
+ end
154
+
155
+ describe "#to_pb" do
156
+ let(:v){ instance.to_pb }
157
+ it "returns the correct value in petabytes" do
158
+ expect(v).to be( value.to_f / 1000000000000000 )
159
+ end
160
+ it "returns an float" do
161
+ expect(v.class).to eq(Float)
162
+ end
163
+ end
164
+
165
+ describe "#to_pib" do
166
+ let(:v){ instance.to_pib }
167
+ it "returns the correct value in pebibytes" do
168
+ expect(v).to be( value.to_f / 1125899906842624 )
169
+ end
170
+ it "returns an float" do
171
+ expect(v.class).to eq(Float)
172
+ end
173
+ end
174
+
175
+ describe "#to_eb" do
176
+ let(:v){ instance.to_eb }
177
+ it "returns the correct value in exabytes" do
178
+ expect(v).to be( value.to_f / 1000000000000000000 )
179
+ end
180
+ it "returns an float" do
181
+ expect(v.class).to eq(Float)
182
+ end
183
+ end
184
+
185
+ describe "#to_eib" do
186
+ let(:v){ instance.to_eib }
187
+ it "returns the correct value in exbibytes" do
188
+ expect(v).to be( value.to_f / 1152921504606846976 )
189
+ end
190
+ it "returns an float" do
191
+ expect(v.class).to eq(Float)
192
+ end
193
+ end
194
+
195
+ describe "#to_zb" do
196
+ let(:v){ instance.to_zb }
197
+ it "returns the correct value in zettabytes" do
198
+ expect(v).to be( value.to_f / 1000000000000000000000 )
199
+ end
200
+ it "returns an float" do
201
+ expect(v.class).to eq(Float)
202
+ end
203
+ end
204
+
205
+ describe "#to_zib" do
206
+ let(:v){ instance.to_zib }
207
+ it "returns the correct value in zebibytes" do
208
+ expect(v).to be( value.to_f / 1180591620717411303424 )
209
+ end
210
+ it "returns an float" do
211
+ expect(v.class).to eq(Float)
212
+ end
213
+ end
214
+
215
+ describe "#to_yb" do
216
+ let(:v){ instance.to_yb }
217
+ it "returns the correct value in yottabytes" do
218
+ expect(v).to be( value.to_f / 1000000000000000000000000 )
219
+ end
220
+ it "returns an float" do
221
+ expect(v.class).to eq(Float)
222
+ end
223
+ end
224
+
225
+ describe "#to_yib" do
226
+ let(:v){ instance.to_yib }
227
+ it "returns the correct value in yobibytes" do
228
+ expect(v).to be( value.to_f / 1208925819614629174706176 )
229
+ end
230
+ it "returns an float" do
231
+ expect(v.class).to eq(Float)
232
+ end
233
+ end
234
+
235
+
236
+ ###
237
+ # Comparison
238
+ ###
239
+
240
+ include_examples "compares to itself correctly"
241
+
242
+ include_examples "compares to a duplicate instance correctly"
243
+
244
+ include_examples "compares to an instance of equal value correctly"
245
+
246
+ #include_examples "compares to an equal numeric correctly", value:value.to_i
247
+
248
+ #include_examples "compares to an equal numeric correctly", value:value.to_r
249
+
250
+ #include_examples "compares to an equal float correctly", value
251
+
252
+ #larger_value = rand(value+1..1024**9)
253
+
254
+ #include_examples "compares to a larger numeric correctly", value, larger_value.to_i
255
+
256
+ #include_examples "compares to a larger numeric correctly", value, larger_value.to_r
257
+
258
+ #include_examples "compares to a larger float correctly", value, larger_value
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+ # larger_value = rand(value+1..1024**9)
270
+
271
+ # context "when compared to a larger instance (#{larger_value} bytes)" do
272
+ # let(:a){ instance }
273
+ # let(:b){ described_class.new(larger_value) }
274
+ # include_examples "correct comparison", {
275
+ # :== => false,
276
+ # :=== => false,
277
+ # :eql? => false,
278
+ # :equal? => false,
279
+ # :<=> => -1,
280
+ # :> => false,
281
+ # :< => true,
282
+ # :>= => false,
283
+ # :<= => true
284
+ # }
285
+ # end
286
+
287
+ # context "when a larger instance (#{larger_value} bytes) is compared to it" do
288
+ # let(:a){ described_class.new(larger_value) }
289
+ # let(:b){ instance }
290
+ # include_examples "correct comparison", {
291
+ # :== => false,
292
+ # :=== => false,
293
+ # :eql? => false,
294
+ # :equal? => false,
295
+ # :<=> => 1,
296
+ # :> => true,
297
+ # :< => false,
298
+ # :>= => true,
299
+ # :<= => false
300
+ # }
301
+ # end
302
+
303
+ # context "when compared to a larger integer (#{larger_value.to_i})" do
304
+ # let(:a){ instance }
305
+ # let(:b){ larger_value.to_i }
306
+ # include_examples "correct comparison", {
307
+ # :== => false,
308
+ # :=== => false,
309
+ # :eql? => false,
310
+ # :equal? => false,
311
+ # :<=> => -1,
312
+ # :> => false,
313
+ # :< => true,
314
+ # :>= => false,
315
+ # :<= => true
316
+ # }
317
+ # end
318
+
319
+ # context "when a larger integer (#{larger_value.to_i}) is compared to it" do
320
+ # let(:a){ larger_value.to_i }
321
+ # let(:b){ instance }
322
+ # include_examples "correct comparison", {
323
+ # :== => false,
324
+ # :=== => false,
325
+ # :eql? => false,
326
+ # :equal? => false,
327
+ # :<=> => 1,
328
+ # :> => true,
329
+ # :< => false,
330
+ # :>= => true,
331
+ # :<= => false
332
+ # }
333
+ # end
334
+
335
+ # f = value.to_f
336
+ # context "when compared to a larger float (#{f})" do
337
+ # rounding_error_direction = f <=> value.to_i
338
+ # let(:a){ instance }
339
+ # let(:b){ f }
340
+ # include_examples "correct comparison", {
341
+ # :== => rounding_error_direction.zero?,
342
+ # :=== => rounding_error_direction.zero?,
343
+ # :eql? => false,
344
+ # :equal? => false,
345
+ # :<=> => -rounding_error_direction,
346
+ # :> => false,
347
+ # :< => true,
348
+ # :>= => ( rounding_error.zero? or rounding_error.positive? ),
349
+ # :<= => ( rounding_error.zero? or rounding_error.negative? )
350
+ # }
351
+ # end
352
+
353
+ # context "when an equal float is compared to it" do
354
+ # f = value.to_f
355
+ # rounding_error = value.to_i <=> value.to_f
356
+ # let(:a){ f }
357
+ # let(:b){ instance }
358
+ # include_examples "correct comparison", {
359
+ # :== => rounding_error.zero?,
360
+ # :=== => rounding_error.zero?,
361
+ # :eql? => false,
362
+ # :equal? => false,
363
+ # :<=> => rounding_error,
364
+ # :> => rounding_error.positive?,
365
+ # :< => rounding_error.negative?,
366
+ # :>= => ( rounding_error.zero? or rounding_error.positive? ),
367
+ # :<= => ( rounding_error.zero? or rounding_error.negative? )
368
+ # }
369
+ # end
370
+
371
+ # context "when compared to an equal rational" do
372
+ # let(:a){ instance }
373
+ # let(:b){ value.to_r }
374
+ # include_examples "correct comparison", {
375
+ # :== => true,
376
+ # :=== => true,
377
+ # :eql? => false,
378
+ # :equal? => false,
379
+ # :<=> => 0,
380
+ # :> => false,
381
+ # :< => false,
382
+ # :>= => true,
383
+ # :<= => true
384
+ # }
385
+ # end
386
+
387
+ # context "when an equal rational is compared to it" do
388
+ # let(:a){ value.to_r }
389
+ # let(:b){ instance }
390
+ # include_examples "correct comparison", {
391
+ # :== => true,
392
+ # :=== => true,
393
+ # :eql? => false,
394
+ # :equal? => false,
395
+ # :<=> => 0,
396
+ # :> => false,
397
+ # :< => false,
398
+ # :>= => true,
399
+ # :<= => true
400
+ # }
401
+ # end
402
+
403
+
404
+
405
+ include_examples "correctly reports positive, negative, and zero", value
406
+
407
+ end
408
+ end