blackbox 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +2 -0
- data/Guardfile +16 -0
- data/Makefile +24 -0
- data/blackbox.gemspec +4 -0
- data/coverage/.last_run.json +1 -1
- data/coverage/.resultset.json +171 -2
- data/coverage/index.html +1064 -34
- data/doc/BB.html +5 -5
- data/doc/BB/Crypto.html +21 -21
- data/doc/BB/Hash.html +4 -4
- data/doc/BB/String.html +2 -2
- data/doc/_index.html +17 -2
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +2 -2
- data/doc/index.html +2 -2
- data/doc/method_list.html +24 -0
- data/doc/top-level-namespace.html +2 -2
- data/lib/blackbox/crypto.rb +24 -24
- data/lib/blackbox/number.rb +167 -0
- data/lib/blackbox/version.rb +1 -1
- data/spec/blackbox_spec.rb +1 -1
- data/spec/crypto_spec.rb +4 -4
- data/spec/hash_spec.rb +4 -4
- data/spec/number_spec.rb +93 -0
- data/spec/string_spec.rb +1 -1
- metadata +65 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdaefa7436a7c8a81c9451794fb09b6f70ab7c3c
|
4
|
+
data.tar.gz: 403ccd62b87e73ecc8638bdbbddc5b27949c671e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 540618ca3b6c4995042203b2a66e11b8f76da4edf996ce04f1a4a64ccd411d74574b56c163bfd9634d3b03fb0f3b3faf191a0b6b98ec695592060b294474c841
|
7
|
+
data.tar.gz: f7ba66a6d44011d21125ce3657bb09abaaa1403bf6042f650a72fef3248a574ebc5371c0786dd9655b763677d234b9f6d247bed0d8f90155c512a9808054d489
|
data/.gitattributes
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
guard :rspec, cmd: "bundle exec rspec -f Fuubar" do
|
2
|
+
require "guard/rspec/dsl"
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
4
|
+
|
5
|
+
# Feel free to open issues for suggestions and improvements
|
6
|
+
|
7
|
+
# RSpec files
|
8
|
+
rspec = dsl.rspec
|
9
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
10
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
11
|
+
watch(rspec.spec_files)
|
12
|
+
|
13
|
+
# Ruby files
|
14
|
+
ruby = dsl.ruby
|
15
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
16
|
+
end
|
data/Makefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# 0. make test
|
2
|
+
# 1. commit changes
|
3
|
+
# 2. bump
|
4
|
+
# 3. make release
|
5
|
+
|
6
|
+
.PHONY: test doc release
|
7
|
+
|
8
|
+
test:
|
9
|
+
bundle exec rake
|
10
|
+
|
11
|
+
doc:
|
12
|
+
bundle exec yardoc
|
13
|
+
|
14
|
+
push: test doc
|
15
|
+
git commit -m 'Documentation update' doc coverage README.md
|
16
|
+
git checkout gh-pages
|
17
|
+
git checkout master -- doc coverage
|
18
|
+
git commit -m 'Documentation update'
|
19
|
+
git checkout master
|
20
|
+
git push origin gh-pages
|
21
|
+
|
22
|
+
release: test doc push
|
23
|
+
bundle exec rake release
|
24
|
+
|
data/blackbox.gemspec
CHANGED
@@ -23,4 +23,8 @@ Gem::Specification.new do |gem|
|
|
23
23
|
gem.add_development_dependency "simplecov"
|
24
24
|
gem.add_development_dependency "redcarpet"
|
25
25
|
gem.add_development_dependency "yard"
|
26
|
+
gem.add_development_dependency "bump"
|
27
|
+
gem.add_development_dependency "guard"
|
28
|
+
gem.add_development_dependency "guard-rspec"
|
29
|
+
gem.add_development_dependency "fuubar"
|
26
30
|
end
|
data/coverage/.last_run.json
CHANGED
data/coverage/.resultset.json
CHANGED
@@ -112,7 +112,7 @@
|
|
112
112
|
null,
|
113
113
|
null,
|
114
114
|
1,
|
115
|
-
|
115
|
+
100,
|
116
116
|
null,
|
117
117
|
null,
|
118
118
|
null,
|
@@ -153,6 +153,175 @@
|
|
153
153
|
null,
|
154
154
|
null
|
155
155
|
],
|
156
|
+
"/home/moe/code/blackbox/lib/blackbox/number.rb": [
|
157
|
+
1,
|
158
|
+
null,
|
159
|
+
1,
|
160
|
+
null,
|
161
|
+
1,
|
162
|
+
1,
|
163
|
+
1,
|
164
|
+
null,
|
165
|
+
null,
|
166
|
+
null,
|
167
|
+
null,
|
168
|
+
null,
|
169
|
+
null,
|
170
|
+
null,
|
171
|
+
null,
|
172
|
+
null,
|
173
|
+
null,
|
174
|
+
null,
|
175
|
+
null,
|
176
|
+
null,
|
177
|
+
null,
|
178
|
+
null,
|
179
|
+
null,
|
180
|
+
null,
|
181
|
+
null,
|
182
|
+
null,
|
183
|
+
null,
|
184
|
+
null,
|
185
|
+
null,
|
186
|
+
null,
|
187
|
+
null,
|
188
|
+
null,
|
189
|
+
null,
|
190
|
+
null,
|
191
|
+
null,
|
192
|
+
null,
|
193
|
+
null,
|
194
|
+
null,
|
195
|
+
null,
|
196
|
+
null,
|
197
|
+
null,
|
198
|
+
null,
|
199
|
+
null,
|
200
|
+
null,
|
201
|
+
1,
|
202
|
+
11,
|
203
|
+
null,
|
204
|
+
11,
|
205
|
+
null,
|
206
|
+
11,
|
207
|
+
11,
|
208
|
+
11,
|
209
|
+
11,
|
210
|
+
11,
|
211
|
+
null,
|
212
|
+
11,
|
213
|
+
null,
|
214
|
+
1,
|
215
|
+
null,
|
216
|
+
10,
|
217
|
+
10,
|
218
|
+
10,
|
219
|
+
10,
|
220
|
+
10,
|
221
|
+
null,
|
222
|
+
10,
|
223
|
+
null,
|
224
|
+
10,
|
225
|
+
10,
|
226
|
+
10,
|
227
|
+
null,
|
228
|
+
null,
|
229
|
+
null,
|
230
|
+
null,
|
231
|
+
10,
|
232
|
+
0,
|
233
|
+
0,
|
234
|
+
null,
|
235
|
+
null,
|
236
|
+
null,
|
237
|
+
null,
|
238
|
+
null,
|
239
|
+
null,
|
240
|
+
null,
|
241
|
+
null,
|
242
|
+
null,
|
243
|
+
null,
|
244
|
+
null,
|
245
|
+
null,
|
246
|
+
null,
|
247
|
+
null,
|
248
|
+
null,
|
249
|
+
null,
|
250
|
+
null,
|
251
|
+
null,
|
252
|
+
null,
|
253
|
+
null,
|
254
|
+
null,
|
255
|
+
null,
|
256
|
+
null,
|
257
|
+
null,
|
258
|
+
null,
|
259
|
+
null,
|
260
|
+
null,
|
261
|
+
null,
|
262
|
+
null,
|
263
|
+
null,
|
264
|
+
null,
|
265
|
+
1,
|
266
|
+
11,
|
267
|
+
null,
|
268
|
+
11,
|
269
|
+
11,
|
270
|
+
11,
|
271
|
+
null,
|
272
|
+
11,
|
273
|
+
11,
|
274
|
+
11,
|
275
|
+
null,
|
276
|
+
null,
|
277
|
+
null,
|
278
|
+
0,
|
279
|
+
null,
|
280
|
+
null,
|
281
|
+
null,
|
282
|
+
null,
|
283
|
+
null,
|
284
|
+
null,
|
285
|
+
null,
|
286
|
+
null,
|
287
|
+
null,
|
288
|
+
null,
|
289
|
+
null,
|
290
|
+
null,
|
291
|
+
null,
|
292
|
+
null,
|
293
|
+
null,
|
294
|
+
null,
|
295
|
+
null,
|
296
|
+
null,
|
297
|
+
null,
|
298
|
+
null,
|
299
|
+
null,
|
300
|
+
null,
|
301
|
+
null,
|
302
|
+
null,
|
303
|
+
null,
|
304
|
+
null,
|
305
|
+
null,
|
306
|
+
1,
|
307
|
+
12,
|
308
|
+
null,
|
309
|
+
12,
|
310
|
+
12,
|
311
|
+
null,
|
312
|
+
12,
|
313
|
+
12,
|
314
|
+
12,
|
315
|
+
12,
|
316
|
+
null,
|
317
|
+
0,
|
318
|
+
null,
|
319
|
+
null,
|
320
|
+
null,
|
321
|
+
null,
|
322
|
+
null,
|
323
|
+
null
|
324
|
+
],
|
156
325
|
"/home/moe/code/blackbox/lib/blackbox/string.rb": [
|
157
326
|
1,
|
158
327
|
null,
|
@@ -173,6 +342,6 @@
|
|
173
342
|
null
|
174
343
|
]
|
175
344
|
},
|
176
|
-
"timestamp":
|
345
|
+
"timestamp": 1448213121
|
177
346
|
}
|
178
347
|
}
|
data/coverage/index.html
CHANGED
@@ -14,27 +14,27 @@
|
|
14
14
|
<img src="./assets/0.7.1/loading.gif" alt="loading"/>
|
15
15
|
</div>
|
16
16
|
<div id="wrapper" style="display:none;">
|
17
|
-
<div class="timestamp">Generated <abbr class="timeago" title="
|
17
|
+
<div class="timestamp">Generated <abbr class="timeago" title="2015-11-22T18:25:21+01:00">2015-11-22T18:25:21+01:00</abbr></div>
|
18
18
|
<ul class="group_tabs"></ul>
|
19
19
|
|
20
20
|
<div id="content">
|
21
21
|
<div class="file_list_container" id="AllFiles">
|
22
22
|
<h2>
|
23
23
|
<span class="group_name">All Files</span>
|
24
|
-
(<span class="covered_percent"><span class="green">
|
24
|
+
(<span class="covered_percent"><span class="green">95.79%</span></span>
|
25
25
|
covered at
|
26
26
|
<span class="covered_strength">
|
27
27
|
<span class="green">
|
28
|
-
|
28
|
+
65.07
|
29
29
|
</span>
|
30
30
|
</span> hits/line)
|
31
31
|
</h2>
|
32
32
|
<a name="AllFiles"></a>
|
33
33
|
<div>
|
34
|
-
<b>
|
35
|
-
<b>
|
36
|
-
<span class="green"><b>
|
37
|
-
<span class="red"><b>
|
34
|
+
<b>4</b> files in total.
|
35
|
+
<b>95</b> relevant lines.
|
36
|
+
<span class="green"><b>91</b> lines covered</span> and
|
37
|
+
<span class="red"><b>4</b> lines missed </span>
|
38
38
|
</div>
|
39
39
|
<table class="file_list">
|
40
40
|
<thead>
|
@@ -67,7 +67,17 @@
|
|
67
67
|
<td>13</td>
|
68
68
|
<td>13</td>
|
69
69
|
<td>0</td>
|
70
|
-
<td>7
|
70
|
+
<td>14.7</td>
|
71
|
+
</tr>
|
72
|
+
|
73
|
+
<tr>
|
74
|
+
<td class="strong"><a href="#7a135b0407697dbb00ab589a4ca6477d724b7276" class="src_link" title="lib/blackbox/number.rb">lib/blackbox/number.rb</a></td>
|
75
|
+
<td class="green strong">91.11 %</td>
|
76
|
+
<td>167</td>
|
77
|
+
<td>45</td>
|
78
|
+
<td>41</td>
|
79
|
+
<td>4</td>
|
80
|
+
<td>8.0</td>
|
71
81
|
</tr>
|
72
82
|
|
73
83
|
<tr>
|
@@ -149,7 +159,7 @@
|
|
149
159
|
<li class="covered" data-hits="1" data-linenumber="7">
|
150
160
|
<span class="hits">1</span>
|
151
161
|
|
152
|
-
<code class="ruby"> class << self
|
162
|
+
<code class="ruby"> class << self</code>
|
153
163
|
</li>
|
154
164
|
|
155
165
|
<li class="never" data-hits="" data-linenumber="8">
|
@@ -203,25 +213,25 @@
|
|
203
213
|
<li class="covered" data-hits="1" data-linenumber="16">
|
204
214
|
<span class="hits">1</span>
|
205
215
|
|
206
|
-
<code class="ruby"> def encrypt(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
216
|
+
<code class="ruby"> def encrypt(plaintext, key, cipher_type='aes-256-cbc', iv=nil)</code>
|
207
217
|
</li>
|
208
218
|
|
209
219
|
<li class="covered" data-hits="697" data-linenumber="17">
|
210
220
|
<span class="hits">697</span>
|
211
221
|
|
212
|
-
<code class="ruby">
|
222
|
+
<code class="ruby"> cipher = OpenSSL::Cipher::Cipher.new(cipher_type)</code>
|
213
223
|
</li>
|
214
224
|
|
215
225
|
<li class="covered" data-hits="697" data-linenumber="18">
|
216
226
|
<span class="hits">697</span>
|
217
227
|
|
218
|
-
<code class="ruby">
|
228
|
+
<code class="ruby"> cipher.encrypt</code>
|
219
229
|
</li>
|
220
230
|
|
221
231
|
<li class="covered" data-hits="697" data-linenumber="19">
|
222
232
|
<span class="hits">697</span>
|
223
233
|
|
224
|
-
<code class="ruby">
|
234
|
+
<code class="ruby"> cipher.key = key</code>
|
225
235
|
</li>
|
226
236
|
|
227
237
|
<li class="covered" data-hits="697" data-linenumber="20">
|
@@ -233,13 +243,13 @@
|
|
233
243
|
<li class="covered" data-hits="328" data-linenumber="21">
|
234
244
|
<span class="hits">328</span>
|
235
245
|
|
236
|
-
<code class="ruby"> iv =
|
246
|
+
<code class="ruby"> iv = cipher.random_iv</code>
|
237
247
|
</li>
|
238
248
|
|
239
249
|
<li class="covered" data-hits="328" data-linenumber="22">
|
240
250
|
<span class="hits">328</span>
|
241
251
|
|
242
|
-
<code class="ruby"> [iv.length].pack('C') + iv +
|
252
|
+
<code class="ruby"> [iv.length].pack('C') + iv + cipher.update(plaintext) + cipher.final</code>
|
243
253
|
</li>
|
244
254
|
|
245
255
|
<li class="never" data-hits="" data-linenumber="23">
|
@@ -251,13 +261,13 @@
|
|
251
261
|
<li class="covered" data-hits="369" data-linenumber="24">
|
252
262
|
<span class="hits">369</span>
|
253
263
|
|
254
|
-
<code class="ruby">
|
264
|
+
<code class="ruby"> cipher.iv = iv</code>
|
255
265
|
</li>
|
256
266
|
|
257
267
|
<li class="covered" data-hits="369" data-linenumber="25">
|
258
268
|
<span class="hits">369</span>
|
259
269
|
|
260
|
-
<code class="ruby">
|
270
|
+
<code class="ruby"> cipher.update(plaintext) + cipher.final</code>
|
261
271
|
</li>
|
262
272
|
|
263
273
|
<li class="never" data-hits="" data-linenumber="26">
|
@@ -269,7 +279,7 @@
|
|
269
279
|
<li class="never" data-hits="" data-linenumber="27">
|
270
280
|
|
271
281
|
|
272
|
-
<code class="ruby"> end
|
282
|
+
<code class="ruby"> end</code>
|
273
283
|
</li>
|
274
284
|
|
275
285
|
<li class="never" data-hits="" data-linenumber="28">
|
@@ -323,25 +333,25 @@
|
|
323
333
|
<li class="covered" data-hits="1" data-linenumber="36">
|
324
334
|
<span class="hits">1</span>
|
325
335
|
|
326
|
-
<code class="ruby"> def decrypt(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
336
|
+
<code class="ruby"> def decrypt(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)</code>
|
327
337
|
</li>
|
328
338
|
|
329
339
|
<li class="covered" data-hits="369" data-linenumber="37">
|
330
340
|
<span class="hits">369</span>
|
331
341
|
|
332
|
-
<code class="ruby">
|
342
|
+
<code class="ruby"> cipher = OpenSSL::Cipher::Cipher.new(cipher_type)</code>
|
333
343
|
</li>
|
334
344
|
|
335
345
|
<li class="covered" data-hits="369" data-linenumber="38">
|
336
346
|
<span class="hits">369</span>
|
337
347
|
|
338
|
-
<code class="ruby">
|
348
|
+
<code class="ruby"> cipher.decrypt</code>
|
339
349
|
</li>
|
340
350
|
|
341
351
|
<li class="covered" data-hits="369" data-linenumber="39">
|
342
352
|
<span class="hits">369</span>
|
343
353
|
|
344
|
-
<code class="ruby">
|
354
|
+
<code class="ruby"> cipher.key = key</code>
|
345
355
|
</li>
|
346
356
|
|
347
357
|
<li class="covered" data-hits="369" data-linenumber="40">
|
@@ -365,7 +375,7 @@
|
|
365
375
|
<li class="covered" data-hits="180" data-linenumber="43">
|
366
376
|
<span class="hits">180</span>
|
367
377
|
|
368
|
-
<code class="ruby">
|
378
|
+
<code class="ruby"> cipher.iv = ciphertext.slice!(0..iv_len-1)</code>
|
369
379
|
</li>
|
370
380
|
|
371
381
|
<li class="never" data-hits="" data-linenumber="44">
|
@@ -383,7 +393,7 @@
|
|
383
393
|
<li class="covered" data-hits="123" data-linenumber="46">
|
384
394
|
<span class="hits">123</span>
|
385
395
|
|
386
|
-
<code class="ruby">
|
396
|
+
<code class="ruby"> cipher.iv = iv</code>
|
387
397
|
</li>
|
388
398
|
|
389
399
|
<li class="never" data-hits="" data-linenumber="47">
|
@@ -395,19 +405,19 @@
|
|
395
405
|
<li class="covered" data-hits="369" data-linenumber="48">
|
396
406
|
<span class="hits">369</span>
|
397
407
|
|
398
|
-
<code class="ruby">
|
408
|
+
<code class="ruby"> cipher.update(ciphertext) + cipher.final</code>
|
399
409
|
</li>
|
400
410
|
|
401
411
|
<li class="never" data-hits="" data-linenumber="49">
|
402
412
|
|
403
413
|
|
404
|
-
<code class="ruby"> end
|
414
|
+
<code class="ruby"> end</code>
|
405
415
|
</li>
|
406
416
|
|
407
417
|
<li class="never" data-hits="" data-linenumber="50">
|
408
418
|
|
409
419
|
|
410
|
-
<code class="ruby"
|
420
|
+
<code class="ruby"></code>
|
411
421
|
</li>
|
412
422
|
|
413
423
|
<li class="never" data-hits="" data-linenumber="51">
|
@@ -461,7 +471,7 @@
|
|
461
471
|
<li class="covered" data-hits="1" data-linenumber="59">
|
462
472
|
<span class="hits">1</span>
|
463
473
|
|
464
|
-
<code class="ruby"> def encrypt_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
474
|
+
<code class="ruby"> def encrypt_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)</code>
|
465
475
|
</li>
|
466
476
|
|
467
477
|
<li class="covered" data-hits="246" data-linenumber="60">
|
@@ -527,7 +537,7 @@
|
|
527
537
|
<li class="covered" data-hits="1" data-linenumber="70">
|
528
538
|
<span class="hits">1</span>
|
529
539
|
|
530
|
-
<code class="ruby"> def decrypt_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
540
|
+
<code class="ruby"> def decrypt_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)</code>
|
531
541
|
</li>
|
532
542
|
|
533
543
|
<li class="covered" data-hits="123" data-linenumber="71">
|
@@ -599,7 +609,7 @@
|
|
599
609
|
<li class="covered" data-hits="1" data-linenumber="82">
|
600
610
|
<span class="hits">1</span>
|
601
611
|
|
602
|
-
<code class="ruby"> def encrypt_urlsafe_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
612
|
+
<code class="ruby"> def encrypt_urlsafe_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)</code>
|
603
613
|
</li>
|
604
614
|
|
605
615
|
<li class="covered" data-hits="246" data-linenumber="83">
|
@@ -665,7 +675,7 @@
|
|
665
675
|
<li class="covered" data-hits="1" data-linenumber="93">
|
666
676
|
<span class="hits">1</span>
|
667
677
|
|
668
|
-
<code class="ruby"> def decrypt_urlsafe_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
678
|
+
<code class="ruby"> def decrypt_urlsafe_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)</code>
|
669
679
|
</li>
|
670
680
|
|
671
681
|
<li class="covered" data-hits="123" data-linenumber="94">
|
@@ -683,7 +693,7 @@
|
|
683
693
|
<li class="never" data-hits="" data-linenumber="96">
|
684
694
|
|
685
695
|
|
686
|
-
<code class="ruby"> end
|
696
|
+
<code class="ruby"> end</code>
|
687
697
|
</li>
|
688
698
|
|
689
699
|
<li class="never" data-hits="" data-linenumber="97">
|
@@ -776,8 +786,8 @@
|
|
776
786
|
<code class="ruby"> def symbolize_keys(hash)</code>
|
777
787
|
</li>
|
778
788
|
|
779
|
-
<li class="covered" data-hits="
|
780
|
-
<span class="hits">
|
789
|
+
<li class="covered" data-hits="100" data-linenumber="10">
|
790
|
+
<span class="hits">100</span>
|
781
791
|
|
782
792
|
<code class="ruby"> hash.each_with_object({}){|(k,v), h| h[k.to_sym] = v}</code>
|
783
793
|
</li>
|
@@ -1020,6 +1030,1026 @@
|
|
1020
1030
|
</pre>
|
1021
1031
|
</div>
|
1022
1032
|
|
1033
|
+
<div class="source_table" id="7a135b0407697dbb00ab589a4ca6477d724b7276">
|
1034
|
+
<div class="header">
|
1035
|
+
<h3>lib/blackbox/number.rb</h3>
|
1036
|
+
<h4><span class="green">91.11 %</span> covered</h4>
|
1037
|
+
<div>
|
1038
|
+
<b>45</b> relevant lines.
|
1039
|
+
<span class="green"><b>41</b> lines covered</span> and
|
1040
|
+
<span class="red"><b>4</b> lines missed.</span>
|
1041
|
+
</div>
|
1042
|
+
</div>
|
1043
|
+
|
1044
|
+
<pre>
|
1045
|
+
<ol>
|
1046
|
+
|
1047
|
+
<li class="covered" data-hits="1" data-linenumber="1">
|
1048
|
+
<span class="hits">1</span>
|
1049
|
+
|
1050
|
+
<code class="ruby">require 'blackbox/hash'</code>
|
1051
|
+
</li>
|
1052
|
+
|
1053
|
+
<li class="never" data-hits="" data-linenumber="2">
|
1054
|
+
|
1055
|
+
|
1056
|
+
<code class="ruby"></code>
|
1057
|
+
</li>
|
1058
|
+
|
1059
|
+
<li class="covered" data-hits="1" data-linenumber="3">
|
1060
|
+
<span class="hits">1</span>
|
1061
|
+
|
1062
|
+
<code class="ruby">module BB</code>
|
1063
|
+
</li>
|
1064
|
+
|
1065
|
+
<li class="never" data-hits="" data-linenumber="4">
|
1066
|
+
|
1067
|
+
|
1068
|
+
<code class="ruby"> # String utilities.</code>
|
1069
|
+
</li>
|
1070
|
+
|
1071
|
+
<li class="covered" data-hits="1" data-linenumber="5">
|
1072
|
+
<span class="hits">1</span>
|
1073
|
+
|
1074
|
+
<code class="ruby"> module Number</code>
|
1075
|
+
</li>
|
1076
|
+
|
1077
|
+
<li class="covered" data-hits="1" data-linenumber="6">
|
1078
|
+
<span class="hits">1</span>
|
1079
|
+
|
1080
|
+
<code class="ruby"> class << self</code>
|
1081
|
+
</li>
|
1082
|
+
|
1083
|
+
<li class="covered" data-hits="1" data-linenumber="7">
|
1084
|
+
<span class="hits">1</span>
|
1085
|
+
|
1086
|
+
<code class="ruby"> STORAGE_UNITS = ['byte', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'].freeze</code>
|
1087
|
+
</li>
|
1088
|
+
|
1089
|
+
<li class="never" data-hits="" data-linenumber="8">
|
1090
|
+
|
1091
|
+
|
1092
|
+
<code class="ruby"></code>
|
1093
|
+
</li>
|
1094
|
+
|
1095
|
+
<li class="never" data-hits="" data-linenumber="9">
|
1096
|
+
|
1097
|
+
|
1098
|
+
<code class="ruby"> ##</code>
|
1099
|
+
</li>
|
1100
|
+
|
1101
|
+
<li class="never" data-hits="" data-linenumber="10">
|
1102
|
+
|
1103
|
+
|
1104
|
+
<code class="ruby"> # Formats the bytes in +size+ into a more understandable representation</code>
|
1105
|
+
</li>
|
1106
|
+
|
1107
|
+
<li class="never" data-hits="" data-linenumber="11">
|
1108
|
+
|
1109
|
+
|
1110
|
+
<code class="ruby"> # (e.g., giving it 1500 yields 1.5 KB). This method is useful for</code>
|
1111
|
+
</li>
|
1112
|
+
|
1113
|
+
<li class="never" data-hits="" data-linenumber="12">
|
1114
|
+
|
1115
|
+
|
1116
|
+
<code class="ruby"> # reporting file sizes to users. This method returns nil if</code>
|
1117
|
+
</li>
|
1118
|
+
|
1119
|
+
<li class="never" data-hits="" data-linenumber="13">
|
1120
|
+
|
1121
|
+
|
1122
|
+
<code class="ruby"> # +size+ cannot be converted into a number. You can customize the</code>
|
1123
|
+
</li>
|
1124
|
+
|
1125
|
+
<li class="never" data-hits="" data-linenumber="14">
|
1126
|
+
|
1127
|
+
|
1128
|
+
<code class="ruby"> # format in the +options+ hash.</code>
|
1129
|
+
</li>
|
1130
|
+
|
1131
|
+
<li class="never" data-hits="" data-linenumber="15">
|
1132
|
+
|
1133
|
+
|
1134
|
+
<code class="ruby"> #</code>
|
1135
|
+
</li>
|
1136
|
+
|
1137
|
+
<li class="never" data-hits="" data-linenumber="16">
|
1138
|
+
|
1139
|
+
|
1140
|
+
<code class="ruby"> # @overload to_human_size(number, options={})</code>
|
1141
|
+
</li>
|
1142
|
+
|
1143
|
+
<li class="never" data-hits="" data-linenumber="17">
|
1144
|
+
|
1145
|
+
|
1146
|
+
<code class="ruby"> # @param [Fixnum] number</code>
|
1147
|
+
</li>
|
1148
|
+
|
1149
|
+
<li class="never" data-hits="" data-linenumber="18">
|
1150
|
+
|
1151
|
+
|
1152
|
+
<code class="ruby"> # Number value to format.</code>
|
1153
|
+
</li>
|
1154
|
+
|
1155
|
+
<li class="never" data-hits="" data-linenumber="19">
|
1156
|
+
|
1157
|
+
|
1158
|
+
<code class="ruby"> # @param [Hash] options</code>
|
1159
|
+
</li>
|
1160
|
+
|
1161
|
+
<li class="never" data-hits="" data-linenumber="20">
|
1162
|
+
|
1163
|
+
|
1164
|
+
<code class="ruby"> # Options for formatter.</code>
|
1165
|
+
</li>
|
1166
|
+
|
1167
|
+
<li class="never" data-hits="" data-linenumber="21">
|
1168
|
+
|
1169
|
+
|
1170
|
+
<code class="ruby"> # @option options [Fixnum] :precision (1)</code>
|
1171
|
+
</li>
|
1172
|
+
|
1173
|
+
<li class="never" data-hits="" data-linenumber="22">
|
1174
|
+
|
1175
|
+
|
1176
|
+
<code class="ruby"> # Sets the level of precision.</code>
|
1177
|
+
</li>
|
1178
|
+
|
1179
|
+
<li class="never" data-hits="" data-linenumber="23">
|
1180
|
+
|
1181
|
+
|
1182
|
+
<code class="ruby"> # @option options [String] :separator (".")</code>
|
1183
|
+
</li>
|
1184
|
+
|
1185
|
+
<li class="never" data-hits="" data-linenumber="24">
|
1186
|
+
|
1187
|
+
|
1188
|
+
<code class="ruby"> # Sets the separator between the units.</code>
|
1189
|
+
</li>
|
1190
|
+
|
1191
|
+
<li class="never" data-hits="" data-linenumber="25">
|
1192
|
+
|
1193
|
+
|
1194
|
+
<code class="ruby"> # @option options [String] :delimiter ("")</code>
|
1195
|
+
</li>
|
1196
|
+
|
1197
|
+
<li class="never" data-hits="" data-linenumber="26">
|
1198
|
+
|
1199
|
+
|
1200
|
+
<code class="ruby"> # Sets the thousands delimiter.</code>
|
1201
|
+
</li>
|
1202
|
+
|
1203
|
+
<li class="never" data-hits="" data-linenumber="27">
|
1204
|
+
|
1205
|
+
|
1206
|
+
<code class="ruby"> # @option options [String] :kilo (1024)</code>
|
1207
|
+
</li>
|
1208
|
+
|
1209
|
+
<li class="never" data-hits="" data-linenumber="28">
|
1210
|
+
|
1211
|
+
|
1212
|
+
<code class="ruby"> # Sets the number of bytes in a kilobyte.</code>
|
1213
|
+
</li>
|
1214
|
+
|
1215
|
+
<li class="never" data-hits="" data-linenumber="29">
|
1216
|
+
|
1217
|
+
|
1218
|
+
<code class="ruby"> # @option options [String] :format ("%n %u")</code>
|
1219
|
+
</li>
|
1220
|
+
|
1221
|
+
<li class="never" data-hits="" data-linenumber="30">
|
1222
|
+
|
1223
|
+
|
1224
|
+
<code class="ruby"> # Sets the display format.</code>
|
1225
|
+
</li>
|
1226
|
+
|
1227
|
+
<li class="never" data-hits="" data-linenumber="31">
|
1228
|
+
|
1229
|
+
|
1230
|
+
<code class="ruby"> #</code>
|
1231
|
+
</li>
|
1232
|
+
|
1233
|
+
<li class="never" data-hits="" data-linenumber="32">
|
1234
|
+
|
1235
|
+
|
1236
|
+
<code class="ruby"> # @return [String] The formatted representation of bytes</code>
|
1237
|
+
</li>
|
1238
|
+
|
1239
|
+
<li class="never" data-hits="" data-linenumber="33">
|
1240
|
+
|
1241
|
+
|
1242
|
+
<code class="ruby"> #</code>
|
1243
|
+
</li>
|
1244
|
+
|
1245
|
+
<li class="never" data-hits="" data-linenumber="34">
|
1246
|
+
|
1247
|
+
|
1248
|
+
<code class="ruby"> # @example</code>
|
1249
|
+
</li>
|
1250
|
+
|
1251
|
+
<li class="never" data-hits="" data-linenumber="35">
|
1252
|
+
|
1253
|
+
|
1254
|
+
<code class="ruby"> # to_human_size(123) # => 123 Bytes</code>
|
1255
|
+
</li>
|
1256
|
+
|
1257
|
+
<li class="never" data-hits="" data-linenumber="36">
|
1258
|
+
|
1259
|
+
|
1260
|
+
<code class="ruby"> # to_human_size(1234) # => 1.2 KB</code>
|
1261
|
+
</li>
|
1262
|
+
|
1263
|
+
<li class="never" data-hits="" data-linenumber="37">
|
1264
|
+
|
1265
|
+
|
1266
|
+
<code class="ruby"> # to_human_size(12345) # => 12.1 KB</code>
|
1267
|
+
</li>
|
1268
|
+
|
1269
|
+
<li class="never" data-hits="" data-linenumber="38">
|
1270
|
+
|
1271
|
+
|
1272
|
+
<code class="ruby"> # to_human_size(1234567) # => 1.2 MB</code>
|
1273
|
+
</li>
|
1274
|
+
|
1275
|
+
<li class="never" data-hits="" data-linenumber="39">
|
1276
|
+
|
1277
|
+
|
1278
|
+
<code class="ruby"> # to_human_size(1234567890) # => 1.1 GB</code>
|
1279
|
+
</li>
|
1280
|
+
|
1281
|
+
<li class="never" data-hits="" data-linenumber="40">
|
1282
|
+
|
1283
|
+
|
1284
|
+
<code class="ruby"> # to_human_size(1234567890123) # => 1.1 TB</code>
|
1285
|
+
</li>
|
1286
|
+
|
1287
|
+
<li class="never" data-hits="" data-linenumber="41">
|
1288
|
+
|
1289
|
+
|
1290
|
+
<code class="ruby"> # to_human_size(1234567, :precision => 2) # => 1.18 MB</code>
|
1291
|
+
</li>
|
1292
|
+
|
1293
|
+
<li class="never" data-hits="" data-linenumber="42">
|
1294
|
+
|
1295
|
+
|
1296
|
+
<code class="ruby"> # to_human_size(483989, :precision => 0) # => 473 KB</code>
|
1297
|
+
</li>
|
1298
|
+
|
1299
|
+
<li class="never" data-hits="" data-linenumber="43">
|
1300
|
+
|
1301
|
+
|
1302
|
+
<code class="ruby"> # to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB</code>
|
1303
|
+
</li>
|
1304
|
+
|
1305
|
+
<li class="never" data-hits="" data-linenumber="44">
|
1306
|
+
|
1307
|
+
|
1308
|
+
<code class="ruby"> #</code>
|
1309
|
+
</li>
|
1310
|
+
|
1311
|
+
<li class="covered" data-hits="1" data-linenumber="45">
|
1312
|
+
<span class="hits">1</span>
|
1313
|
+
|
1314
|
+
<code class="ruby"> def to_human_size(number, args={})</code>
|
1315
|
+
</li>
|
1316
|
+
|
1317
|
+
<li class="covered" data-hits="11" data-linenumber="46">
|
1318
|
+
<span class="hits">11</span>
|
1319
|
+
|
1320
|
+
<code class="ruby"> return nil if number.nil?</code>
|
1321
|
+
</li>
|
1322
|
+
|
1323
|
+
<li class="never" data-hits="" data-linenumber="47">
|
1324
|
+
|
1325
|
+
|
1326
|
+
<code class="ruby"></code>
|
1327
|
+
</li>
|
1328
|
+
|
1329
|
+
<li class="covered" data-hits="11" data-linenumber="48">
|
1330
|
+
<span class="hits">11</span>
|
1331
|
+
|
1332
|
+
<code class="ruby"> options = BB::Hash.symbolize_keys(args)</code>
|
1333
|
+
</li>
|
1334
|
+
|
1335
|
+
<li class="never" data-hits="" data-linenumber="49">
|
1336
|
+
|
1337
|
+
|
1338
|
+
<code class="ruby"></code>
|
1339
|
+
</li>
|
1340
|
+
|
1341
|
+
<li class="covered" data-hits="11" data-linenumber="50">
|
1342
|
+
<span class="hits">11</span>
|
1343
|
+
|
1344
|
+
<code class="ruby"> precision ||= (options[:precision] || 1)</code>
|
1345
|
+
</li>
|
1346
|
+
|
1347
|
+
<li class="covered" data-hits="11" data-linenumber="51">
|
1348
|
+
<span class="hits">11</span>
|
1349
|
+
|
1350
|
+
<code class="ruby"> separator ||= (options[:separator] || '.')</code>
|
1351
|
+
</li>
|
1352
|
+
|
1353
|
+
<li class="covered" data-hits="11" data-linenumber="52">
|
1354
|
+
<span class="hits">11</span>
|
1355
|
+
|
1356
|
+
<code class="ruby"> delimiter ||= (options[:delimiter] || '')</code>
|
1357
|
+
</li>
|
1358
|
+
|
1359
|
+
<li class="covered" data-hits="11" data-linenumber="53">
|
1360
|
+
<span class="hits">11</span>
|
1361
|
+
|
1362
|
+
<code class="ruby"> kilo ||= (options[:kilo] || 1024)</code>
|
1363
|
+
</li>
|
1364
|
+
|
1365
|
+
<li class="covered" data-hits="11" data-linenumber="54">
|
1366
|
+
<span class="hits">11</span>
|
1367
|
+
|
1368
|
+
<code class="ruby"> storage_units_format ||= (options[:format] || '%n%u')</code>
|
1369
|
+
</li>
|
1370
|
+
|
1371
|
+
<li class="never" data-hits="" data-linenumber="55">
|
1372
|
+
|
1373
|
+
|
1374
|
+
<code class="ruby"></code>
|
1375
|
+
</li>
|
1376
|
+
|
1377
|
+
<li class="covered" data-hits="11" data-linenumber="56">
|
1378
|
+
<span class="hits">11</span>
|
1379
|
+
|
1380
|
+
<code class="ruby"> if number.to_i < kilo</code>
|
1381
|
+
</li>
|
1382
|
+
|
1383
|
+
<li class="never" data-hits="" data-linenumber="57">
|
1384
|
+
|
1385
|
+
|
1386
|
+
<code class="ruby"> #unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true)</code>
|
1387
|
+
</li>
|
1388
|
+
|
1389
|
+
<li class="covered" data-hits="1" data-linenumber="58">
|
1390
|
+
<span class="hits">1</span>
|
1391
|
+
|
1392
|
+
<code class="ruby"> storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, '')</code>
|
1393
|
+
</li>
|
1394
|
+
|
1395
|
+
<li class="never" data-hits="" data-linenumber="59">
|
1396
|
+
|
1397
|
+
|
1398
|
+
<code class="ruby"> else</code>
|
1399
|
+
</li>
|
1400
|
+
|
1401
|
+
<li class="covered" data-hits="10" data-linenumber="60">
|
1402
|
+
<span class="hits">10</span>
|
1403
|
+
|
1404
|
+
<code class="ruby"> max_exp = STORAGE_UNITS.size - 1</code>
|
1405
|
+
</li>
|
1406
|
+
|
1407
|
+
<li class="covered" data-hits="10" data-linenumber="61">
|
1408
|
+
<span class="hits">10</span>
|
1409
|
+
|
1410
|
+
<code class="ruby"> number = Float(number)</code>
|
1411
|
+
</li>
|
1412
|
+
|
1413
|
+
<li class="covered" data-hits="10" data-linenumber="62">
|
1414
|
+
<span class="hits">10</span>
|
1415
|
+
|
1416
|
+
<code class="ruby"> exponent = (Math.log(number) / Math.log(kilo)).to_i # Convert to base 1024</code>
|
1417
|
+
</li>
|
1418
|
+
|
1419
|
+
<li class="covered" data-hits="10" data-linenumber="63">
|
1420
|
+
<span class="hits">10</span>
|
1421
|
+
|
1422
|
+
<code class="ruby"> exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit</code>
|
1423
|
+
</li>
|
1424
|
+
|
1425
|
+
<li class="covered" data-hits="10" data-linenumber="64">
|
1426
|
+
<span class="hits">10</span>
|
1427
|
+
|
1428
|
+
<code class="ruby"> number /= kilo ** exponent</code>
|
1429
|
+
</li>
|
1430
|
+
|
1431
|
+
<li class="never" data-hits="" data-linenumber="65">
|
1432
|
+
|
1433
|
+
|
1434
|
+
<code class="ruby"></code>
|
1435
|
+
</li>
|
1436
|
+
|
1437
|
+
<li class="covered" data-hits="10" data-linenumber="66">
|
1438
|
+
<span class="hits">10</span>
|
1439
|
+
|
1440
|
+
<code class="ruby"> unit = STORAGE_UNITS[exponent]</code>
|
1441
|
+
</li>
|
1442
|
+
|
1443
|
+
<li class="never" data-hits="" data-linenumber="67">
|
1444
|
+
|
1445
|
+
|
1446
|
+
<code class="ruby"></code>
|
1447
|
+
</li>
|
1448
|
+
|
1449
|
+
<li class="covered" data-hits="10" data-linenumber="68">
|
1450
|
+
<span class="hits">10</span>
|
1451
|
+
|
1452
|
+
<code class="ruby"> begin</code>
|
1453
|
+
</li>
|
1454
|
+
|
1455
|
+
<li class="covered" data-hits="10" data-linenumber="69">
|
1456
|
+
<span class="hits">10</span>
|
1457
|
+
|
1458
|
+
<code class="ruby"> escaped_separator = Regexp.escape(separator)</code>
|
1459
|
+
</li>
|
1460
|
+
|
1461
|
+
<li class="covered" data-hits="10" data-linenumber="70">
|
1462
|
+
<span class="hits">10</span>
|
1463
|
+
|
1464
|
+
<code class="ruby"> formatted_number = with_precision(number,</code>
|
1465
|
+
</li>
|
1466
|
+
|
1467
|
+
<li class="never" data-hits="" data-linenumber="71">
|
1468
|
+
|
1469
|
+
|
1470
|
+
<code class="ruby"> :precision => precision,</code>
|
1471
|
+
</li>
|
1472
|
+
|
1473
|
+
<li class="never" data-hits="" data-linenumber="72">
|
1474
|
+
|
1475
|
+
|
1476
|
+
<code class="ruby"> :separator => separator,</code>
|
1477
|
+
</li>
|
1478
|
+
|
1479
|
+
<li class="never" data-hits="" data-linenumber="73">
|
1480
|
+
|
1481
|
+
|
1482
|
+
<code class="ruby"> :delimiter => delimiter</code>
|
1483
|
+
</li>
|
1484
|
+
|
1485
|
+
<li class="never" data-hits="" data-linenumber="74">
|
1486
|
+
|
1487
|
+
|
1488
|
+
<code class="ruby"> ).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')</code>
|
1489
|
+
</li>
|
1490
|
+
|
1491
|
+
<li class="covered" data-hits="10" data-linenumber="75">
|
1492
|
+
<span class="hits">10</span>
|
1493
|
+
|
1494
|
+
<code class="ruby"> storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)</code>
|
1495
|
+
</li>
|
1496
|
+
|
1497
|
+
<li class="missed" data-hits="0" data-linenumber="76">
|
1498
|
+
|
1499
|
+
|
1500
|
+
<code class="ruby"> rescue => e</code>
|
1501
|
+
</li>
|
1502
|
+
|
1503
|
+
<li class="missed" data-hits="0" data-linenumber="77">
|
1504
|
+
|
1505
|
+
|
1506
|
+
<code class="ruby"> e</code>
|
1507
|
+
</li>
|
1508
|
+
|
1509
|
+
<li class="never" data-hits="" data-linenumber="78">
|
1510
|
+
|
1511
|
+
|
1512
|
+
<code class="ruby"> #number</code>
|
1513
|
+
</li>
|
1514
|
+
|
1515
|
+
<li class="never" data-hits="" data-linenumber="79">
|
1516
|
+
|
1517
|
+
|
1518
|
+
<code class="ruby"> end</code>
|
1519
|
+
</li>
|
1520
|
+
|
1521
|
+
<li class="never" data-hits="" data-linenumber="80">
|
1522
|
+
|
1523
|
+
|
1524
|
+
<code class="ruby"> end</code>
|
1525
|
+
</li>
|
1526
|
+
|
1527
|
+
<li class="never" data-hits="" data-linenumber="81">
|
1528
|
+
|
1529
|
+
|
1530
|
+
<code class="ruby"> end</code>
|
1531
|
+
</li>
|
1532
|
+
|
1533
|
+
<li class="never" data-hits="" data-linenumber="82">
|
1534
|
+
|
1535
|
+
|
1536
|
+
<code class="ruby"></code>
|
1537
|
+
</li>
|
1538
|
+
|
1539
|
+
<li class="never" data-hits="" data-linenumber="83">
|
1540
|
+
|
1541
|
+
|
1542
|
+
<code class="ruby"> ##</code>
|
1543
|
+
</li>
|
1544
|
+
|
1545
|
+
<li class="never" data-hits="" data-linenumber="84">
|
1546
|
+
|
1547
|
+
|
1548
|
+
<code class="ruby"> # Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).</code>
|
1549
|
+
</li>
|
1550
|
+
|
1551
|
+
<li class="never" data-hits="" data-linenumber="85">
|
1552
|
+
|
1553
|
+
|
1554
|
+
<code class="ruby"> # You can customize the format in the +options+ hash.</code>
|
1555
|
+
</li>
|
1556
|
+
|
1557
|
+
<li class="never" data-hits="" data-linenumber="86">
|
1558
|
+
|
1559
|
+
|
1560
|
+
<code class="ruby"> #</code>
|
1561
|
+
</li>
|
1562
|
+
|
1563
|
+
<li class="never" data-hits="" data-linenumber="87">
|
1564
|
+
|
1565
|
+
|
1566
|
+
<code class="ruby"> # @overload with_precision(number, options={})</code>
|
1567
|
+
</li>
|
1568
|
+
|
1569
|
+
<li class="never" data-hits="" data-linenumber="88">
|
1570
|
+
|
1571
|
+
|
1572
|
+
<code class="ruby"> # @param [Fixnum, Float] number</code>
|
1573
|
+
</li>
|
1574
|
+
|
1575
|
+
<li class="never" data-hits="" data-linenumber="89">
|
1576
|
+
|
1577
|
+
|
1578
|
+
<code class="ruby"> # Number value to format.</code>
|
1579
|
+
</li>
|
1580
|
+
|
1581
|
+
<li class="never" data-hits="" data-linenumber="90">
|
1582
|
+
|
1583
|
+
|
1584
|
+
<code class="ruby"> # @param [Hash] options</code>
|
1585
|
+
</li>
|
1586
|
+
|
1587
|
+
<li class="never" data-hits="" data-linenumber="91">
|
1588
|
+
|
1589
|
+
|
1590
|
+
<code class="ruby"> # Options for formatter.</code>
|
1591
|
+
</li>
|
1592
|
+
|
1593
|
+
<li class="never" data-hits="" data-linenumber="92">
|
1594
|
+
|
1595
|
+
|
1596
|
+
<code class="ruby"> # @option options [Fixnum] :precision (3)</code>
|
1597
|
+
</li>
|
1598
|
+
|
1599
|
+
<li class="never" data-hits="" data-linenumber="93">
|
1600
|
+
|
1601
|
+
|
1602
|
+
<code class="ruby"> # Sets the level of precision.</code>
|
1603
|
+
</li>
|
1604
|
+
|
1605
|
+
<li class="never" data-hits="" data-linenumber="94">
|
1606
|
+
|
1607
|
+
|
1608
|
+
<code class="ruby"> # @option options [String] :separator (".")</code>
|
1609
|
+
</li>
|
1610
|
+
|
1611
|
+
<li class="never" data-hits="" data-linenumber="95">
|
1612
|
+
|
1613
|
+
|
1614
|
+
<code class="ruby"> # Sets the separator between the units.</code>
|
1615
|
+
</li>
|
1616
|
+
|
1617
|
+
<li class="never" data-hits="" data-linenumber="96">
|
1618
|
+
|
1619
|
+
|
1620
|
+
<code class="ruby"> # @option options [String] :delimiter ("")</code>
|
1621
|
+
</li>
|
1622
|
+
|
1623
|
+
<li class="never" data-hits="" data-linenumber="97">
|
1624
|
+
|
1625
|
+
|
1626
|
+
<code class="ruby"> # Sets the thousands delimiter.</code>
|
1627
|
+
</li>
|
1628
|
+
|
1629
|
+
<li class="never" data-hits="" data-linenumber="98">
|
1630
|
+
|
1631
|
+
|
1632
|
+
<code class="ruby"> #</code>
|
1633
|
+
</li>
|
1634
|
+
|
1635
|
+
<li class="never" data-hits="" data-linenumber="99">
|
1636
|
+
|
1637
|
+
|
1638
|
+
<code class="ruby"> # @return [String] The formatted representation of the number.</code>
|
1639
|
+
</li>
|
1640
|
+
|
1641
|
+
<li class="never" data-hits="" data-linenumber="100">
|
1642
|
+
|
1643
|
+
|
1644
|
+
<code class="ruby"> #</code>
|
1645
|
+
</li>
|
1646
|
+
|
1647
|
+
<li class="never" data-hits="" data-linenumber="101">
|
1648
|
+
|
1649
|
+
|
1650
|
+
<code class="ruby"> # @example</code>
|
1651
|
+
</li>
|
1652
|
+
|
1653
|
+
<li class="never" data-hits="" data-linenumber="102">
|
1654
|
+
|
1655
|
+
|
1656
|
+
<code class="ruby"> # with_precision(111.2345) # => 111.235</code>
|
1657
|
+
</li>
|
1658
|
+
|
1659
|
+
<li class="never" data-hits="" data-linenumber="103">
|
1660
|
+
|
1661
|
+
|
1662
|
+
<code class="ruby"> # with_precision(111.2345, :precision => 2) # => 111.23</code>
|
1663
|
+
</li>
|
1664
|
+
|
1665
|
+
<li class="never" data-hits="" data-linenumber="104">
|
1666
|
+
|
1667
|
+
|
1668
|
+
<code class="ruby"> # with_precision(13, :precision => 5) # => 13.00000</code>
|
1669
|
+
</li>
|
1670
|
+
|
1671
|
+
<li class="never" data-hits="" data-linenumber="105">
|
1672
|
+
|
1673
|
+
|
1674
|
+
<code class="ruby"> # with_precision(389.32314, :precision => 0) # => 389</code>
|
1675
|
+
</li>
|
1676
|
+
|
1677
|
+
<li class="never" data-hits="" data-linenumber="106">
|
1678
|
+
|
1679
|
+
|
1680
|
+
<code class="ruby"> # with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')</code>
|
1681
|
+
</li>
|
1682
|
+
|
1683
|
+
<li class="never" data-hits="" data-linenumber="107">
|
1684
|
+
|
1685
|
+
|
1686
|
+
<code class="ruby"> # # => 1.111,23</code>
|
1687
|
+
</li>
|
1688
|
+
|
1689
|
+
<li class="never" data-hits="" data-linenumber="108">
|
1690
|
+
|
1691
|
+
|
1692
|
+
<code class="ruby"> #</code>
|
1693
|
+
</li>
|
1694
|
+
|
1695
|
+
<li class="covered" data-hits="1" data-linenumber="109">
|
1696
|
+
<span class="hits">1</span>
|
1697
|
+
|
1698
|
+
<code class="ruby"> def with_precision(number, args)</code>
|
1699
|
+
</li>
|
1700
|
+
|
1701
|
+
<li class="covered" data-hits="11" data-linenumber="110">
|
1702
|
+
<span class="hits">11</span>
|
1703
|
+
|
1704
|
+
<code class="ruby"> options = BB::Hash.symbolize_keys(args)</code>
|
1705
|
+
</li>
|
1706
|
+
|
1707
|
+
<li class="never" data-hits="" data-linenumber="111">
|
1708
|
+
|
1709
|
+
|
1710
|
+
<code class="ruby"></code>
|
1711
|
+
</li>
|
1712
|
+
|
1713
|
+
<li class="covered" data-hits="11" data-linenumber="112">
|
1714
|
+
<span class="hits">11</span>
|
1715
|
+
|
1716
|
+
<code class="ruby"> precision ||= (options[:precision] || 3)</code>
|
1717
|
+
</li>
|
1718
|
+
|
1719
|
+
<li class="covered" data-hits="11" data-linenumber="113">
|
1720
|
+
<span class="hits">11</span>
|
1721
|
+
|
1722
|
+
<code class="ruby"> separator ||= (options[:separator] || '.')</code>
|
1723
|
+
</li>
|
1724
|
+
|
1725
|
+
<li class="covered" data-hits="11" data-linenumber="114">
|
1726
|
+
<span class="hits">11</span>
|
1727
|
+
|
1728
|
+
<code class="ruby"> delimiter ||= (options[:delimiter] || '')</code>
|
1729
|
+
</li>
|
1730
|
+
|
1731
|
+
<li class="never" data-hits="" data-linenumber="115">
|
1732
|
+
|
1733
|
+
|
1734
|
+
<code class="ruby"></code>
|
1735
|
+
</li>
|
1736
|
+
|
1737
|
+
<li class="covered" data-hits="11" data-linenumber="116">
|
1738
|
+
<span class="hits">11</span>
|
1739
|
+
|
1740
|
+
<code class="ruby"> begin</code>
|
1741
|
+
</li>
|
1742
|
+
|
1743
|
+
<li class="covered" data-hits="11" data-linenumber="117">
|
1744
|
+
<span class="hits">11</span>
|
1745
|
+
|
1746
|
+
<code class="ruby"> rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision</code>
|
1747
|
+
</li>
|
1748
|
+
|
1749
|
+
<li class="covered" data-hits="11" data-linenumber="118">
|
1750
|
+
<span class="hits">11</span>
|
1751
|
+
|
1752
|
+
<code class="ruby"> with_delimiter("%01.#{precision}f" % rounded_number,</code>
|
1753
|
+
</li>
|
1754
|
+
|
1755
|
+
<li class="never" data-hits="" data-linenumber="119">
|
1756
|
+
|
1757
|
+
|
1758
|
+
<code class="ruby"> :separator => separator,</code>
|
1759
|
+
</li>
|
1760
|
+
|
1761
|
+
<li class="never" data-hits="" data-linenumber="120">
|
1762
|
+
|
1763
|
+
|
1764
|
+
<code class="ruby"> :delimiter => delimiter)</code>
|
1765
|
+
</li>
|
1766
|
+
|
1767
|
+
<li class="never" data-hits="" data-linenumber="121">
|
1768
|
+
|
1769
|
+
|
1770
|
+
<code class="ruby"> rescue</code>
|
1771
|
+
</li>
|
1772
|
+
|
1773
|
+
<li class="missed" data-hits="0" data-linenumber="122">
|
1774
|
+
|
1775
|
+
|
1776
|
+
<code class="ruby"> number</code>
|
1777
|
+
</li>
|
1778
|
+
|
1779
|
+
<li class="never" data-hits="" data-linenumber="123">
|
1780
|
+
|
1781
|
+
|
1782
|
+
<code class="ruby"> end</code>
|
1783
|
+
</li>
|
1784
|
+
|
1785
|
+
<li class="never" data-hits="" data-linenumber="124">
|
1786
|
+
|
1787
|
+
|
1788
|
+
<code class="ruby"> end</code>
|
1789
|
+
</li>
|
1790
|
+
|
1791
|
+
<li class="never" data-hits="" data-linenumber="125">
|
1792
|
+
|
1793
|
+
|
1794
|
+
<code class="ruby"></code>
|
1795
|
+
</li>
|
1796
|
+
|
1797
|
+
<li class="never" data-hits="" data-linenumber="126">
|
1798
|
+
|
1799
|
+
|
1800
|
+
<code class="ruby"> ##</code>
|
1801
|
+
</li>
|
1802
|
+
|
1803
|
+
<li class="never" data-hits="" data-linenumber="127">
|
1804
|
+
|
1805
|
+
|
1806
|
+
<code class="ruby"> # Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can</code>
|
1807
|
+
</li>
|
1808
|
+
|
1809
|
+
<li class="never" data-hits="" data-linenumber="128">
|
1810
|
+
|
1811
|
+
|
1812
|
+
<code class="ruby"> # customize the format in the +options+ hash.</code>
|
1813
|
+
</li>
|
1814
|
+
|
1815
|
+
<li class="never" data-hits="" data-linenumber="129">
|
1816
|
+
|
1817
|
+
|
1818
|
+
<code class="ruby"> #</code>
|
1819
|
+
</li>
|
1820
|
+
|
1821
|
+
<li class="never" data-hits="" data-linenumber="130">
|
1822
|
+
|
1823
|
+
|
1824
|
+
<code class="ruby"> # @overload with_delimiter(number, options={})</code>
|
1825
|
+
</li>
|
1826
|
+
|
1827
|
+
<li class="never" data-hits="" data-linenumber="131">
|
1828
|
+
|
1829
|
+
|
1830
|
+
<code class="ruby"> # @param [Fixnum, Float] number</code>
|
1831
|
+
</li>
|
1832
|
+
|
1833
|
+
<li class="never" data-hits="" data-linenumber="132">
|
1834
|
+
|
1835
|
+
|
1836
|
+
<code class="ruby"> # Number value to format.</code>
|
1837
|
+
</li>
|
1838
|
+
|
1839
|
+
<li class="never" data-hits="" data-linenumber="133">
|
1840
|
+
|
1841
|
+
|
1842
|
+
<code class="ruby"> # @param [Hash] options</code>
|
1843
|
+
</li>
|
1844
|
+
|
1845
|
+
<li class="never" data-hits="" data-linenumber="134">
|
1846
|
+
|
1847
|
+
|
1848
|
+
<code class="ruby"> # Options for formatter.</code>
|
1849
|
+
</li>
|
1850
|
+
|
1851
|
+
<li class="never" data-hits="" data-linenumber="135">
|
1852
|
+
|
1853
|
+
|
1854
|
+
<code class="ruby"> # @option options [String] :delimiter (", ")</code>
|
1855
|
+
</li>
|
1856
|
+
|
1857
|
+
<li class="never" data-hits="" data-linenumber="136">
|
1858
|
+
|
1859
|
+
|
1860
|
+
<code class="ruby"> # Sets the thousands delimiter.</code>
|
1861
|
+
</li>
|
1862
|
+
|
1863
|
+
<li class="never" data-hits="" data-linenumber="137">
|
1864
|
+
|
1865
|
+
|
1866
|
+
<code class="ruby"> # @option options [String] :separator (".")</code>
|
1867
|
+
</li>
|
1868
|
+
|
1869
|
+
<li class="never" data-hits="" data-linenumber="138">
|
1870
|
+
|
1871
|
+
|
1872
|
+
<code class="ruby"> # Sets the separator between the units.</code>
|
1873
|
+
</li>
|
1874
|
+
|
1875
|
+
<li class="never" data-hits="" data-linenumber="139">
|
1876
|
+
|
1877
|
+
|
1878
|
+
<code class="ruby"> #</code>
|
1879
|
+
</li>
|
1880
|
+
|
1881
|
+
<li class="never" data-hits="" data-linenumber="140">
|
1882
|
+
|
1883
|
+
|
1884
|
+
<code class="ruby"> # @return [String] The formatted representation of the number.</code>
|
1885
|
+
</li>
|
1886
|
+
|
1887
|
+
<li class="never" data-hits="" data-linenumber="141">
|
1888
|
+
|
1889
|
+
|
1890
|
+
<code class="ruby"> #</code>
|
1891
|
+
</li>
|
1892
|
+
|
1893
|
+
<li class="never" data-hits="" data-linenumber="142">
|
1894
|
+
|
1895
|
+
|
1896
|
+
<code class="ruby"> # @example</code>
|
1897
|
+
</li>
|
1898
|
+
|
1899
|
+
<li class="never" data-hits="" data-linenumber="143">
|
1900
|
+
|
1901
|
+
|
1902
|
+
<code class="ruby"> # with_delimiter(12345678) # => 12,345,678</code>
|
1903
|
+
</li>
|
1904
|
+
|
1905
|
+
<li class="never" data-hits="" data-linenumber="144">
|
1906
|
+
|
1907
|
+
|
1908
|
+
<code class="ruby"> # with_delimiter(12345678.05) # => 12,345,678.05</code>
|
1909
|
+
</li>
|
1910
|
+
|
1911
|
+
<li class="never" data-hits="" data-linenumber="145">
|
1912
|
+
|
1913
|
+
|
1914
|
+
<code class="ruby"> # with_delimiter(12345678, :delimiter => ".") # => 12.345.678</code>
|
1915
|
+
</li>
|
1916
|
+
|
1917
|
+
<li class="never" data-hits="" data-linenumber="146">
|
1918
|
+
|
1919
|
+
|
1920
|
+
<code class="ruby"> # with_delimiter(12345678, :separator => ",") # => 12,345,678</code>
|
1921
|
+
</li>
|
1922
|
+
|
1923
|
+
<li class="never" data-hits="" data-linenumber="147">
|
1924
|
+
|
1925
|
+
|
1926
|
+
<code class="ruby"> # with_delimiter(98765432.98, :delimiter => " ", :separator => ",")</code>
|
1927
|
+
</li>
|
1928
|
+
|
1929
|
+
<li class="never" data-hits="" data-linenumber="148">
|
1930
|
+
|
1931
|
+
|
1932
|
+
<code class="ruby"> # # => 98 765 432,98</code>
|
1933
|
+
</li>
|
1934
|
+
|
1935
|
+
<li class="never" data-hits="" data-linenumber="149">
|
1936
|
+
|
1937
|
+
|
1938
|
+
<code class="ruby"> #</code>
|
1939
|
+
</li>
|
1940
|
+
|
1941
|
+
<li class="covered" data-hits="1" data-linenumber="150">
|
1942
|
+
<span class="hits">1</span>
|
1943
|
+
|
1944
|
+
<code class="ruby"> def with_delimiter(number, args)</code>
|
1945
|
+
</li>
|
1946
|
+
|
1947
|
+
<li class="covered" data-hits="12" data-linenumber="151">
|
1948
|
+
<span class="hits">12</span>
|
1949
|
+
|
1950
|
+
<code class="ruby"> options = BB::Hash.symbolize_keys(args)</code>
|
1951
|
+
</li>
|
1952
|
+
|
1953
|
+
<li class="never" data-hits="" data-linenumber="152">
|
1954
|
+
|
1955
|
+
|
1956
|
+
<code class="ruby"></code>
|
1957
|
+
</li>
|
1958
|
+
|
1959
|
+
<li class="covered" data-hits="12" data-linenumber="153">
|
1960
|
+
<span class="hits">12</span>
|
1961
|
+
|
1962
|
+
<code class="ruby"> delimiter ||= (options[:delimiter] || '.')</code>
|
1963
|
+
</li>
|
1964
|
+
|
1965
|
+
<li class="covered" data-hits="12" data-linenumber="154">
|
1966
|
+
<span class="hits">12</span>
|
1967
|
+
|
1968
|
+
<code class="ruby"> separator ||= (options[:separator] || ',')</code>
|
1969
|
+
</li>
|
1970
|
+
|
1971
|
+
<li class="never" data-hits="" data-linenumber="155">
|
1972
|
+
|
1973
|
+
|
1974
|
+
<code class="ruby"></code>
|
1975
|
+
</li>
|
1976
|
+
|
1977
|
+
<li class="covered" data-hits="12" data-linenumber="156">
|
1978
|
+
<span class="hits">12</span>
|
1979
|
+
|
1980
|
+
<code class="ruby"> begin</code>
|
1981
|
+
</li>
|
1982
|
+
|
1983
|
+
<li class="covered" data-hits="12" data-linenumber="157">
|
1984
|
+
<span class="hits">12</span>
|
1985
|
+
|
1986
|
+
<code class="ruby"> parts = number.to_s.split('.')</code>
|
1987
|
+
</li>
|
1988
|
+
|
1989
|
+
<li class="covered" data-hits="12" data-linenumber="158">
|
1990
|
+
<span class="hits">12</span>
|
1991
|
+
|
1992
|
+
<code class="ruby"> parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")</code>
|
1993
|
+
</li>
|
1994
|
+
|
1995
|
+
<li class="covered" data-hits="12" data-linenumber="159">
|
1996
|
+
<span class="hits">12</span>
|
1997
|
+
|
1998
|
+
<code class="ruby"> parts.join(separator)</code>
|
1999
|
+
</li>
|
2000
|
+
|
2001
|
+
<li class="never" data-hits="" data-linenumber="160">
|
2002
|
+
|
2003
|
+
|
2004
|
+
<code class="ruby"> rescue</code>
|
2005
|
+
</li>
|
2006
|
+
|
2007
|
+
<li class="missed" data-hits="0" data-linenumber="161">
|
2008
|
+
|
2009
|
+
|
2010
|
+
<code class="ruby"> number</code>
|
2011
|
+
</li>
|
2012
|
+
|
2013
|
+
<li class="never" data-hits="" data-linenumber="162">
|
2014
|
+
|
2015
|
+
|
2016
|
+
<code class="ruby"> end</code>
|
2017
|
+
</li>
|
2018
|
+
|
2019
|
+
<li class="never" data-hits="" data-linenumber="163">
|
2020
|
+
|
2021
|
+
|
2022
|
+
<code class="ruby"> end</code>
|
2023
|
+
</li>
|
2024
|
+
|
2025
|
+
<li class="never" data-hits="" data-linenumber="164">
|
2026
|
+
|
2027
|
+
|
2028
|
+
<code class="ruby"> end</code>
|
2029
|
+
</li>
|
2030
|
+
|
2031
|
+
<li class="never" data-hits="" data-linenumber="165">
|
2032
|
+
|
2033
|
+
|
2034
|
+
<code class="ruby"> end</code>
|
2035
|
+
</li>
|
2036
|
+
|
2037
|
+
<li class="never" data-hits="" data-linenumber="166">
|
2038
|
+
|
2039
|
+
|
2040
|
+
<code class="ruby">end</code>
|
2041
|
+
</li>
|
2042
|
+
|
2043
|
+
<li class="never" data-hits="" data-linenumber="167">
|
2044
|
+
|
2045
|
+
|
2046
|
+
<code class="ruby"></code>
|
2047
|
+
</li>
|
2048
|
+
|
2049
|
+
</ol>
|
2050
|
+
</pre>
|
2051
|
+
</div>
|
2052
|
+
|
1023
2053
|
<div class="source_table" id="b75f047c409859a12a41002ece7dbd0d81dd859d">
|
1024
2054
|
<div class="header">
|
1025
2055
|
<h3>lib/blackbox/string.rb</h3>
|