activesupport 4.0.13 → 4.2.11.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +406 -418
- data/MIT-LICENSE +1 -1
- data/README.rdoc +7 -2
- data/lib/active_support/backtrace_cleaner.rb +8 -8
- data/lib/active_support/benchmarkable.rb +0 -10
- data/lib/active_support/cache/file_store.rb +32 -22
- data/lib/active_support/cache/mem_cache_store.rb +5 -7
- data/lib/active_support/cache/memory_store.rb +1 -0
- data/lib/active_support/cache/strategy/local_cache.rb +11 -30
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/cache.rb +75 -41
- data/lib/active_support/callbacks.rb +482 -261
- data/lib/active_support/concern.rb +23 -7
- data/lib/active_support/configurable.rb +1 -1
- data/lib/active_support/core_ext/array/access.rb +11 -1
- data/lib/active_support/core_ext/array/conversions.rb +2 -17
- data/lib/active_support/core_ext/array/grouping.rb +29 -12
- data/lib/active_support/core_ext/array/prepend_and_append.rb +2 -2
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +0 -15
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +16 -0
- data/lib/active_support/core_ext/class/attribute.rb +1 -2
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -170
- data/lib/active_support/core_ext/class/delegating_attributes.rb +13 -8
- data/lib/active_support/core_ext/class/subclasses.rb +0 -2
- data/lib/active_support/core_ext/class.rb +0 -1
- data/lib/active_support/core_ext/date/calculations.rb +10 -0
- data/lib/active_support/core_ext/date/conversions.rb +9 -1
- data/lib/active_support/core_ext/date/zones.rb +2 -33
- data/lib/active_support/core_ext/date_and_time/calculations.rb +41 -11
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +45 -22
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +4 -2
- data/lib/active_support/core_ext/date_time/zones.rb +3 -21
- data/lib/active_support/core_ext/date_time.rb +1 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +17 -1
- data/lib/active_support/core_ext/file/atomic.rb +1 -1
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +9 -8
- data/lib/active_support/core_ext/hash/except.rb +8 -2
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -0
- data/lib/active_support/core_ext/hash/keys.rb +25 -19
- data/lib/active_support/core_ext/hash/slice.rb +8 -2
- data/lib/active_support/core_ext/hash/transform_values.rb +23 -0
- data/lib/active_support/core_ext/hash.rb +2 -1
- data/lib/active_support/core_ext/integer/time.rb +0 -15
- data/lib/active_support/core_ext/kernel/concern.rb +10 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +1 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +13 -2
- data/lib/active_support/core_ext/kernel.rb +3 -2
- data/lib/active_support/core_ext/load_error.rb +4 -1
- data/lib/active_support/core_ext/marshal.rb +8 -5
- data/lib/active_support/core_ext/module/aliasing.rb +2 -2
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -1
- data/lib/active_support/core_ext/module/attribute_accessors.rb +160 -14
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +53 -25
- data/lib/active_support/core_ext/module/deprecation.rb +0 -2
- data/lib/active_support/core_ext/module/introspection.rb +0 -16
- data/lib/active_support/core_ext/module/method_transplanting.rb +13 -0
- data/lib/active_support/core_ext/module.rb +1 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +11 -3
- data/lib/active_support/core_ext/numeric/time.rb +4 -29
- data/lib/active_support/core_ext/object/blank.rb +44 -18
- data/lib/active_support/core_ext/object/deep_dup.rb +6 -6
- data/lib/active_support/core_ext/object/duplicable.rb +72 -33
- data/lib/active_support/core_ext/object/inclusion.rb +16 -15
- data/lib/active_support/core_ext/object/itself.rb +15 -0
- data/lib/active_support/core_ext/object/json.rb +197 -0
- data/lib/active_support/core_ext/object/to_query.rb +14 -6
- data/lib/active_support/core_ext/object/try.rb +36 -14
- data/lib/active_support/core_ext/object/with_options.rb +30 -3
- data/lib/active_support/core_ext/object.rb +2 -1
- data/lib/active_support/core_ext/string/access.rb +35 -35
- data/lib/active_support/core_ext/string/conversions.rb +10 -9
- data/lib/active_support/core_ext/string/exclude.rb +3 -3
- data/lib/active_support/core_ext/string/filters.rb +51 -3
- data/lib/active_support/core_ext/string/inflections.rb +15 -10
- data/lib/active_support/core_ext/string/output_safety.rb +97 -33
- data/lib/active_support/core_ext/string/zones.rb +1 -0
- data/lib/active_support/core_ext/thread.rb +12 -5
- data/lib/active_support/core_ext/time/calculations.rb +47 -68
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +4 -2
- data/lib/active_support/core_ext/time/zones.rb +2 -20
- data/lib/active_support/core_ext/time.rb +1 -0
- data/lib/active_support/core_ext.rb +0 -1
- data/lib/active_support/dependencies/autoload.rb +1 -1
- data/lib/active_support/dependencies.rb +64 -25
- data/lib/active_support/deprecation/behaviors.rb +4 -4
- data/lib/active_support/deprecation.rb +4 -4
- data/lib/active_support/duration.rb +55 -11
- data/lib/active_support/file_update_checker.rb +1 -1
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/hash_with_indifferent_access.rb +39 -11
- data/lib/active_support/i18n.rb +4 -4
- data/lib/active_support/i18n_railtie.rb +1 -7
- data/lib/active_support/inflections.rb +6 -1
- data/lib/active_support/inflector/inflections.rb +19 -19
- data/lib/active_support/inflector/methods.rb +66 -25
- data/lib/active_support/json/decoding.rb +15 -22
- data/lib/active_support/json/encoding.rb +125 -286
- data/lib/active_support/key_generator.rb +8 -10
- data/lib/active_support/lazy_load_hooks.rb +1 -1
- data/lib/active_support/log_subscriber/test_helper.rb +1 -1
- data/lib/active_support/logger.rb +51 -1
- data/lib/active_support/logger_silence.rb +7 -4
- data/lib/active_support/logger_thread_safe_level.rb +32 -0
- data/lib/active_support/message_encryptor.rb +14 -6
- data/lib/active_support/message_verifier.rb +16 -12
- data/lib/active_support/multibyte/chars.rb +2 -3
- data/lib/active_support/multibyte/unicode.rb +46 -58
- data/lib/active_support/notifications/fanout.rb +12 -7
- data/lib/active_support/notifications/instrumenter.rb +2 -1
- data/lib/active_support/notifications.rb +11 -6
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +23 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +66 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +49 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +87 -0
- data/lib/active_support/number_helper.rb +32 -324
- data/lib/active_support/ordered_options.rb +8 -0
- data/lib/active_support/per_thread_registry.rb +13 -10
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/subscriber.rb +35 -3
- data/lib/active_support/test_case.rb +52 -19
- data/lib/active_support/testing/assertions.rb +1 -31
- data/lib/active_support/testing/autorun.rb +2 -2
- data/lib/active_support/testing/constant_lookup.rb +1 -5
- data/lib/active_support/testing/declarative.rb +7 -21
- data/lib/active_support/testing/isolation.rb +29 -69
- data/lib/active_support/testing/setup_and_teardown.rb +17 -2
- data/lib/active_support/testing/tagged_logging.rb +2 -2
- data/lib/active_support/testing/time_helpers.rb +134 -0
- data/lib/active_support/time.rb +0 -2
- data/lib/active_support/time_with_zone.rb +60 -40
- data/lib/active_support/values/time_zone.rb +101 -101
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +4 -7
- data/lib/active_support/xml_mini/jdom.rb +6 -5
- data/lib/active_support/xml_mini/libxml.rb +1 -3
- data/lib/active_support/xml_mini/libxmlsax.rb +1 -4
- data/lib/active_support/xml_mini/nokogiri.rb +1 -3
- data/lib/active_support/xml_mini/nokogirisax.rb +1 -3
- data/lib/active_support/xml_mini/rexml.rb +7 -8
- data/lib/active_support/xml_mini.rb +33 -15
- data/lib/active_support.rb +27 -2
- metadata +43 -43
- data/lib/active_support/basic_object.rb +0 -11
- data/lib/active_support/buffered_logger.rb +0 -21
- data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
- data/lib/active_support/core_ext/hash/diff.rb +0 -14
- data/lib/active_support/core_ext/logger.rb +0 -67
- data/lib/active_support/core_ext/object/to_json.rb +0 -27
- data/lib/active_support/core_ext/proc.rb +0 -17
- data/lib/active_support/core_ext/string/encoding.rb +0 -8
- data/lib/active_support/file_watcher.rb +0 -36
- data/lib/active_support/json/variable.rb +0 -18
- data/lib/active_support/testing/pending.rb +0 -14
@@ -0,0 +1,66 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module NumberHelper
|
3
|
+
class NumberToHumanConverter < NumberConverter # :nodoc:
|
4
|
+
DECIMAL_UNITS = { 0 => :unit, 1 => :ten, 2 => :hundred, 3 => :thousand, 6 => :million, 9 => :billion, 12 => :trillion, 15 => :quadrillion,
|
5
|
+
-1 => :deci, -2 => :centi, -3 => :mili, -6 => :micro, -9 => :nano, -12 => :pico, -15 => :femto }
|
6
|
+
INVERTED_DECIMAL_UNITS = DECIMAL_UNITS.invert
|
7
|
+
|
8
|
+
self.namespace = :human
|
9
|
+
self.validate_float = true
|
10
|
+
|
11
|
+
def convert # :nodoc:
|
12
|
+
@number = Float(number)
|
13
|
+
|
14
|
+
# for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
|
15
|
+
unless options.key?(:strip_insignificant_zeros)
|
16
|
+
options[:strip_insignificant_zeros] = true
|
17
|
+
end
|
18
|
+
|
19
|
+
units = opts[:units]
|
20
|
+
exponent = calculate_exponent(units)
|
21
|
+
@number = number / (10 ** exponent)
|
22
|
+
|
23
|
+
unit = determine_unit(units, exponent)
|
24
|
+
|
25
|
+
rounded_number = NumberToRoundedConverter.convert(number, options)
|
26
|
+
format.gsub(/%n/, rounded_number).gsub(/%u/, unit).strip
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def format
|
32
|
+
options[:format] || translate_in_locale('human.decimal_units.format')
|
33
|
+
end
|
34
|
+
|
35
|
+
def determine_unit(units, exponent)
|
36
|
+
exp = DECIMAL_UNITS[exponent]
|
37
|
+
case units
|
38
|
+
when Hash
|
39
|
+
units[exp] || ''
|
40
|
+
when String, Symbol
|
41
|
+
I18n.translate("#{units}.#{exp}", :locale => options[:locale], :count => number.to_i)
|
42
|
+
else
|
43
|
+
translate_in_locale("human.decimal_units.units.#{exp}", count: number.to_i)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def calculate_exponent(units)
|
48
|
+
exponent = number != 0 ? Math.log10(number.abs).floor : 0
|
49
|
+
unit_exponents(units).find { |e| exponent >= e } || 0
|
50
|
+
end
|
51
|
+
|
52
|
+
def unit_exponents(units)
|
53
|
+
case units
|
54
|
+
when Hash
|
55
|
+
units
|
56
|
+
when String, Symbol
|
57
|
+
I18n.translate(units.to_s, :locale => options[:locale], :raise => true)
|
58
|
+
when nil
|
59
|
+
translate_in_locale("human.decimal_units.units", raise: true)
|
60
|
+
else
|
61
|
+
raise ArgumentError, ":units must be a Hash or String translation scope."
|
62
|
+
end.keys.map { |e_name| INVERTED_DECIMAL_UNITS[e_name] }.sort_by { |e| -e }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module NumberHelper
|
3
|
+
class NumberToHumanSizeConverter < NumberConverter #:nodoc:
|
4
|
+
STORAGE_UNITS = [:byte, :kb, :mb, :gb, :tb]
|
5
|
+
|
6
|
+
self.namespace = :human
|
7
|
+
self.validate_float = true
|
8
|
+
|
9
|
+
def convert
|
10
|
+
@number = Float(number)
|
11
|
+
|
12
|
+
# for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files
|
13
|
+
unless options.key?(:strip_insignificant_zeros)
|
14
|
+
options[:strip_insignificant_zeros] = true
|
15
|
+
end
|
16
|
+
|
17
|
+
if smaller_than_base?
|
18
|
+
number_to_format = number.to_i.to_s
|
19
|
+
else
|
20
|
+
human_size = number / (base ** exponent)
|
21
|
+
number_to_format = NumberToRoundedConverter.convert(human_size, options)
|
22
|
+
end
|
23
|
+
conversion_format.gsub(/%n/, number_to_format).gsub(/%u/, unit)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def conversion_format
|
29
|
+
translate_number_value_with_default('human.storage_units.format', :locale => options[:locale], :raise => true)
|
30
|
+
end
|
31
|
+
|
32
|
+
def unit
|
33
|
+
translate_number_value_with_default(storage_unit_key, :locale => options[:locale], :count => number.to_i, :raise => true)
|
34
|
+
end
|
35
|
+
|
36
|
+
def storage_unit_key
|
37
|
+
key_end = smaller_than_base? ? 'byte' : STORAGE_UNITS[exponent]
|
38
|
+
"human.storage_units.units.#{key_end}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def exponent
|
42
|
+
max = STORAGE_UNITS.size - 1
|
43
|
+
exp = (Math.log(number) / Math.log(base)).to_i
|
44
|
+
exp = max if exp > max # avoid overflow for the highest unit
|
45
|
+
exp
|
46
|
+
end
|
47
|
+
|
48
|
+
def smaller_than_base?
|
49
|
+
number.to_i < base
|
50
|
+
end
|
51
|
+
|
52
|
+
def base
|
53
|
+
opts[:prefix] == :si ? 1000 : 1024
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module NumberHelper
|
3
|
+
class NumberToPercentageConverter < NumberConverter # :nodoc:
|
4
|
+
self.namespace = :percentage
|
5
|
+
|
6
|
+
def convert
|
7
|
+
rounded_number = NumberToRoundedConverter.convert(number, options)
|
8
|
+
options[:format].gsub(/%n/, rounded_number)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module NumberHelper
|
3
|
+
class NumberToPhoneConverter < NumberConverter #:nodoc:
|
4
|
+
def convert
|
5
|
+
str = country_code(opts[:country_code])
|
6
|
+
str << convert_to_phone_number(number.to_s.strip)
|
7
|
+
str << phone_ext(opts[:extension])
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def convert_to_phone_number(number)
|
13
|
+
if opts[:area_code]
|
14
|
+
convert_with_area_code(number)
|
15
|
+
else
|
16
|
+
convert_without_area_code(number)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def convert_with_area_code(number)
|
21
|
+
number.gsub!(/(\d{1,3})(\d{3})(\d{4}$)/,"(\\1) \\2#{delimiter}\\3")
|
22
|
+
number
|
23
|
+
end
|
24
|
+
|
25
|
+
def convert_without_area_code(number)
|
26
|
+
number.gsub!(/(\d{0,3})(\d{3})(\d{4})$/,"\\1#{delimiter}\\2#{delimiter}\\3")
|
27
|
+
number.slice!(0, 1) if start_with_delimiter?(number)
|
28
|
+
number
|
29
|
+
end
|
30
|
+
|
31
|
+
def start_with_delimiter?(number)
|
32
|
+
delimiter.present? && number.start_with?(delimiter)
|
33
|
+
end
|
34
|
+
|
35
|
+
def delimiter
|
36
|
+
opts[:delimiter] || "-"
|
37
|
+
end
|
38
|
+
|
39
|
+
def country_code(code)
|
40
|
+
code.blank? ? "" : "+#{code}#{delimiter}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def phone_ext(ext)
|
44
|
+
ext.blank? ? "" : " x #{ext}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module ActiveSupport
|
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
|
+
significant = options.delete :significant
|
10
|
+
|
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 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
|
+
|
39
|
+
delimited_number = NumberToDelimitedConverter.convert(formatted_string, options)
|
40
|
+
format_number(delimited_number)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def digits_and_rounded_number(precision)
|
46
|
+
if zero?
|
47
|
+
[1, 0]
|
48
|
+
else
|
49
|
+
digits = digit_count(number)
|
50
|
+
multiplier = 10 ** (digits - precision)
|
51
|
+
rounded_number = calculate_rounded_number(multiplier)
|
52
|
+
digits = digit_count(rounded_number) # After rounding, the number of digits may have changed
|
53
|
+
[digits, rounded_number]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def calculate_rounded_number(multiplier)
|
58
|
+
(number / BigDecimal.new(multiplier.to_f.to_s)).round * multiplier
|
59
|
+
end
|
60
|
+
|
61
|
+
def digit_count(number)
|
62
|
+
number.zero? ? 1 : (Math.log10(absolute_number(number)) + 1).floor
|
63
|
+
end
|
64
|
+
|
65
|
+
def strip_insignificant_zeros
|
66
|
+
options[:strip_insignificant_zeros]
|
67
|
+
end
|
68
|
+
|
69
|
+
def format_number(number)
|
70
|
+
if strip_insignificant_zeros
|
71
|
+
escaped_separator = Regexp.escape(options[:separator])
|
72
|
+
number.sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
|
73
|
+
else
|
74
|
+
number
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def absolute_number(number)
|
79
|
+
number.respond_to?(:abs) ? number.abs : number.to_d.abs
|
80
|
+
end
|
81
|
+
|
82
|
+
def zero?
|
83
|
+
number.respond_to?(:zero?) ? number.zero? : number.to_d.zero?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|