activesupport 4.2.11.3 → 5.0.7.2
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 +4 -4
- data/CHANGELOG.md +678 -348
- data/MIT-LICENSE +2 -2
- data/README.rdoc +2 -3
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +1 -1
- data/lib/active_support/benchmarkable.rb +1 -1
- data/lib/active_support/cache/file_store.rb +36 -22
- data/lib/active_support/cache/mem_cache_store.rb +63 -54
- data/lib/active_support/cache/memory_store.rb +16 -21
- data/lib/active_support/cache/null_store.rb +1 -4
- data/lib/active_support/cache/strategy/local_cache.rb +31 -20
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +4 -4
- data/lib/active_support/cache.rb +71 -87
- data/lib/active_support/callbacks.rb +109 -113
- data/lib/active_support/concern.rb +1 -1
- data/lib/active_support/concurrency/latch.rb +11 -12
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +1 -0
- data/lib/active_support/core_ext/array/access.rb +27 -1
- data/lib/active_support/core_ext/array/conversions.rb +6 -4
- data/lib/active_support/core_ext/array/grouping.rb +9 -18
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/wrap.rb +5 -4
- data/lib/active_support/core_ext/array.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -10
- data/lib/active_support/core_ext/class/attribute.rb +10 -9
- data/lib/active_support/core_ext/class/subclasses.rb +3 -2
- data/lib/active_support/core_ext/class.rb +0 -1
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +1 -1
- data/lib/active_support/core_ext/date/conversions.rb +7 -6
- data/lib/active_support/core_ext/date.rb +1 -1
- data/lib/active_support/core_ext/date_and_time/calculations.rb +100 -27
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +0 -1
- data/lib/active_support/core_ext/date_and_time/zones.rb +3 -4
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +14 -8
- data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
- data/lib/active_support/core_ext/date_time.rb +1 -1
- data/lib/active_support/core_ext/enumerable.rb +75 -25
- data/lib/active_support/core_ext/file/atomic.rb +30 -25
- data/lib/active_support/core_ext/hash/conversions.rb +22 -2
- data/lib/active_support/core_ext/hash/deep_merge.rb +1 -1
- data/lib/active_support/core_ext/hash/except.rb +9 -8
- data/lib/active_support/core_ext/hash/indifferent_access.rb +1 -1
- data/lib/active_support/core_ext/hash/keys.rb +25 -21
- data/lib/active_support/core_ext/hash/slice.rb +1 -1
- data/lib/active_support/core_ext/hash/transform_values.rb +11 -5
- data/lib/active_support/core_ext/integer/time.rb +2 -2
- data/lib/active_support/core_ext/kernel/concern.rb +2 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -10
- data/lib/active_support/core_ext/kernel/reporting.rb +2 -84
- data/lib/active_support/core_ext/kernel.rb +0 -1
- data/lib/active_support/core_ext/load_error.rb +5 -2
- data/lib/active_support/core_ext/marshal.rb +7 -9
- data/lib/active_support/core_ext/module/aliasing.rb +6 -1
- data/lib/active_support/core_ext/module/anonymous.rb +10 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
- data/lib/active_support/core_ext/module/attribute_accessors.rb +15 -15
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +4 -4
- data/lib/active_support/core_ext/module/delegation.rb +11 -20
- data/lib/active_support/core_ext/module/deprecation.rb +2 -2
- data/lib/active_support/core_ext/module/introspection.rb +8 -2
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -13
- data/lib/active_support/core_ext/module/qualified_const.rb +30 -12
- data/lib/active_support/core_ext/module/remove_method.rb +23 -0
- data/lib/active_support/core_ext/module.rb +1 -0
- data/lib/active_support/core_ext/name_error.rb +15 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +78 -77
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +26 -6
- data/lib/active_support/core_ext/numeric.rb +1 -0
- data/lib/active_support/core_ext/object/blank.rb +15 -3
- data/lib/active_support/core_ext/object/deep_dup.rb +10 -3
- data/lib/active_support/core_ext/object/duplicable.rb +7 -12
- data/lib/active_support/core_ext/object/inclusion.rb +2 -2
- data/lib/active_support/core_ext/object/instance_variables.rb +1 -1
- data/lib/active_support/core_ext/object/json.rb +15 -7
- data/lib/active_support/core_ext/object/to_query.rb +1 -1
- data/lib/active_support/core_ext/object/try.rb +67 -21
- data/lib/active_support/core_ext/object/with_options.rb +1 -1
- data/lib/active_support/core_ext/object.rb +0 -1
- data/lib/active_support/core_ext/range/conversions.rb +18 -6
- data/lib/active_support/core_ext/range/each.rb +16 -18
- data/lib/active_support/core_ext/range/include_range.rb +20 -20
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string/behavior.rb +1 -1
- data/lib/active_support/core_ext/string/conversions.rb +3 -2
- data/lib/active_support/core_ext/string/filters.rb +1 -2
- data/lib/active_support/core_ext/string/inflections.rb +32 -5
- data/lib/active_support/core_ext/string/multibyte.rb +11 -7
- data/lib/active_support/core_ext/string/output_safety.rb +12 -14
- data/lib/active_support/core_ext/string/strip.rb +3 -6
- data/lib/active_support/core_ext/struct.rb +3 -6
- data/lib/active_support/core_ext/time/calculations.rb +18 -9
- data/lib/active_support/core_ext/time/conversions.rb +4 -2
- data/lib/active_support/core_ext/time/marshal.rb +2 -29
- data/lib/active_support/core_ext/time/zones.rb +36 -4
- data/lib/active_support/core_ext/time.rb +0 -1
- data/lib/active_support/core_ext/uri.rb +1 -3
- data/lib/active_support/core_ext.rb +2 -1
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/dependencies.rb +88 -95
- data/lib/active_support/deprecation/behaviors.rb +15 -1
- data/lib/active_support/deprecation/instance_delegator.rb +13 -0
- data/lib/active_support/deprecation/method_wrappers.rb +42 -16
- data/lib/active_support/deprecation/proxy_wrappers.rb +47 -24
- data/lib/active_support/deprecation/reporting.rb +23 -5
- data/lib/active_support/deprecation.rb +1 -1
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/duration.rb +90 -15
- data/lib/active_support/evented_file_update_checker.rb +199 -0
- data/lib/active_support/execution_wrapper.rb +126 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +23 -3
- data/lib/active_support/gem_version.rb +5 -5
- data/lib/active_support/gzip.rb +1 -1
- data/lib/active_support/hash_with_indifferent_access.rb +40 -11
- data/lib/active_support/i18n_railtie.rb +25 -4
- data/lib/active_support/inflector/inflections.rb +36 -5
- data/lib/active_support/inflector/methods.rb +97 -90
- data/lib/active_support/inflector/transliterate.rb +36 -21
- data/lib/active_support/json/decoding.rb +11 -10
- data/lib/active_support/json/encoding.rb +1 -51
- data/lib/active_support/key_generator.rb +7 -9
- data/lib/active_support/lazy_load_hooks.rb +46 -18
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber/test_helper.rb +3 -3
- data/lib/active_support/log_subscriber.rb +1 -1
- data/lib/active_support/logger.rb +3 -4
- data/lib/active_support/logger_silence.rb +2 -1
- data/lib/active_support/logger_thread_safe_level.rb +2 -3
- data/lib/active_support/message_encryptor.rb +7 -7
- data/lib/active_support/message_verifier.rb +70 -8
- data/lib/active_support/multibyte/chars.rb +12 -3
- data/lib/active_support/multibyte/unicode.rb +44 -21
- data/lib/active_support/notifications/fanout.rb +5 -5
- data/lib/active_support/notifications/instrumenter.rb +20 -2
- data/lib/active_support/notifications.rb +2 -2
- data/lib/active_support/number_helper/number_to_currency_converter.rb +7 -9
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +8 -3
- data/lib/active_support/number_helper/number_to_human_converter.rb +6 -4
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +6 -2
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +1 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +11 -2
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +30 -25
- data/lib/active_support/number_helper.rb +90 -67
- data/lib/active_support/ordered_hash.rb +1 -1
- data/lib/active_support/ordered_options.rb +15 -1
- data/lib/active_support/per_thread_registry.rb +3 -0
- data/lib/active_support/rails.rb +2 -2
- data/lib/active_support/railtie.rb +6 -1
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +101 -47
- data/lib/active_support/string_inquirer.rb +1 -1
- data/lib/active_support/subscriber.rb +5 -10
- data/lib/active_support/tagged_logging.rb +8 -7
- data/lib/active_support/test_case.rb +17 -29
- data/lib/active_support/testing/assertions.rb +15 -13
- data/lib/active_support/testing/deprecation.rb +9 -8
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +22 -8
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/time_helpers.rb +3 -1
- data/lib/active_support/time_with_zone.rb +123 -33
- data/lib/active_support/values/time_zone.rb +101 -47
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/xml_mini/jdom.rb +1 -1
- data/lib/active_support/xml_mini/libxml.rb +2 -2
- data/lib/active_support/xml_mini/nokogiri.rb +2 -2
- data/lib/active_support.rb +11 -6
- metadata +36 -17
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
- data/lib/active_support/core_ext/date_time/zones.rb +0 -6
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- data/lib/active_support/core_ext/thread.rb +0 -86
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'active_support/core_ext/big_decimal/conversions'
|
2
2
|
require 'active_support/number_helper'
|
3
|
+
require 'active_support/core_ext/module/deprecation'
|
3
4
|
|
4
|
-
|
5
|
+
module ActiveSupport::NumericWithFormat
|
5
6
|
|
6
7
|
# Provides options for converting numbers into formatted strings.
|
7
8
|
# Options are provided for phone numbers, currency, percentage,
|
@@ -14,70 +15,72 @@ class Numeric
|
|
14
15
|
# ==== Examples
|
15
16
|
#
|
16
17
|
# Phone Numbers:
|
17
|
-
# 5551234.to_s(:phone) # => 555-1234
|
18
|
-
# 1235551234.to_s(:phone) # => 123-555-1234
|
19
|
-
# 1235551234.to_s(:phone, area_code: true) # => (123) 555-1234
|
20
|
-
# 1235551234.to_s(:phone, delimiter: ' ') # => 123 555 1234
|
21
|
-
# 1235551234.to_s(:phone, area_code: true, extension: 555) # => (123) 555-1234 x 555
|
22
|
-
# 1235551234.to_s(:phone, country_code: 1) # => +1-123-555-1234
|
18
|
+
# 5551234.to_s(:phone) # => "555-1234"
|
19
|
+
# 1235551234.to_s(:phone) # => "123-555-1234"
|
20
|
+
# 1235551234.to_s(:phone, area_code: true) # => "(123) 555-1234"
|
21
|
+
# 1235551234.to_s(:phone, delimiter: ' ') # => "123 555 1234"
|
22
|
+
# 1235551234.to_s(:phone, area_code: true, extension: 555) # => "(123) 555-1234 x 555"
|
23
|
+
# 1235551234.to_s(:phone, country_code: 1) # => "+1-123-555-1234"
|
23
24
|
# 1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.')
|
24
|
-
# # => +1.123.555.1234 x 1343
|
25
|
+
# # => "+1.123.555.1234 x 1343"
|
25
26
|
#
|
26
27
|
# Currency:
|
27
|
-
# 1234567890.50.to_s(:currency) # => $1,234,567,890.50
|
28
|
-
# 1234567890.506.to_s(:currency) # => $1,234,567,890.51
|
29
|
-
# 1234567890.506.to_s(:currency, precision: 3) # => $1,234,567,890.506
|
30
|
-
# 1234567890.506.to_s(:currency, locale: :fr) # => 1 234 567 890,51 €
|
28
|
+
# 1234567890.50.to_s(:currency) # => "$1,234,567,890.50"
|
29
|
+
# 1234567890.506.to_s(:currency) # => "$1,234,567,890.51"
|
30
|
+
# 1234567890.506.to_s(:currency, precision: 3) # => "$1,234,567,890.506"
|
31
|
+
# 1234567890.506.to_s(:currency, locale: :fr) # => "1 234 567 890,51 €"
|
31
32
|
# -1234567890.50.to_s(:currency, negative_format: '(%u%n)')
|
32
|
-
# # => ($1,234,567,890.50)
|
33
|
+
# # => "($1,234,567,890.50)"
|
33
34
|
# 1234567890.50.to_s(:currency, unit: '£', separator: ',', delimiter: '')
|
34
|
-
# # => £1234567890,50
|
35
|
+
# # => "£1234567890,50"
|
35
36
|
# 1234567890.50.to_s(:currency, unit: '£', separator: ',', delimiter: '', format: '%n %u')
|
36
|
-
# # => 1234567890,50 £
|
37
|
+
# # => "1234567890,50 £"
|
37
38
|
#
|
38
39
|
# Percentage:
|
39
|
-
# 100.to_s(:percentage) # => 100.000%
|
40
|
-
# 100.to_s(:percentage, precision: 0) # => 100%
|
41
|
-
# 1000.to_s(:percentage, delimiter: '.', separator: ',') # => 1.000,000%
|
42
|
-
# 302.24398923423.to_s(:percentage, precision: 5) # => 302.24399%
|
43
|
-
# 1000.to_s(:percentage, locale: :fr) # => 1 000,000%
|
44
|
-
# 100.to_s(:percentage, format: '%n %') # => 100.000 %
|
40
|
+
# 100.to_s(:percentage) # => "100.000%"
|
41
|
+
# 100.to_s(:percentage, precision: 0) # => "100%"
|
42
|
+
# 1000.to_s(:percentage, delimiter: '.', separator: ',') # => "1.000,000%"
|
43
|
+
# 302.24398923423.to_s(:percentage, precision: 5) # => "302.24399%"
|
44
|
+
# 1000.to_s(:percentage, locale: :fr) # => "1 000,000%"
|
45
|
+
# 100.to_s(:percentage, format: '%n %') # => "100.000 %"
|
45
46
|
#
|
46
47
|
# Delimited:
|
47
|
-
# 12345678.to_s(:delimited) # => 12,345,678
|
48
|
-
# 12345678.05.to_s(:delimited) # => 12,345,678.05
|
49
|
-
# 12345678.to_s(:delimited, delimiter: '.') # => 12.345.678
|
50
|
-
# 12345678.to_s(:delimited, delimiter: ',') # => 12,345,678
|
51
|
-
# 12345678.05.to_s(:delimited, separator: ' ') # => 12,345,678 05
|
52
|
-
# 12345678.05.to_s(:delimited, locale: :fr) # => 12 345 678,05
|
48
|
+
# 12345678.to_s(:delimited) # => "12,345,678"
|
49
|
+
# 12345678.05.to_s(:delimited) # => "12,345,678.05"
|
50
|
+
# 12345678.to_s(:delimited, delimiter: '.') # => "12.345.678"
|
51
|
+
# 12345678.to_s(:delimited, delimiter: ',') # => "12,345,678"
|
52
|
+
# 12345678.05.to_s(:delimited, separator: ' ') # => "12,345,678 05"
|
53
|
+
# 12345678.05.to_s(:delimited, locale: :fr) # => "12 345 678,05"
|
53
54
|
# 98765432.98.to_s(:delimited, delimiter: ' ', separator: ',')
|
54
|
-
# # => 98 765 432,98
|
55
|
+
# # => "98 765 432,98"
|
55
56
|
#
|
56
57
|
# Rounded:
|
57
|
-
# 111.2345.to_s(:rounded) # => 111.235
|
58
|
-
# 111.2345.to_s(:rounded, precision: 2) # => 111.23
|
59
|
-
# 13.to_s(:rounded, precision: 5) # => 13.00000
|
60
|
-
# 389.32314.to_s(:rounded, precision: 0) # => 389
|
61
|
-
# 111.2345.to_s(:rounded, significant: true) # => 111
|
62
|
-
# 111.2345.to_s(:rounded, precision: 1, significant: true) # => 100
|
63
|
-
# 13.to_s(:rounded, precision: 5, significant: true) # => 13.000
|
64
|
-
# 111.234.to_s(:rounded, locale: :fr) # => 111,234
|
58
|
+
# 111.2345.to_s(:rounded) # => "111.235"
|
59
|
+
# 111.2345.to_s(:rounded, precision: 2) # => "111.23"
|
60
|
+
# 13.to_s(:rounded, precision: 5) # => "13.00000"
|
61
|
+
# 389.32314.to_s(:rounded, precision: 0) # => "389"
|
62
|
+
# 111.2345.to_s(:rounded, significant: true) # => "111"
|
63
|
+
# 111.2345.to_s(:rounded, precision: 1, significant: true) # => "100"
|
64
|
+
# 13.to_s(:rounded, precision: 5, significant: true) # => "13.000"
|
65
|
+
# 111.234.to_s(:rounded, locale: :fr) # => "111,234"
|
65
66
|
# 13.to_s(:rounded, precision: 5, significant: true, strip_insignificant_zeros: true)
|
66
|
-
# # => 13
|
67
|
-
# 389.32314.to_s(:rounded, precision: 4, significant: true) # => 389.3
|
67
|
+
# # => "13"
|
68
|
+
# 389.32314.to_s(:rounded, precision: 4, significant: true) # => "389.3"
|
68
69
|
# 1111.2345.to_s(:rounded, precision: 2, separator: ',', delimiter: '.')
|
69
|
-
# # => 1.111,23
|
70
|
+
# # => "1.111,23"
|
70
71
|
#
|
71
72
|
# Human-friendly size in Bytes:
|
72
|
-
# 123.to_s(:human_size) # => 123 Bytes
|
73
|
-
# 1234.to_s(:human_size) # => 1.21 KB
|
74
|
-
# 12345.to_s(:human_size) # => 12.1 KB
|
75
|
-
# 1234567.to_s(:human_size) # => 1.18 MB
|
76
|
-
# 1234567890.to_s(:human_size) # => 1.15 GB
|
77
|
-
# 1234567890123.to_s(:human_size) # => 1.12 TB
|
78
|
-
#
|
79
|
-
#
|
80
|
-
# 1234567.to_s(:human_size, precision: 2
|
73
|
+
# 123.to_s(:human_size) # => "123 Bytes"
|
74
|
+
# 1234.to_s(:human_size) # => "1.21 KB"
|
75
|
+
# 12345.to_s(:human_size) # => "12.1 KB"
|
76
|
+
# 1234567.to_s(:human_size) # => "1.18 MB"
|
77
|
+
# 1234567890.to_s(:human_size) # => "1.15 GB"
|
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"
|
81
|
+
# 1234567.to_s(:human_size, precision: 2) # => "1.2 MB"
|
82
|
+
# 483989.to_s(:human_size, precision: 2) # => "470 KB"
|
83
|
+
# 1234567.to_s(:human_size, precision: 2, separator: ',') # => "1,2 MB"
|
81
84
|
# 1234567890123.to_s(:human_size, precision: 5) # => "1.1228 TB"
|
82
85
|
# 524288000.to_s(:human_size, precision: 5) # => "500 MB"
|
83
86
|
#
|
@@ -97,47 +100,45 @@ class Numeric
|
|
97
100
|
# 1234567.to_s(:human, precision: 1,
|
98
101
|
# separator: ',',
|
99
102
|
# significant: false) # => "1,2 Million"
|
100
|
-
def
|
103
|
+
def to_s(format = nil, options = nil)
|
101
104
|
case format
|
105
|
+
when nil
|
106
|
+
super()
|
107
|
+
when Integer, String
|
108
|
+
super(format)
|
102
109
|
when :phone
|
103
|
-
return ActiveSupport::NumberHelper.number_to_phone(self, options)
|
110
|
+
return ActiveSupport::NumberHelper.number_to_phone(self, options || {})
|
104
111
|
when :currency
|
105
|
-
return ActiveSupport::NumberHelper.number_to_currency(self, options)
|
112
|
+
return ActiveSupport::NumberHelper.number_to_currency(self, options || {})
|
106
113
|
when :percentage
|
107
|
-
return ActiveSupport::NumberHelper.number_to_percentage(self, options)
|
114
|
+
return ActiveSupport::NumberHelper.number_to_percentage(self, options || {})
|
108
115
|
when :delimited
|
109
|
-
return ActiveSupport::NumberHelper.number_to_delimited(self, options)
|
116
|
+
return ActiveSupport::NumberHelper.number_to_delimited(self, options || {})
|
110
117
|
when :rounded
|
111
|
-
return ActiveSupport::NumberHelper.number_to_rounded(self, options)
|
118
|
+
return ActiveSupport::NumberHelper.number_to_rounded(self, options || {})
|
112
119
|
when :human
|
113
|
-
return ActiveSupport::NumberHelper.number_to_human(self, options)
|
120
|
+
return ActiveSupport::NumberHelper.number_to_human(self, options || {})
|
114
121
|
when :human_size
|
115
|
-
return ActiveSupport::NumberHelper.number_to_human_size(self, options)
|
122
|
+
return ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
|
123
|
+
when Symbol
|
124
|
+
super()
|
116
125
|
else
|
117
|
-
|
126
|
+
super(format)
|
118
127
|
end
|
119
128
|
end
|
120
129
|
|
121
|
-
|
122
|
-
|
123
|
-
if 0.class == Integer
|
124
|
-
klasses << Integer
|
125
|
-
else
|
126
|
-
klasses << Fixnum << Bignum
|
130
|
+
def to_formatted_s(*args)
|
131
|
+
to_s(*args)
|
127
132
|
end
|
133
|
+
deprecate to_formatted_s: :to_s
|
134
|
+
end
|
128
135
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
options = args[1] || {}
|
136
|
-
|
137
|
-
self.to_formatted_s(format, options)
|
138
|
-
else
|
139
|
-
to_default_s(*args)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
136
|
+
# Ruby 2.4+ unifies Fixnum & Bignum into Integer.
|
137
|
+
if 0.class == Integer
|
138
|
+
Integer.prepend ActiveSupport::NumericWithFormat
|
139
|
+
else
|
140
|
+
Fixnum.prepend ActiveSupport::NumericWithFormat
|
141
|
+
Bignum.prepend ActiveSupport::NumericWithFormat
|
143
142
|
end
|
143
|
+
Float.prepend ActiveSupport::NumericWithFormat
|
144
|
+
BigDecimal.prepend ActiveSupport::NumericWithFormat
|
@@ -0,0 +1,26 @@
|
|
1
|
+
unless 1.respond_to?(:positive?) # TODO: Remove this file when we drop support to ruby < 2.3
|
2
|
+
class Numeric
|
3
|
+
# Returns true if the number is positive.
|
4
|
+
#
|
5
|
+
# 1.positive? # => true
|
6
|
+
# 0.positive? # => false
|
7
|
+
# -1.positive? # => false
|
8
|
+
def positive?
|
9
|
+
self > 0
|
10
|
+
end
|
11
|
+
|
12
|
+
# Returns true if the number is negative.
|
13
|
+
#
|
14
|
+
# -1.negative? # => true
|
15
|
+
# 0.negative? # => false
|
16
|
+
# 1.negative? # => false
|
17
|
+
def negative?
|
18
|
+
self < 0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Complex
|
23
|
+
undef :positive?
|
24
|
+
undef :negative?
|
25
|
+
end
|
26
|
+
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'active_support/duration'
|
2
2
|
require 'active_support/core_ext/time/calculations'
|
3
3
|
require 'active_support/core_ext/time/acts_like'
|
4
|
+
require 'active_support/core_ext/date/calculations'
|
5
|
+
require 'active_support/core_ext/date/acts_like'
|
4
6
|
|
5
7
|
class Numeric
|
6
8
|
# Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
|
@@ -17,37 +19,55 @@ class Numeric
|
|
17
19
|
# # equivalent to Time.current.advance(months: 4, years: 5)
|
18
20
|
# (4.months + 5.years).from_now
|
19
21
|
def seconds
|
20
|
-
ActiveSupport::Duration.
|
22
|
+
ActiveSupport::Duration.seconds(self)
|
21
23
|
end
|
22
24
|
alias :second :seconds
|
23
25
|
|
26
|
+
# Returns a Duration instance matching the number of minutes provided.
|
27
|
+
#
|
28
|
+
# 2.minutes # => 2 minutes
|
24
29
|
def minutes
|
25
|
-
ActiveSupport::Duration.
|
30
|
+
ActiveSupport::Duration.minutes(self)
|
26
31
|
end
|
27
32
|
alias :minute :minutes
|
28
33
|
|
34
|
+
# Returns a Duration instance matching the number of hours provided.
|
35
|
+
#
|
36
|
+
# 2.hours # => 2 hours
|
29
37
|
def hours
|
30
|
-
ActiveSupport::Duration.
|
38
|
+
ActiveSupport::Duration.hours(self)
|
31
39
|
end
|
32
40
|
alias :hour :hours
|
33
41
|
|
42
|
+
# Returns a Duration instance matching the number of days provided.
|
43
|
+
#
|
44
|
+
# 2.days # => 2 days
|
34
45
|
def days
|
35
|
-
ActiveSupport::Duration.
|
46
|
+
ActiveSupport::Duration.days(self)
|
36
47
|
end
|
37
48
|
alias :day :days
|
38
49
|
|
50
|
+
# Returns a Duration instance matching the number of weeks provided.
|
51
|
+
#
|
52
|
+
# 2.weeks # => 2 weeks
|
39
53
|
def weeks
|
40
|
-
ActiveSupport::Duration.
|
54
|
+
ActiveSupport::Duration.weeks(self)
|
41
55
|
end
|
42
56
|
alias :week :weeks
|
43
57
|
|
58
|
+
# Returns a Duration instance matching the number of fortnights provided.
|
59
|
+
#
|
60
|
+
# 2.fortnights # => 4 weeks
|
44
61
|
def fortnights
|
45
|
-
ActiveSupport::Duration.
|
62
|
+
ActiveSupport::Duration.weeks(self * 2)
|
46
63
|
end
|
47
64
|
alias :fortnight :fortnights
|
48
65
|
|
66
|
+
# Returns the number of milliseconds equivalent to the seconds provided.
|
49
67
|
# Used with the standard time durations, like 1.hour.in_milliseconds --
|
50
68
|
# so we can feed them to JavaScript functions like getTime().
|
69
|
+
#
|
70
|
+
# 2.in_milliseconds # => 2_000
|
51
71
|
def in_milliseconds
|
52
72
|
self * 1000
|
53
73
|
end
|
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
1
|
class Object
|
4
2
|
# An object is blank if it's false, empty, or a whitespace string.
|
5
3
|
# For example, +false+, '', ' ', +nil+, [], and {} are all blank.
|
@@ -114,7 +112,10 @@ class String
|
|
114
112
|
#
|
115
113
|
# @return [true, false]
|
116
114
|
def blank?
|
117
|
-
|
115
|
+
# The regexp that matches blank strings is expensive. For the case of empty
|
116
|
+
# strings we can speed up this method (~3.5x) with an empty? call. The
|
117
|
+
# penalty for the rest of strings is marginal.
|
118
|
+
empty? || BLANK_RE === self
|
118
119
|
end
|
119
120
|
end
|
120
121
|
|
@@ -129,3 +130,14 @@ class Numeric #:nodoc:
|
|
129
130
|
false
|
130
131
|
end
|
131
132
|
end
|
133
|
+
|
134
|
+
class Time #:nodoc:
|
135
|
+
# No Time is blank:
|
136
|
+
#
|
137
|
+
# Time.now.blank? # => false
|
138
|
+
#
|
139
|
+
# @return [false]
|
140
|
+
def blank?
|
141
|
+
false
|
142
|
+
end
|
143
|
+
end
|
@@ -25,7 +25,7 @@ class Array
|
|
25
25
|
# array[1][2] # => nil
|
26
26
|
# dup[1][2] # => 4
|
27
27
|
def deep_dup
|
28
|
-
map
|
28
|
+
map(&:deep_dup)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -39,8 +39,15 @@ class Hash
|
|
39
39
|
# hash[:a][:c] # => nil
|
40
40
|
# dup[:a][:c] # => "c"
|
41
41
|
def deep_dup
|
42
|
-
|
43
|
-
|
42
|
+
hash = dup
|
43
|
+
each_pair do |key, value|
|
44
|
+
if key.frozen? && ::String === key
|
45
|
+
hash[key] = value.deep_dup
|
46
|
+
else
|
47
|
+
hash.delete(key)
|
48
|
+
hash[key.deep_dup] = value.deep_dup
|
49
|
+
end
|
44
50
|
end
|
51
|
+
hash
|
45
52
|
end
|
46
53
|
end
|
@@ -19,7 +19,7 @@
|
|
19
19
|
class Object
|
20
20
|
# Can you safely dup this object?
|
21
21
|
#
|
22
|
-
# False for
|
22
|
+
# False for method objects;
|
23
23
|
# true otherwise.
|
24
24
|
def duplicable?
|
25
25
|
true
|
@@ -104,17 +104,12 @@ end
|
|
104
104
|
|
105
105
|
require 'bigdecimal'
|
106
106
|
class BigDecimal
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
def duplicable?
|
114
|
-
true
|
115
|
-
end
|
116
|
-
rescue TypeError
|
117
|
-
# can't dup, so use superclass implementation
|
107
|
+
# BigDecimals are duplicable:
|
108
|
+
#
|
109
|
+
# BigDecimal("1.2").duplicable? # => true
|
110
|
+
# BigDecimal("1.2").dup # => #<BigDecimal:...,'0.12E1',18(18)>
|
111
|
+
def duplicable?
|
112
|
+
true
|
118
113
|
end
|
119
114
|
end
|
120
115
|
|
@@ -5,7 +5,7 @@ class Object
|
|
5
5
|
# characters = ["Konata", "Kagami", "Tsukasa"]
|
6
6
|
# "Konata".in?(characters) # => true
|
7
7
|
#
|
8
|
-
# This will throw an ArgumentError if the argument doesn't respond
|
8
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond
|
9
9
|
# to +#include?+.
|
10
10
|
def in?(another_object)
|
11
11
|
another_object.include?(self)
|
@@ -18,7 +18,7 @@ class Object
|
|
18
18
|
#
|
19
19
|
# params[:bucket_type].presence_in %w( project calendar )
|
20
20
|
#
|
21
|
-
# This will throw an ArgumentError if the argument doesn't respond to +#include?+.
|
21
|
+
# This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
|
22
22
|
#
|
23
23
|
# @return [Object]
|
24
24
|
def presence_in(another_object)
|
@@ -9,7 +9,6 @@ require 'time'
|
|
9
9
|
require 'active_support/core_ext/time/conversions'
|
10
10
|
require 'active_support/core_ext/date_time/conversions'
|
11
11
|
require 'active_support/core_ext/date/conversions'
|
12
|
-
require 'active_support/core_ext/module/aliasing'
|
13
12
|
|
14
13
|
# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
|
15
14
|
# their default behavior. That said, we need to define the basic to_json method in all of them,
|
@@ -26,22 +25,25 @@ require 'active_support/core_ext/module/aliasing'
|
|
26
25
|
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
|
27
26
|
# ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
|
28
27
|
# should give exactly the same results with or without active support.
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
|
29
|
+
module ActiveSupport
|
30
|
+
module ToJsonWithActiveSupportEncoder # :nodoc:
|
31
|
+
def to_json(options = nil)
|
32
32
|
if options.is_a?(::JSON::State)
|
33
33
|
# Called from JSON.{generate,dump}, forward it to JSON gem's to_json
|
34
|
-
|
34
|
+
super(options)
|
35
35
|
else
|
36
36
|
# to_json is being invoked directly, use ActiveSupport's encoder
|
37
37
|
ActiveSupport::JSON.encode(self, options)
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
41
|
-
alias_method_chain :to_json, :active_support_encoder
|
42
40
|
end
|
43
41
|
end
|
44
42
|
|
43
|
+
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].reverse_each do |klass|
|
44
|
+
klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
|
45
|
+
end
|
46
|
+
|
45
47
|
class Object
|
46
48
|
def as_json(options = nil) #:nodoc:
|
47
49
|
if respond_to?(:to_hash)
|
@@ -195,3 +197,9 @@ class Process::Status #:nodoc:
|
|
195
197
|
{ :exitstatus => exitstatus, :pid => pid }
|
196
198
|
end
|
197
199
|
end
|
200
|
+
|
201
|
+
class Exception
|
202
|
+
def as_json(options = nil)
|
203
|
+
to_s
|
204
|
+
end
|
205
|
+
end
|
@@ -38,7 +38,7 @@ class Array
|
|
38
38
|
# Calls <tt>to_param</tt> on all its elements and joins the result with
|
39
39
|
# slashes. This is used by <tt>url_for</tt> in Action Pack.
|
40
40
|
def to_param
|
41
|
-
collect
|
41
|
+
collect(&:to_param).join '/'
|
42
42
|
end
|
43
43
|
|
44
44
|
# Converts an array into a string suitable for use as a URL query string,
|
@@ -1,4 +1,34 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module Tryable #:nodoc:
|
5
|
+
def try(*a, &b)
|
6
|
+
try!(*a, &b) if a.empty? || respond_to?(a.first)
|
7
|
+
end
|
8
|
+
|
9
|
+
def try!(*a, &b)
|
10
|
+
if a.empty? && block_given?
|
11
|
+
if b.arity == 0
|
12
|
+
instance_eval(&b)
|
13
|
+
else
|
14
|
+
yield self
|
15
|
+
end
|
16
|
+
else
|
17
|
+
public_send(*a, &b)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
1
23
|
class Object
|
24
|
+
include ActiveSupport::Tryable
|
25
|
+
|
26
|
+
##
|
27
|
+
# :method: try
|
28
|
+
#
|
29
|
+
# :call-seq:
|
30
|
+
# try(*a, &b)
|
31
|
+
#
|
2
32
|
# Invokes the public method whose name goes as first argument just like
|
3
33
|
# +public_send+ does, except that if the receiver does not respond to it the
|
4
34
|
# call returns +nil+ rather than raising an exception.
|
@@ -21,11 +51,11 @@ class Object
|
|
21
51
|
#
|
22
52
|
# +try+ will also return +nil+ if the receiver does not respond to the method:
|
23
53
|
#
|
24
|
-
# @person.try(:non_existing_method)
|
54
|
+
# @person.try(:non_existing_method) # => nil
|
25
55
|
#
|
26
56
|
# instead of
|
27
57
|
#
|
28
|
-
# @person.non_existing_method if @person.respond_to?(:non_existing_method)
|
58
|
+
# @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
|
29
59
|
#
|
30
60
|
# +try+ returns +nil+ when called on +nil+ regardless of whether it responds
|
31
61
|
# to the method:
|
@@ -56,27 +86,40 @@ class Object
|
|
56
86
|
#
|
57
87
|
# Please also note that +try+ is defined on +Object+. Therefore, it won't work
|
58
88
|
# with instances of classes that do not have +Object+ among their ancestors,
|
59
|
-
# like direct subclasses of +BasicObject+.
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
89
|
+
# like direct subclasses of +BasicObject+.
|
90
|
+
|
91
|
+
##
|
92
|
+
# :method: try!
|
93
|
+
#
|
94
|
+
# :call-seq:
|
95
|
+
# try!(*a, &b)
|
96
|
+
#
|
97
|
+
# Same as #try, but raises a +NoMethodError+ exception if the receiver is
|
98
|
+
# not +nil+ and does not implement the tried method.
|
99
|
+
#
|
100
|
+
# "a".try!(:upcase) # => "A"
|
101
|
+
# nil.try!(:upcase) # => nil
|
102
|
+
# 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer
|
103
|
+
end
|
65
104
|
|
66
|
-
|
67
|
-
|
105
|
+
class Delegator
|
106
|
+
include ActiveSupport::Tryable
|
68
107
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
108
|
+
##
|
109
|
+
# :method: try
|
110
|
+
#
|
111
|
+
# :call-seq:
|
112
|
+
# try(a*, &b)
|
113
|
+
#
|
114
|
+
# See Object#try
|
115
|
+
|
116
|
+
##
|
117
|
+
# :method: try!
|
118
|
+
#
|
119
|
+
# :call-seq:
|
120
|
+
# try!(a*, &b)
|
121
|
+
#
|
122
|
+
# See Object#try!
|
80
123
|
end
|
81
124
|
|
82
125
|
class NilClass
|
@@ -94,6 +137,9 @@ class NilClass
|
|
94
137
|
nil
|
95
138
|
end
|
96
139
|
|
140
|
+
# Calling +try!+ on +nil+ always returns +nil+.
|
141
|
+
#
|
142
|
+
# nil.try!(:name) # => nil
|
97
143
|
def try!(*args)
|
98
144
|
nil
|
99
145
|
end
|
@@ -7,7 +7,7 @@ class Object
|
|
7
7
|
# provided. Each method called on the block variable must take an options
|
8
8
|
# hash as its final argument.
|
9
9
|
#
|
10
|
-
# Without <tt>with_options
|
10
|
+
# Without <tt>with_options</tt>, this code contains duplication:
|
11
11
|
#
|
12
12
|
# class Account < ActiveRecord::Base
|
13
13
|
# has_many :customers, dependent: :destroy
|
@@ -2,7 +2,6 @@ require 'active_support/core_ext/object/acts_like'
|
|
2
2
|
require 'active_support/core_ext/object/blank'
|
3
3
|
require 'active_support/core_ext/object/duplicable'
|
4
4
|
require 'active_support/core_ext/object/deep_dup'
|
5
|
-
require 'active_support/core_ext/object/itself'
|
6
5
|
require 'active_support/core_ext/object/try'
|
7
6
|
require 'active_support/core_ext/object/inclusion'
|
8
7
|
|