core_ext 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/core_ext.rb +4 -1
- data/lib/core_ext/callbacks.rb +15 -2
- data/lib/core_ext/descendants_tracker.rb +50 -0
- data/lib/core_ext/gzip.rb +36 -0
- data/lib/core_ext/i18n.rb +11 -0
- data/lib/core_ext/locale/en.yml +135 -0
- data/lib/core_ext/number_helper.rb +358 -0
- data/lib/core_ext/number_helper/number_converter.rb +182 -0
- data/lib/core_ext/number_helper/number_to_currency_converter.rb +46 -0
- data/lib/core_ext/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/core_ext/number_helper/number_to_human_converter.rb +68 -0
- data/lib/core_ext/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/core_ext/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/core_ext/number_helper/number_to_phone_converter.rb +49 -0
- data/lib/core_ext/number_helper/number_to_rounded_converter.rb +90 -0
- data/lib/core_ext/numeric/conversions.rb +8 -1
- data/lib/core_ext/string/conversions.rb +2 -1
- data/lib/core_ext/test_case.rb +1 -0
- data/lib/core_ext/values/unicode_tables.dat +0 -0
- data/lib/core_ext/version.rb +1 -1
- data/lib/core_ext/xml_mini.rb +1 -1
- metadata +17 -5
- data/lib/core_ext/marshal.rb +0 -19
@@ -0,0 +1,90 @@
|
|
1
|
+
module CoreExt
|
2
|
+
module NumberHelper
|
3
|
+
class NumberToRoundedConverter < NumberConverter # :nodoc:
|
4
|
+
self.namespace = :precision
|
5
|
+
self.validate_float = true
|
6
|
+
|
7
|
+
def convert
|
8
|
+
precision = options.delete :precision
|
9
|
+
|
10
|
+
if precision
|
11
|
+
case number
|
12
|
+
when Float, String
|
13
|
+
@number = BigDecimal(number.to_s)
|
14
|
+
when Rational
|
15
|
+
@number = BigDecimal(number, digit_count(number.to_i) + precision)
|
16
|
+
else
|
17
|
+
@number = number.to_d
|
18
|
+
end
|
19
|
+
|
20
|
+
if options.delete(:significant) && precision > 0
|
21
|
+
digits, rounded_number = digits_and_rounded_number(precision)
|
22
|
+
precision -= digits
|
23
|
+
precision = 0 if precision < 0 # don't let it be negative
|
24
|
+
else
|
25
|
+
rounded_number = number.round(precision)
|
26
|
+
rounded_number = rounded_number.to_i if precision == 0 && rounded_number.finite?
|
27
|
+
rounded_number = rounded_number.abs if rounded_number.zero? # prevent showing negative zeros
|
28
|
+
end
|
29
|
+
|
30
|
+
formatted_string =
|
31
|
+
if BigDecimal === rounded_number && rounded_number.finite?
|
32
|
+
s = rounded_number.to_s('F') + '0'*precision
|
33
|
+
a, b = s.split('.', 2)
|
34
|
+
a + '.' + b[0, precision]
|
35
|
+
else
|
36
|
+
"%00.#{precision}f" % rounded_number
|
37
|
+
end
|
38
|
+
else
|
39
|
+
formatted_string = number
|
40
|
+
end
|
41
|
+
|
42
|
+
delimited_number = NumberToDelimitedConverter.convert(formatted_string, options)
|
43
|
+
format_number(delimited_number)
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def digits_and_rounded_number(precision)
|
49
|
+
if zero?
|
50
|
+
[1, 0]
|
51
|
+
else
|
52
|
+
digits = digit_count(number)
|
53
|
+
multiplier = 10 ** (digits - precision)
|
54
|
+
rounded_number = calculate_rounded_number(multiplier)
|
55
|
+
digits = digit_count(rounded_number) # After rounding, the number of digits may have changed
|
56
|
+
[digits, rounded_number]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def calculate_rounded_number(multiplier)
|
61
|
+
(number / BigDecimal.new(multiplier.to_f.to_s)).round * multiplier
|
62
|
+
end
|
63
|
+
|
64
|
+
def digit_count(number)
|
65
|
+
number.zero? ? 1 : (Math.log10(absolute_number(number)) + 1).floor
|
66
|
+
end
|
67
|
+
|
68
|
+
def strip_insignificant_zeros
|
69
|
+
options[:strip_insignificant_zeros]
|
70
|
+
end
|
71
|
+
|
72
|
+
def format_number(number)
|
73
|
+
if strip_insignificant_zeros
|
74
|
+
escaped_separator = Regexp.escape(options[:separator])
|
75
|
+
number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
|
76
|
+
else
|
77
|
+
number
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def absolute_number(number)
|
82
|
+
number.respond_to?(:abs) ? number.abs : number.to_d.abs
|
83
|
+
end
|
84
|
+
|
85
|
+
def zero?
|
86
|
+
number.respond_to?(:zero?) ? number.zero? : number.to_d.zero?
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'core_ext/big_decimal/conversions'
|
2
2
|
require 'core_ext/deprecation'
|
3
|
+
require 'core_ext/number_helper'
|
3
4
|
|
4
5
|
module CoreExt::NumericWithFormat
|
5
6
|
|
@@ -75,6 +76,8 @@ module CoreExt::NumericWithFormat
|
|
75
76
|
# 1234567.to_s(:human_size) # => 1.18 MB
|
76
77
|
# 1234567890.to_s(:human_size) # => 1.15 GB
|
77
78
|
# 1234567890123.to_s(:human_size) # => 1.12 TB
|
79
|
+
# 1234567890123456.to_s(:human_size) # => 1.1 PB
|
80
|
+
# 1234567890123456789.to_s(:human_size) # => 1.07 EB
|
78
81
|
# 1234567.to_s(:human_size, precision: 2) # => 1.2 MB
|
79
82
|
# 483989.to_s(:human_size, precision: 2) # => 470 KB
|
80
83
|
# 1234567.to_s(:human_size, precision: 2, separator: ',') # => 1,2 MB
|
@@ -117,7 +120,11 @@ module CoreExt::NumericWithFormat
|
|
117
120
|
when :human_size
|
118
121
|
return CoreExt::NumberHelper.number_to_human_size(self, options)
|
119
122
|
else
|
120
|
-
|
123
|
+
if is_a?(Float) || format.is_a?(Symbol)
|
124
|
+
super()
|
125
|
+
else
|
126
|
+
super
|
127
|
+
end
|
121
128
|
end
|
122
129
|
end
|
123
130
|
|
data/lib/core_ext/test_case.rb
CHANGED
@@ -56,6 +56,7 @@ module CoreExt
|
|
56
56
|
include CoreExt::Testing::TaggedLogging
|
57
57
|
include CoreExt::Testing::SetupAndTeardown
|
58
58
|
include CoreExt::Testing::Assertions
|
59
|
+
include CoreExt::Testing::Deprecation
|
59
60
|
include CoreExt::Testing::TimeHelpers
|
60
61
|
include CoreExt::Testing::FileFixtures
|
61
62
|
extend CoreExt::Testing::Declarative
|
Binary file
|
data/lib/core_ext/version.rb
CHANGED
data/lib/core_ext/xml_mini.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: core_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Panachi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -179,11 +179,13 @@ files:
|
|
179
179
|
- lib/core_ext/deprecation/method_wrappers.rb
|
180
180
|
- lib/core_ext/deprecation/proxy_wrappers.rb
|
181
181
|
- lib/core_ext/deprecation/reporting.rb
|
182
|
+
- lib/core_ext/descendants_tracker.rb
|
182
183
|
- lib/core_ext/digest/uuid.rb
|
183
184
|
- lib/core_ext/duration.rb
|
184
185
|
- lib/core_ext/enumerable.rb
|
185
186
|
- lib/core_ext/file.rb
|
186
187
|
- lib/core_ext/file/atomic.rb
|
188
|
+
- lib/core_ext/gzip.rb
|
187
189
|
- lib/core_ext/hash.rb
|
188
190
|
- lib/core_ext/hash/compact.rb
|
189
191
|
- lib/core_ext/hash/conversions.rb
|
@@ -195,6 +197,7 @@ files:
|
|
195
197
|
- lib/core_ext/hash/slice.rb
|
196
198
|
- lib/core_ext/hash/transform_values.rb
|
197
199
|
- lib/core_ext/hash_with_indifferent_access.rb
|
200
|
+
- lib/core_ext/i18n.rb
|
198
201
|
- lib/core_ext/inflections.rb
|
199
202
|
- lib/core_ext/inflector.rb
|
200
203
|
- lib/core_ext/inflector/inflections.rb
|
@@ -213,9 +216,9 @@ files:
|
|
213
216
|
- lib/core_ext/kernel/reporting.rb
|
214
217
|
- lib/core_ext/kernel/singleton_class.rb
|
215
218
|
- lib/core_ext/load_error.rb
|
219
|
+
- lib/core_ext/locale/en.yml
|
216
220
|
- lib/core_ext/logger.rb
|
217
221
|
- lib/core_ext/logger_silence.rb
|
218
|
-
- lib/core_ext/marshal.rb
|
219
222
|
- lib/core_ext/module.rb
|
220
223
|
- lib/core_ext/module/aliasing.rb
|
221
224
|
- lib/core_ext/module/anonymous.rb
|
@@ -233,6 +236,15 @@ files:
|
|
233
236
|
- lib/core_ext/multibyte/chars.rb
|
234
237
|
- lib/core_ext/multibyte/unicode.rb
|
235
238
|
- lib/core_ext/name_error.rb
|
239
|
+
- lib/core_ext/number_helper.rb
|
240
|
+
- lib/core_ext/number_helper/number_converter.rb
|
241
|
+
- lib/core_ext/number_helper/number_to_currency_converter.rb
|
242
|
+
- lib/core_ext/number_helper/number_to_delimited_converter.rb
|
243
|
+
- lib/core_ext/number_helper/number_to_human_converter.rb
|
244
|
+
- lib/core_ext/number_helper/number_to_human_size_converter.rb
|
245
|
+
- lib/core_ext/number_helper/number_to_percentage_converter.rb
|
246
|
+
- lib/core_ext/number_helper/number_to_phone_converter.rb
|
247
|
+
- lib/core_ext/number_helper/number_to_rounded_converter.rb
|
236
248
|
- lib/core_ext/numeric.rb
|
237
249
|
- lib/core_ext/numeric/bytes.rb
|
238
250
|
- lib/core_ext/numeric/conversions.rb
|
@@ -301,6 +313,7 @@ files:
|
|
301
313
|
- lib/core_ext/time_with_zone.rb
|
302
314
|
- lib/core_ext/time_zone.rb
|
303
315
|
- lib/core_ext/uri.rb
|
316
|
+
- lib/core_ext/values/unicode_tables.dat
|
304
317
|
- lib/core_ext/version.rb
|
305
318
|
- lib/core_ext/xml_mini.rb
|
306
319
|
- lib/core_ext/xml_mini/jdom.rb
|
@@ -331,9 +344,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
331
344
|
version: '0'
|
332
345
|
requirements: []
|
333
346
|
rubyforge_project:
|
334
|
-
rubygems_version: 2.4.5
|
347
|
+
rubygems_version: 2.4.5.1
|
335
348
|
signing_key:
|
336
349
|
specification_version: 4
|
337
350
|
summary: ActiveSupport's core_ext for non Rails projects
|
338
351
|
test_files: []
|
339
|
-
has_rdoc:
|
data/lib/core_ext/marshal.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
module CoreExt
|
2
|
-
module MarshalWithAutoloading # :nodoc:
|
3
|
-
def load(source)
|
4
|
-
super(source)
|
5
|
-
rescue ArgumentError, NameError => exc
|
6
|
-
if exc.message.match(%r|undefined class/module (.+)|)
|
7
|
-
# try loading the class/module
|
8
|
-
$1.constantize
|
9
|
-
# if it is an IO we need to go back to read the object
|
10
|
-
source.rewind if source.respond_to?(:rewind)
|
11
|
-
retry
|
12
|
-
else
|
13
|
-
raise exc
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
Marshal.singleton_class.prepend(CoreExt::MarshalWithAutoloading)
|