activesupport 4.2.11.1 → 6.1.7.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 +4 -4
- data/CHANGELOG.md +464 -391
- data/MIT-LICENSE +2 -2
- data/README.rdoc +7 -7
- data/lib/active_support/actionable_error.rb +48 -0
- data/lib/active_support/all.rb +5 -3
- data/lib/active_support/array_inquirer.rb +50 -0
- data/lib/active_support/backtrace_cleaner.rb +34 -6
- data/lib/active_support/benchmarkable.rb +6 -4
- data/lib/active_support/builder.rb +3 -1
- data/lib/active_support/cache/file_store.rb +61 -55
- data/lib/active_support/cache/mem_cache_store.rb +115 -100
- data/lib/active_support/cache/memory_store.rb +81 -58
- data/lib/active_support/cache/null_store.rb +11 -7
- data/lib/active_support/cache/redis_cache_store.rb +493 -0
- data/lib/active_support/cache/strategy/local_cache.rb +90 -42
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
- data/lib/active_support/cache.rb +386 -225
- data/lib/active_support/callbacks.rb +661 -594
- data/lib/active_support/concern.rb +80 -7
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +35 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +16 -17
- data/lib/active_support/configuration_file.rb +51 -0
- data/lib/active_support/core_ext/array/access.rb +41 -1
- data/lib/active_support/core_ext/array/conversions.rb +24 -20
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/extract_options.rb +2 -0
- data/lib/active_support/core_ext/array/grouping.rb +11 -18
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/wrap.rb +7 -4
- data/lib/active_support/core_ext/array.rb +9 -6
- data/lib/active_support/core_ext/benchmark.rb +5 -3
- data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
- data/lib/active_support/core_ext/big_decimal.rb +3 -1
- data/lib/active_support/core_ext/class/attribute.rb +52 -48
- data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
- data/lib/active_support/core_ext/class/subclasses.rb +18 -25
- data/lib/active_support/core_ext/class.rb +4 -3
- data/lib/active_support/core_ext/date/acts_like.rb +3 -1
- data/lib/active_support/core_ext/date/blank.rb +14 -0
- data/lib/active_support/core_ext/date/calculations.rb +17 -14
- data/lib/active_support/core_ext/date/conversions.rb +27 -24
- data/lib/active_support/core_ext/date/zones.rb +4 -2
- data/lib/active_support/core_ext/date.rb +6 -4
- data/lib/active_support/core_ext/date_and_time/calculations.rb +167 -65
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +19 -3
- data/lib/active_support/core_ext/date_and_time/zones.rb +12 -13
- data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
- data/lib/active_support/core_ext/date_time/blank.rb +14 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +37 -19
- data/lib/active_support/core_ext/date_time/compatibility.rb +8 -6
- data/lib/active_support/core_ext/date_time/conversions.rb +16 -13
- data/lib/active_support/core_ext/date_time.rb +7 -5
- data/lib/active_support/core_ext/digest/uuid.rb +8 -5
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +186 -22
- data/lib/active_support/core_ext/file/atomic.rb +38 -31
- data/lib/active_support/core_ext/file.rb +3 -1
- data/lib/active_support/core_ext/hash/conversions.rb +62 -41
- data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +13 -10
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
- data/lib/active_support/core_ext/hash/keys.rb +20 -43
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +8 -29
- data/lib/active_support/core_ext/hash.rb +10 -9
- data/lib/active_support/core_ext/integer/inflections.rb +3 -1
- data/lib/active_support/core_ext/integer/multiple.rb +3 -1
- data/lib/active_support/core_ext/integer/time.rb +11 -18
- data/lib/active_support/core_ext/integer.rb +5 -3
- data/lib/active_support/core_ext/kernel/concern.rb +5 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +4 -84
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/kernel.rb +5 -5
- data/lib/active_support/core_ext/load_error.rb +3 -22
- data/lib/active_support/core_ext/marshal.rb +10 -8
- data/lib/active_support/core_ext/module/aliasing.rb +6 -44
- data/lib/active_support/core_ext/module/anonymous.rb +12 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
- data/lib/active_support/core_ext/module/attribute_accessors.rb +63 -69
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +148 -0
- data/lib/active_support/core_ext/module/concerning.rb +19 -14
- data/lib/active_support/core_ext/module/delegation.rb +164 -51
- data/lib/active_support/core_ext/module/deprecation.rb +4 -2
- data/lib/active_support/core_ext/module/introspection.rb +23 -22
- data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
- data/lib/active_support/core_ext/module/remove_method.rb +8 -3
- data/lib/active_support/core_ext/module.rb +13 -11
- data/lib/active_support/core_ext/name_error.rb +51 -4
- data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +133 -136
- data/lib/active_support/core_ext/numeric/time.rb +35 -23
- data/lib/active_support/core_ext/numeric.rb +5 -3
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +27 -3
- data/lib/active_support/core_ext/object/conversions.rb +6 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
- data/lib/active_support/core_ext/object/duplicable.rb +13 -93
- data/lib/active_support/core_ext/object/inclusion.rb +5 -3
- data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
- data/lib/active_support/core_ext/object/json.rb +63 -21
- data/lib/active_support/core_ext/object/to_param.rb +3 -1
- data/lib/active_support/core_ext/object/to_query.rb +10 -5
- data/lib/active_support/core_ext/object/try.rb +81 -23
- data/lib/active_support/core_ext/object/with_options.rb +16 -3
- data/lib/active_support/core_ext/object.rb +14 -13
- data/lib/active_support/core_ext/range/compare_range.rb +82 -0
- data/lib/active_support/core_ext/range/conversions.rb +37 -15
- data/lib/active_support/core_ext/range/each.rb +18 -17
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +28 -0
- data/lib/active_support/core_ext/range/overlaps.rb +2 -0
- data/lib/active_support/core_ext/range.rb +7 -4
- data/lib/active_support/core_ext/regexp.rb +10 -1
- data/lib/active_support/core_ext/securerandom.rb +45 -0
- data/lib/active_support/core_ext/string/access.rb +9 -18
- data/lib/active_support/core_ext/string/behavior.rb +3 -1
- data/lib/active_support/core_ext/string/conversions.rb +8 -4
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +48 -6
- data/lib/active_support/core_ext/string/indent.rb +6 -4
- data/lib/active_support/core_ext/string/inflections.rb +102 -26
- data/lib/active_support/core_ext/string/inquiry.rb +4 -1
- data/lib/active_support/core_ext/string/multibyte.rb +18 -9
- data/lib/active_support/core_ext/string/output_safety.rb +125 -40
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
- data/lib/active_support/core_ext/string/strip.rb +6 -5
- data/lib/active_support/core_ext/string/zones.rb +4 -2
- data/lib/active_support/core_ext/string.rb +15 -13
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +14 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/time/acts_like.rb +3 -1
- data/lib/active_support/core_ext/time/calculations.rb +137 -53
- data/lib/active_support/core_ext/time/compatibility.rb +4 -2
- data/lib/active_support/core_ext/time/conversions.rb +22 -13
- data/lib/active_support/core_ext/time/zones.rb +41 -7
- data/lib/active_support/core_ext/time.rb +7 -6
- data/lib/active_support/core_ext/uri.rb +11 -8
- data/lib/active_support/core_ext.rb +3 -1
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +210 -0
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/dependencies/zeitwerk_integration.rb +120 -0
- data/lib/active_support/dependencies.rb +241 -175
- data/lib/active_support/deprecation/behaviors.rb +58 -12
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/disallowed.rb +56 -0
- data/lib/active_support/deprecation/instance_delegator.rb +16 -2
- data/lib/active_support/deprecation/method_wrappers.rb +62 -21
- data/lib/active_support/deprecation/proxy_wrappers.rb +81 -30
- data/lib/active_support/deprecation/reporting.rb +81 -18
- data/lib/active_support/deprecation.rb +17 -9
- data/lib/active_support/descendants_tracker.rb +61 -9
- data/lib/active_support/digest.rb +22 -0
- data/lib/active_support/duration/iso8601_parser.rb +123 -0
- data/lib/active_support/duration/iso8601_serializer.rb +59 -0
- data/lib/active_support/duration.rb +364 -39
- data/lib/active_support/encrypted_configuration.rb +45 -0
- data/lib/active_support/encrypted_file.rb +117 -0
- data/lib/active_support/environment_inquirer.rb +20 -0
- data/lib/active_support/evented_file_update_checker.rb +170 -0
- data/lib/active_support/execution_wrapper.rb +132 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +62 -37
- data/lib/active_support/fork_tracker.rb +64 -0
- data/lib/active_support/gem_version.rb +7 -5
- data/lib/active_support/gzip.rb +7 -5
- data/lib/active_support/hash_with_indifferent_access.rb +171 -48
- data/lib/active_support/i18n.rb +9 -6
- data/lib/active_support/i18n_railtie.rb +47 -16
- data/lib/active_support/inflections.rb +13 -11
- data/lib/active_support/inflector/inflections.rb +58 -14
- data/lib/active_support/inflector/methods.rb +186 -169
- data/lib/active_support/inflector/transliterate.rb +83 -33
- data/lib/active_support/inflector.rb +7 -5
- data/lib/active_support/json/decoding.rb +32 -30
- data/lib/active_support/json/encoding.rb +22 -61
- data/lib/active_support/json.rb +4 -2
- data/lib/active_support/key_generator.rb +11 -43
- data/lib/active_support/lazy_load_hooks.rb +53 -20
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +9 -3
- data/lib/active_support/log_subscriber/test_helper.rb +14 -12
- data/lib/active_support/log_subscriber.rb +52 -19
- data/lib/active_support/logger.rb +10 -24
- data/lib/active_support/logger_silence.rb +14 -20
- data/lib/active_support/logger_thread_safe_level.rb +56 -10
- data/lib/active_support/message_encryptor.rb +167 -57
- data/lib/active_support/message_verifier.rb +151 -18
- data/lib/active_support/messages/metadata.rb +80 -0
- data/lib/active_support/messages/rotation_configuration.rb +23 -0
- data/lib/active_support/messages/rotator.rb +57 -0
- data/lib/active_support/multibyte/chars.rb +35 -80
- data/lib/active_support/multibyte/unicode.rb +22 -330
- data/lib/active_support/multibyte.rb +4 -2
- data/lib/active_support/notifications/fanout.rb +125 -23
- data/lib/active_support/notifications/instrumenter.rb +98 -16
- data/lib/active_support/notifications.rb +82 -14
- data/lib/active_support/number_helper/number_converter.rb +17 -16
- data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -14
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +11 -4
- data/lib/active_support/number_helper/number_to_human_converter.rb +14 -11
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +12 -10
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +5 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +15 -5
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +29 -57
- data/lib/active_support/number_helper/rounding_helper.rb +50 -0
- data/lib/active_support/number_helper.rb +123 -71
- data/lib/active_support/option_merger.rb +25 -4
- data/lib/active_support/ordered_hash.rb +7 -5
- data/lib/active_support/ordered_options.rb +35 -7
- data/lib/active_support/parameter_filter.rb +133 -0
- data/lib/active_support/per_thread_registry.rb +10 -4
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +10 -11
- data/lib/active_support/railtie.rb +66 -10
- data/lib/active_support/reloader.rb +130 -0
- data/lib/active_support/rescuable.rb +112 -57
- data/lib/active_support/secure_compare_rotator.rb +51 -0
- data/lib/active_support/security_utils.rb +26 -15
- data/lib/active_support/string_inquirer.rb +13 -4
- data/lib/active_support/subscriber.rb +80 -31
- data/lib/active_support/tagged_logging.rb +54 -17
- data/lib/active_support/test_case.rb +107 -44
- data/lib/active_support/testing/assertions.rb +158 -20
- data/lib/active_support/testing/autorun.rb +4 -2
- data/lib/active_support/testing/constant_lookup.rb +2 -1
- data/lib/active_support/testing/declarative.rb +3 -1
- data/lib/active_support/testing/deprecation.rb +13 -10
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +35 -26
- data/lib/active_support/testing/method_call_assertions.rb +70 -0
- data/lib/active_support/testing/parallelization/server.rb +78 -0
- data/lib/active_support/testing/parallelization/worker.rb +100 -0
- data/lib/active_support/testing/parallelization.rb +51 -0
- data/lib/active_support/testing/setup_and_teardown.rb +13 -8
- data/lib/active_support/testing/stream.rb +43 -0
- data/lib/active_support/testing/tagged_logging.rb +3 -1
- data/lib/active_support/testing/time_helpers.rb +121 -20
- data/lib/active_support/time.rb +14 -12
- data/lib/active_support/time_with_zone.rb +215 -51
- data/lib/active_support/values/time_zone.rb +223 -71
- data/lib/active_support/version.rb +3 -1
- data/lib/active_support/xml_mini/jdom.rb +116 -115
- data/lib/active_support/xml_mini/libxml.rb +16 -13
- data/lib/active_support/xml_mini/libxmlsax.rb +15 -14
- data/lib/active_support/xml_mini/nokogiri.rb +14 -12
- data/lib/active_support/xml_mini/nokogirisax.rb +14 -13
- data/lib/active_support/xml_mini/rexml.rb +18 -9
- data/lib/active_support/xml_mini.rb +38 -46
- data/lib/active_support.rb +25 -11
- metadata +100 -43
- data/lib/active_support/concurrency/latch.rb +0 -27
- data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
- 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/hash/compact.rb +0 -24
- data/lib/active_support/core_ext/hash/transform_values.rb +0 -23
- data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
- data/lib/active_support/core_ext/module/method_transplanting.rb +0 -13
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
- data/lib/active_support/core_ext/module/reachable.rb +0 -8
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- data/lib/active_support/core_ext/range/include_range.rb +0 -23
- data/lib/active_support/core_ext/struct.rb +0 -6
- data/lib/active_support/core_ext/thread.rb +0 -86
- data/lib/active_support/core_ext/time/marshal.rb +0 -30
- data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -1,8 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/xml_mini"
|
4
|
+
require "active_support/core_ext/hash/keys"
|
5
|
+
require "active_support/core_ext/string/inflections"
|
6
|
+
require "active_support/core_ext/object/to_param"
|
7
|
+
require "active_support/core_ext/object/to_query"
|
6
8
|
|
7
9
|
class Array
|
8
10
|
# Converts the array to a comma-separated sentence where the last element is
|
@@ -32,7 +34,7 @@ class Array
|
|
32
34
|
# ['one', 'two', 'three'].to_sentence # => "one, two, and three"
|
33
35
|
#
|
34
36
|
# ['one', 'two'].to_sentence(passing: 'invalid option')
|
35
|
-
# # => ArgumentError: Unknown key :passing
|
37
|
+
# # => ArgumentError: Unknown key: :passing. Valid keys are: :words_connector, :two_words_connector, :last_word_connector, :locale
|
36
38
|
#
|
37
39
|
# ['one', 'two'].to_sentence(two_words_connector: '-')
|
38
40
|
# # => "one-two"
|
@@ -60,9 +62,9 @@ class Array
|
|
60
62
|
options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
|
61
63
|
|
62
64
|
default_connectors = {
|
63
|
-
:
|
64
|
-
:
|
65
|
-
:
|
65
|
+
words_connector: ", ",
|
66
|
+
two_words_connector: " and ",
|
67
|
+
last_word_connector: ", and "
|
66
68
|
}
|
67
69
|
if defined?(I18n)
|
68
70
|
i18n_connectors = I18n.translate(:'support.array', locale: options[:locale], default: {})
|
@@ -72,27 +74,29 @@ class Array
|
|
72
74
|
|
73
75
|
case length
|
74
76
|
when 0
|
75
|
-
|
77
|
+
+""
|
76
78
|
when 1
|
77
|
-
self[0]
|
79
|
+
+"#{self[0]}"
|
78
80
|
when 2
|
79
|
-
"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
|
81
|
+
+"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
|
80
82
|
else
|
81
|
-
"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
|
83
|
+
+"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
|
82
84
|
end
|
83
85
|
end
|
84
86
|
|
85
87
|
# Extends <tt>Array#to_s</tt> to convert a collection of elements into a
|
86
88
|
# comma separated id list if <tt>:db</tt> argument is given as the format.
|
87
89
|
#
|
88
|
-
# Blog.all.to_formatted_s(:db)
|
90
|
+
# Blog.all.to_formatted_s(:db) # => "1,2,3"
|
91
|
+
# Blog.none.to_formatted_s(:db) # => "null"
|
92
|
+
# [1,2].to_formatted_s # => "[1, 2]"
|
89
93
|
def to_formatted_s(format = :default)
|
90
94
|
case format
|
91
95
|
when :db
|
92
96
|
if empty?
|
93
|
-
|
97
|
+
"null"
|
94
98
|
else
|
95
|
-
collect
|
99
|
+
collect(&:id).join(",")
|
96
100
|
end
|
97
101
|
else
|
98
102
|
to_default_s
|
@@ -177,7 +181,7 @@ class Array
|
|
177
181
|
# </messages>
|
178
182
|
#
|
179
183
|
def to_xml(options = {})
|
180
|
-
require
|
184
|
+
require "active_support/builder" unless defined?(Builder::XmlMarkup)
|
181
185
|
|
182
186
|
options = options.dup
|
183
187
|
options[:indent] ||= 2
|
@@ -185,9 +189,9 @@ class Array
|
|
185
189
|
options[:root] ||= \
|
186
190
|
if first.class != Hash && all? { |e| e.is_a?(first.class) }
|
187
191
|
underscored = ActiveSupport::Inflector.underscore(first.class.name)
|
188
|
-
ActiveSupport::Inflector.pluralize(underscored).tr(
|
192
|
+
ActiveSupport::Inflector.pluralize(underscored).tr("/", "_")
|
189
193
|
else
|
190
|
-
|
194
|
+
"objects"
|
191
195
|
end
|
192
196
|
|
193
197
|
builder = options[:builder]
|
@@ -195,7 +199,7 @@ class Array
|
|
195
199
|
|
196
200
|
root = ActiveSupport::XmlMini.rename_key(options[:root].to_s, options)
|
197
201
|
children = options.delete(:children) || root.singularize
|
198
|
-
attributes = options[:skip_types] ? {} : { type:
|
202
|
+
attributes = options[:skip_types] ? {} : { type: "array" }
|
199
203
|
|
200
204
|
if empty?
|
201
205
|
builder.tag!(root, attributes)
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Array
|
4
|
+
# Removes and returns the elements for which the block returns a true value.
|
5
|
+
# If no block is given, an Enumerator is returned instead.
|
6
|
+
#
|
7
|
+
# numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
8
|
+
# odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9]
|
9
|
+
# numbers # => [0, 2, 4, 6, 8]
|
10
|
+
def extract!
|
11
|
+
return to_enum(:extract!) { size } unless block_given?
|
12
|
+
|
13
|
+
extracted_elements = []
|
14
|
+
|
15
|
+
reject! do |element|
|
16
|
+
extracted_elements << element if yield(element)
|
17
|
+
end
|
18
|
+
|
19
|
+
extracted_elements
|
20
|
+
end
|
21
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Array
|
2
4
|
# Splits or iterates over the array in groups of size +number+,
|
3
5
|
# padding any remaining slots with +fill_with+ unless it is +false+.
|
@@ -89,28 +91,19 @@ class Array
|
|
89
91
|
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
|
90
92
|
# (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
|
91
93
|
def split(value = nil)
|
94
|
+
arr = dup
|
95
|
+
result = []
|
92
96
|
if block_given?
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
else
|
97
|
-
results.last << element
|
98
|
-
end
|
99
|
-
|
100
|
-
results
|
97
|
+
while (idx = arr.index { |i| yield i })
|
98
|
+
result << arr.shift(idx)
|
99
|
+
arr.shift
|
101
100
|
end
|
102
101
|
else
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
results.last.concat(arr.shift(idx))
|
107
|
-
arr.shift
|
108
|
-
results << []
|
109
|
-
else
|
110
|
-
results.last.concat(arr.shift(arr.size))
|
111
|
-
end
|
102
|
+
while (idx = arr.index(value))
|
103
|
+
result << arr.shift(idx)
|
104
|
+
arr.shift
|
112
105
|
end
|
113
|
-
results
|
114
106
|
end
|
107
|
+
result << arr
|
115
108
|
end
|
116
109
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/array_inquirer"
|
4
|
+
|
5
|
+
class Array
|
6
|
+
# Wraps the array in an +ArrayInquirer+ object, which gives a friendlier way
|
7
|
+
# to check its string-like contents.
|
8
|
+
#
|
9
|
+
# pets = [:cat, :dog].inquiry
|
10
|
+
#
|
11
|
+
# pets.cat? # => true
|
12
|
+
# pets.ferret? # => false
|
13
|
+
#
|
14
|
+
# pets.any?(:cat, :ferret) # => true
|
15
|
+
# pets.any?(:ferret, :alligator) # => false
|
16
|
+
def inquiry
|
17
|
+
ActiveSupport::ArrayInquirer.new(self)
|
18
|
+
end
|
19
|
+
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Array
|
2
4
|
# Wraps its argument in an array unless it is already an array (or array-like).
|
3
5
|
#
|
4
6
|
# Specifically:
|
5
7
|
#
|
6
|
-
# * If the argument is +nil+ an empty
|
8
|
+
# * If the argument is +nil+ an empty array is returned.
|
7
9
|
# * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
|
8
10
|
# * Otherwise, returns an array with the argument as its single element.
|
9
11
|
#
|
@@ -15,12 +17,13 @@ class Array
|
|
15
17
|
#
|
16
18
|
# * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
|
17
19
|
# moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
|
18
|
-
#
|
20
|
+
# an array with the argument as its single element right away.
|
19
21
|
# * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
|
20
22
|
# raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
|
21
|
-
# * It does not call +to_a+ on the argument,
|
23
|
+
# * It does not call +to_a+ on the argument, if the argument does not respond to +to_ary+
|
24
|
+
# it returns an array with the argument as its single element.
|
22
25
|
#
|
23
|
-
# The
|
26
|
+
# The last point is easily explained with some enumerables:
|
24
27
|
#
|
25
28
|
# Array(foo: :bar) # => [[:foo, :bar]]
|
26
29
|
# Array.wrap(foo: :bar) # => [{:foo=>:bar}]
|
@@ -1,6 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/array/wrap"
|
4
|
+
require "active_support/core_ext/array/access"
|
5
|
+
require "active_support/core_ext/array/conversions"
|
6
|
+
require "active_support/core_ext/array/extract"
|
7
|
+
require "active_support/core_ext/array/extract_options"
|
8
|
+
require "active_support/core_ext/array/grouping"
|
9
|
+
require "active_support/core_ext/array/inquiry"
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "benchmark"
|
2
4
|
|
3
5
|
class << Benchmark
|
4
6
|
# Benchmark realtime in milliseconds.
|
@@ -8,7 +10,7 @@ class << Benchmark
|
|
8
10
|
#
|
9
11
|
# Benchmark.ms { User.all }
|
10
12
|
# # => 0.074
|
11
|
-
def ms
|
12
|
-
1000 * realtime
|
13
|
+
def ms(&block)
|
14
|
+
1000 * realtime(&block)
|
13
15
|
end
|
14
16
|
end
|
@@ -1,16 +1,14 @@
|
|
1
|
-
|
2
|
-
require 'bigdecimal/util'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
format
|
11
|
-
_original_to_s(format)
|
3
|
+
require "bigdecimal"
|
4
|
+
require "bigdecimal/util"
|
5
|
+
|
6
|
+
module ActiveSupport
|
7
|
+
module BigDecimalWithDefaultFormat #:nodoc:
|
8
|
+
def to_s(format = "F")
|
9
|
+
super(format)
|
12
10
|
end
|
13
11
|
end
|
14
|
-
alias_method :_original_to_s, :to_s
|
15
|
-
alias_method :to_s, :to_formatted_s
|
16
12
|
end
|
13
|
+
|
14
|
+
BigDecimal.prepend(ActiveSupport::BigDecimalWithDefaultFormat)
|
@@ -1,11 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/redefine_method"
|
4
4
|
|
5
5
|
class Class
|
6
6
|
# Declare a class-level attribute whose value is inheritable by subclasses.
|
7
7
|
# Subclasses can change their own value and it will not impact parent class.
|
8
8
|
#
|
9
|
+
# ==== Options
|
10
|
+
#
|
11
|
+
# * <tt>:instance_reader</tt> - Sets the instance reader method (defaults to true).
|
12
|
+
# * <tt>:instance_writer</tt> - Sets the instance writer method (defaults to true).
|
13
|
+
# * <tt>:instance_accessor</tt> - Sets both instance methods (defaults to true).
|
14
|
+
# * <tt>:instance_predicate</tt> - Sets a predicate method (defaults to true).
|
15
|
+
# * <tt>:default</tt> - Sets a default value for the attribute (defaults to nil).
|
16
|
+
#
|
17
|
+
# ==== Examples
|
18
|
+
#
|
9
19
|
# class Base
|
10
20
|
# class_attribute :setting
|
11
21
|
# end
|
@@ -20,14 +30,14 @@ class Class
|
|
20
30
|
# Base.setting # => true
|
21
31
|
#
|
22
32
|
# In the above case as long as Subclass does not assign a value to setting
|
23
|
-
# by performing <tt>Subclass.setting = _something_
|
33
|
+
# by performing <tt>Subclass.setting = _something_</tt>, <tt>Subclass.setting</tt>
|
24
34
|
# would read value assigned to parent class. Once Subclass assigns a value then
|
25
35
|
# the value assigned by Subclass would be returned.
|
26
36
|
#
|
27
37
|
# This matches normal Ruby method inheritance: think of writing an attribute
|
28
38
|
# on a subclass as overriding the reader method. However, you need to be aware
|
29
39
|
# when using +class_attribute+ with mutable structures as +Array+ or +Hash+.
|
30
|
-
# In such cases, you don't want to do changes in
|
40
|
+
# In such cases, you don't want to do changes in place. Instead use setters:
|
31
41
|
#
|
32
42
|
# Base.setting = []
|
33
43
|
# Base.setting # => []
|
@@ -68,60 +78,54 @@ class Class
|
|
68
78
|
# object.setting = false # => NoMethodError
|
69
79
|
#
|
70
80
|
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
81
|
+
#
|
82
|
+
# To set a default value for the attribute, pass <tt>default:</tt>, like so:
|
83
|
+
#
|
84
|
+
# class_attribute :settings, default: {}
|
85
|
+
def class_attribute(*attrs, instance_accessor: true,
|
86
|
+
instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil)
|
76
87
|
|
88
|
+
class_methods, methods = [], []
|
77
89
|
attrs.each do |name|
|
78
|
-
|
79
|
-
|
90
|
+
unless name.is_a?(Symbol) || name.is_a?(String)
|
91
|
+
raise TypeError, "#{name.inspect} is not a symbol nor a string"
|
92
|
+
end
|
80
93
|
|
81
|
-
|
94
|
+
class_methods << <<~RUBY # In case the method exists and is not public
|
95
|
+
silence_redefinition_of_method def #{name}
|
96
|
+
end
|
97
|
+
RUBY
|
82
98
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
define_method(name) { val }
|
99
|
+
methods << <<~RUBY if instance_reader
|
100
|
+
silence_redefinition_of_method def #{name}
|
101
|
+
defined?(@#{name}) ? @#{name} : self.class.#{name}
|
87
102
|
end
|
103
|
+
RUBY
|
88
104
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
instance_variable_get ivar
|
95
|
-
else
|
96
|
-
singleton_class.send name
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
105
|
+
class_methods << <<~RUBY
|
106
|
+
silence_redefinition_of_method def #{name}=(value)
|
107
|
+
redefine_method(:#{name}) { value } if singleton_class?
|
108
|
+
redefine_singleton_method(:#{name}) { value }
|
109
|
+
value
|
100
110
|
end
|
101
|
-
|
102
|
-
|
111
|
+
RUBY
|
112
|
+
|
113
|
+
methods << <<~RUBY if instance_writer
|
114
|
+
silence_redefinition_of_method(:#{name}=)
|
115
|
+
attr_writer :#{name}
|
116
|
+
RUBY
|
103
117
|
|
104
|
-
if
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
instance_variable_get ivar
|
109
|
-
else
|
110
|
-
self.class.public_send name
|
111
|
-
end
|
118
|
+
if instance_predicate
|
119
|
+
class_methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
120
|
+
if instance_reader
|
121
|
+
methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
112
122
|
end
|
113
|
-
define_method("#{name}?") { !!public_send(name) } if instance_predicate
|
114
123
|
end
|
115
|
-
|
116
|
-
attr_writer name if instance_writer
|
117
124
|
end
|
118
|
-
end
|
119
125
|
|
120
|
-
|
126
|
+
location = caller_locations(1, 1).first
|
127
|
+
class_eval(["class << self", *class_methods, "end", *methods].join(";").tr("\n", ";"), location.path, location.lineno)
|
121
128
|
|
122
|
-
|
123
|
-
|
124
|
-
ancestors.first != self
|
125
|
-
end
|
126
|
-
end
|
129
|
+
attrs.each { |name| public_send("#{name}=", default) }
|
130
|
+
end
|
127
131
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# cattr_* became mattr_* aliases in 7dfbd91b0780fbd6a1dd9bfbc176e10894871d2d,
|
2
4
|
# but we keep this around for libraries that directly require it knowing they
|
3
5
|
# want cattr_*. No need to deprecate.
|
4
|
-
require
|
6
|
+
require "active_support/core_ext/module/attribute_accessors"
|
@@ -1,25 +1,22 @@
|
|
1
|
-
|
2
|
-
require 'active_support/core_ext/module/reachable'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
class Class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
descendants.uniq!
|
22
|
-
descendants
|
4
|
+
# Returns an array with all classes that are < than its receiver.
|
5
|
+
#
|
6
|
+
# class C; end
|
7
|
+
# C.descendants # => []
|
8
|
+
#
|
9
|
+
# class B < C; end
|
10
|
+
# C.descendants # => [B]
|
11
|
+
#
|
12
|
+
# class A < B; end
|
13
|
+
# C.descendants # => [B, A]
|
14
|
+
#
|
15
|
+
# class D < C; end
|
16
|
+
# C.descendants # => [B, A, D]
|
17
|
+
def descendants
|
18
|
+
ObjectSpace.each_object(singleton_class).reject do |k|
|
19
|
+
k.singleton_class? || k == self
|
23
20
|
end
|
24
21
|
end
|
25
22
|
|
@@ -31,10 +28,6 @@ class Class
|
|
31
28
|
#
|
32
29
|
# Foo.subclasses # => [Bar]
|
33
30
|
def subclasses
|
34
|
-
|
35
|
-
chain.each do |k|
|
36
|
-
subclasses << k unless chain.any? { |c| c > k }
|
37
|
-
end
|
38
|
-
subclasses
|
31
|
+
descendants.select { |descendant| descendant.superclass == self }
|
39
32
|
end
|
40
33
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/class/attribute"
|
4
|
+
require "active_support/core_ext/class/subclasses"
|
@@ -1,9 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "date"
|
4
|
+
require "active_support/duration"
|
5
|
+
require "active_support/core_ext/object/acts_like"
|
6
|
+
require "active_support/core_ext/date/zones"
|
7
|
+
require "active_support/core_ext/time/zones"
|
8
|
+
require "active_support/core_ext/date_and_time/calculations"
|
7
9
|
|
8
10
|
class Date
|
9
11
|
include DateAndTime::Calculations
|
@@ -26,7 +28,7 @@ class Date
|
|
26
28
|
Thread.current[:beginning_of_week] = find_beginning_of_week!(week_start)
|
27
29
|
end
|
28
30
|
|
29
|
-
# Returns week start day symbol (e.g. :monday), or raises an ArgumentError for invalid day symbol.
|
31
|
+
# Returns week start day symbol (e.g. :monday), or raises an +ArgumentError+ for invalid day symbol.
|
30
32
|
def find_beginning_of_week!(week_start)
|
31
33
|
raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
|
32
34
|
week_start
|
@@ -108,12 +110,13 @@ class Date
|
|
108
110
|
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
109
111
|
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
|
110
112
|
def advance(options)
|
111
|
-
options = options.dup
|
112
113
|
d = self
|
113
|
-
|
114
|
-
d = d >> options
|
115
|
-
d = d
|
116
|
-
d = d +
|
114
|
+
|
115
|
+
d = d >> options[:years] * 12 if options[:years]
|
116
|
+
d = d >> options[:months] if options[:months]
|
117
|
+
d = d + options[:weeks] * 7 if options[:weeks]
|
118
|
+
d = d + options[:days] if options[:days]
|
119
|
+
|
117
120
|
d
|
118
121
|
end
|
119
122
|
|
@@ -129,11 +132,11 @@ class Date
|
|
129
132
|
options.fetch(:day, day)
|
130
133
|
)
|
131
134
|
end
|
132
|
-
|
135
|
+
|
133
136
|
# Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there.
|
134
137
|
def compare_with_coercion(other)
|
135
138
|
if other.is_a?(Time)
|
136
|
-
|
139
|
+
to_datetime <=> other
|
137
140
|
else
|
138
141
|
compare_without_coercion(other)
|
139
142
|
end
|