activesupport 5.2.4.3 → 7.0.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 +244 -459
- data/MIT-LICENSE +1 -1
- data/README.rdoc +4 -3
- data/lib/active_support/actionable_error.rb +48 -0
- data/lib/active_support/array_inquirer.rb +2 -2
- data/lib/active_support/backtrace_cleaner.rb +31 -5
- data/lib/active_support/benchmarkable.rb +3 -3
- data/lib/active_support/cache/file_store.rb +47 -41
- data/lib/active_support/cache/mem_cache_store.rb +151 -40
- data/lib/active_support/cache/memory_store.rb +68 -34
- data/lib/active_support/cache/null_store.rb +16 -3
- data/lib/active_support/cache/redis_cache_store.rb +103 -101
- data/lib/active_support/cache/strategy/local_cache.rb +56 -64
- data/lib/active_support/cache.rb +333 -116
- data/lib/active_support/callbacks.rb +244 -128
- data/lib/active_support/code_generator.rb +65 -0
- data/lib/active_support/concern.rb +72 -5
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +16 -0
- data/lib/active_support/concurrency/share_lock.rb +2 -3
- data/lib/active_support/configurable.rb +15 -16
- data/lib/active_support/configuration_file.rb +51 -0
- data/lib/active_support/core_ext/array/access.rb +15 -7
- data/lib/active_support/core_ext/array/conversions.rb +18 -17
- data/lib/active_support/core_ext/array/deprecated_conversions.rb +25 -0
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/grouping.rb +6 -6
- data/lib/active_support/core_ext/array/inquiry.rb +2 -2
- data/lib/active_support/core_ext/array.rb +2 -1
- data/lib/active_support/core_ext/benchmark.rb +2 -2
- data/lib/active_support/core_ext/big_decimal/conversions.rb +1 -1
- data/lib/active_support/core_ext/class/attribute.rb +32 -47
- data/lib/active_support/core_ext/class/subclasses.rb +9 -22
- data/lib/active_support/core_ext/date/blank.rb +1 -1
- data/lib/active_support/core_ext/date/calculations.rb +15 -14
- data/lib/active_support/core_ext/date/conversions.rb +16 -15
- data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
- data/lib/active_support/core_ext/date.rb +1 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +41 -51
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +15 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +0 -1
- data/lib/active_support/core_ext/date_time/blank.rb +1 -1
- data/lib/active_support/core_ext/date_time/calculations.rb +1 -1
- data/lib/active_support/core_ext/date_time/conversions.rb +13 -14
- data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
- data/lib/active_support/core_ext/date_time.rb +1 -0
- data/lib/active_support/core_ext/digest/uuid.rb +39 -13
- data/lib/active_support/core_ext/enumerable.rb +241 -76
- data/lib/active_support/core_ext/file/atomic.rb +3 -1
- data/lib/active_support/core_ext/hash/conversions.rb +3 -4
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +2 -2
- data/lib/active_support/core_ext/hash/indifferent_access.rb +3 -3
- data/lib/active_support/core_ext/hash/keys.rb +2 -31
- data/lib/active_support/core_ext/hash/slice.rb +6 -27
- data/lib/active_support/core_ext/hash.rb +1 -2
- data/lib/active_support/core_ext/integer/multiple.rb +1 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +4 -4
- data/lib/active_support/core_ext/kernel/singleton_class.rb +1 -1
- data/lib/active_support/core_ext/kernel.rb +0 -1
- data/lib/active_support/core_ext/load_error.rb +1 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +2 -2
- data/lib/active_support/core_ext/module/attribute_accessors.rb +32 -39
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +35 -28
- data/lib/active_support/core_ext/module/concerning.rb +8 -2
- data/lib/active_support/core_ext/module/delegation.rb +70 -33
- data/lib/active_support/core_ext/module/introspection.rb +16 -15
- data/lib/active_support/core_ext/module/redefine_method.rb +8 -17
- data/lib/active_support/core_ext/module.rb +0 -1
- data/lib/active_support/core_ext/name_error.rb +23 -2
- data/lib/active_support/core_ext/numeric/conversions.rb +132 -129
- data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
- data/lib/active_support/core_ext/numeric.rb +1 -1
- data/lib/active_support/core_ext/object/acts_like.rb +29 -5
- data/lib/active_support/core_ext/object/blank.rb +3 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +1 -1
- data/lib/active_support/core_ext/object/duplicable.rb +14 -110
- data/lib/active_support/core_ext/object/json.rb +44 -27
- data/lib/active_support/core_ext/object/to_query.rb +2 -2
- data/lib/active_support/core_ext/object/try.rb +24 -14
- data/lib/active_support/core_ext/object/with_options.rb +21 -2
- data/lib/active_support/core_ext/pathname/existence.rb +21 -0
- data/lib/active_support/core_ext/pathname.rb +3 -0
- data/lib/active_support/core_ext/range/compare_range.rb +23 -27
- data/lib/active_support/core_ext/range/conversions.rb +32 -30
- data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
- data/lib/active_support/core_ext/range/each.rb +1 -2
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +4 -20
- data/lib/active_support/core_ext/range/overlaps.rb +1 -1
- data/lib/active_support/core_ext/range.rb +1 -1
- data/lib/active_support/core_ext/regexp.rb +8 -5
- data/lib/active_support/core_ext/securerandom.rb +23 -3
- data/lib/active_support/core_ext/string/access.rb +5 -16
- data/lib/active_support/core_ext/string/conversions.rb +3 -2
- data/lib/active_support/core_ext/string/filters.rb +42 -1
- data/lib/active_support/core_ext/string/inflections.rb +46 -7
- data/lib/active_support/core_ext/string/inquiry.rb +2 -1
- data/lib/active_support/core_ext/string/multibyte.rb +6 -5
- data/lib/active_support/core_ext/string/output_safety.rb +129 -20
- data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -2
- data/lib/active_support/core_ext/string/strip.rb +3 -1
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/time/calculations.rb +59 -10
- data/lib/active_support/core_ext/time/conversions.rb +15 -12
- data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
- data/lib/active_support/core_ext/time/zones.rb +7 -22
- data/lib/active_support/core_ext/time.rb +1 -0
- data/lib/active_support/core_ext/uri.rb +3 -22
- data/lib/active_support/core_ext.rb +2 -1
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +47 -16
- data/lib/active_support/dependencies/interlock.rb +10 -18
- data/lib/active_support/dependencies/require_dependency.rb +28 -0
- data/lib/active_support/dependencies.rb +60 -715
- data/lib/active_support/deprecation/behaviors.rb +21 -5
- data/lib/active_support/deprecation/disallowed.rb +56 -0
- data/lib/active_support/deprecation/instance_delegator.rb +0 -1
- data/lib/active_support/deprecation/method_wrappers.rb +18 -23
- data/lib/active_support/deprecation/proxy_wrappers.rb +31 -8
- data/lib/active_support/deprecation/reporting.rb +50 -7
- data/lib/active_support/deprecation.rb +7 -2
- data/lib/active_support/descendants_tracker.rb +190 -34
- data/lib/active_support/digest.rb +5 -3
- data/lib/active_support/duration/iso8601_parser.rb +5 -7
- data/lib/active_support/duration/iso8601_serializer.rb +27 -15
- data/lib/active_support/duration.rb +149 -67
- data/lib/active_support/encrypted_configuration.rb +12 -5
- data/lib/active_support/encrypted_file.rb +23 -5
- data/lib/active_support/environment_inquirer.rb +20 -0
- data/lib/active_support/error_reporter.rb +117 -0
- data/lib/active_support/evented_file_update_checker.rb +85 -122
- data/lib/active_support/execution_context/test_helper.rb +13 -0
- data/lib/active_support/execution_context.rb +53 -0
- data/lib/active_support/execution_wrapper.rb +44 -21
- data/lib/active_support/executor/test_helper.rb +7 -0
- data/lib/active_support/file_update_checker.rb +0 -1
- data/lib/active_support/fork_tracker.rb +71 -0
- data/lib/active_support/gem_version.rb +5 -5
- data/lib/active_support/hash_with_indifferent_access.rb +73 -43
- data/lib/active_support/html_safe_translation.rb +43 -0
- data/lib/active_support/i18n.rb +2 -0
- data/lib/active_support/i18n_railtie.rb +15 -8
- data/lib/active_support/inflector/inflections.rb +25 -14
- data/lib/active_support/inflector/methods.rb +38 -71
- data/lib/active_support/inflector/transliterate.rb +47 -18
- data/lib/active_support/isolated_execution_state.rb +72 -0
- data/lib/active_support/json/decoding.rb +25 -26
- data/lib/active_support/json/encoding.rb +14 -6
- data/lib/active_support/key_generator.rb +23 -38
- data/lib/active_support/lazy_load_hooks.rb +19 -5
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +8 -4
- data/lib/active_support/log_subscriber/test_helper.rb +2 -2
- data/lib/active_support/log_subscriber.rb +51 -11
- data/lib/active_support/logger.rb +6 -22
- data/lib/active_support/logger_silence.rb +11 -19
- data/lib/active_support/logger_thread_safe_level.rb +45 -10
- data/lib/active_support/message_encryptor.rb +20 -19
- data/lib/active_support/message_verifier.rb +53 -21
- data/lib/active_support/messages/metadata.rb +13 -4
- data/lib/active_support/messages/rotation_configuration.rb +2 -1
- data/lib/active_support/messages/rotator.rb +10 -9
- data/lib/active_support/multibyte/chars.rb +17 -76
- data/lib/active_support/multibyte/unicode.rb +7 -331
- data/lib/active_support/multibyte.rb +1 -1
- data/lib/active_support/notifications/fanout.rb +163 -37
- data/lib/active_support/notifications/instrumenter.rb +90 -11
- data/lib/active_support/notifications.rb +88 -30
- data/lib/active_support/number_helper/number_converter.rb +6 -9
- data/lib/active_support/number_helper/number_to_currency_converter.rb +12 -12
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +4 -3
- data/lib/active_support/number_helper/number_to_human_converter.rb +4 -3
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +5 -4
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +3 -2
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +12 -7
- data/lib/active_support/number_helper/rounding_helper.rb +12 -32
- data/lib/active_support/number_helper.rb +36 -12
- data/lib/active_support/option_merger.rb +15 -4
- data/lib/active_support/ordered_hash.rb +2 -2
- data/lib/active_support/ordered_options.rb +14 -4
- data/lib/active_support/parameter_filter.rb +138 -0
- data/lib/active_support/per_thread_registry.rb +6 -1
- data/lib/active_support/rails.rb +1 -10
- data/lib/active_support/railtie.rb +77 -5
- data/lib/active_support/reloader.rb +5 -6
- data/lib/active_support/rescuable.rb +8 -8
- data/lib/active_support/ruby_features.rb +7 -0
- data/lib/active_support/secure_compare_rotator.rb +51 -0
- data/lib/active_support/security_utils.rb +19 -12
- data/lib/active_support/string_inquirer.rb +2 -3
- data/lib/active_support/subscriber.rb +79 -46
- data/lib/active_support/tagged_logging.rb +58 -9
- data/lib/active_support/test_case.rb +79 -0
- data/lib/active_support/testing/assertions.rb +62 -11
- data/lib/active_support/testing/deprecation.rb +52 -2
- data/lib/active_support/testing/file_fixtures.rb +2 -0
- data/lib/active_support/testing/isolation.rb +4 -4
- data/lib/active_support/testing/method_call_assertions.rb +32 -5
- data/lib/active_support/testing/parallelization/server.rb +82 -0
- data/lib/active_support/testing/parallelization/worker.rb +103 -0
- data/lib/active_support/testing/parallelization.rb +55 -0
- data/lib/active_support/testing/parallelize_executor.rb +76 -0
- data/lib/active_support/testing/stream.rb +4 -7
- data/lib/active_support/testing/tagged_logging.rb +1 -1
- data/lib/active_support/testing/time_helpers.rb +60 -14
- data/lib/active_support/time_with_zone.rb +139 -64
- data/lib/active_support/values/time_zone.rb +66 -30
- data/lib/active_support/version.rb +1 -1
- data/lib/active_support/xml_mini/jdom.rb +3 -4
- data/lib/active_support/xml_mini/libxml.rb +7 -7
- data/lib/active_support/xml_mini/libxmlsax.rb +5 -5
- data/lib/active_support/xml_mini/nokogiri.rb +6 -6
- data/lib/active_support/xml_mini/nokogirisax.rb +4 -4
- data/lib/active_support/xml_mini/rexml.rb +11 -4
- data/lib/active_support/xml_mini.rb +7 -14
- data/lib/active_support.rb +30 -1
- metadata +64 -35
- data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -9
- data/lib/active_support/core_ext/hash/compact.rb +0 -29
- data/lib/active_support/core_ext/hash/transform_values.rb +0 -32
- data/lib/active_support/core_ext/kernel/agnostics.rb +0 -13
- data/lib/active_support/core_ext/marshal.rb +0 -24
- data/lib/active_support/core_ext/module/reachable.rb +0 -11
- data/lib/active_support/core_ext/numeric/inquiry.rb +0 -28
- data/lib/active_support/core_ext/range/include_range.rb +0 -3
- data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -19,7 +19,7 @@ module ActiveSupport
|
|
19
19
|
# By using <tt>ActiveSupport::Concern</tt> the above module could instead be
|
20
20
|
# written as:
|
21
21
|
#
|
22
|
-
# require
|
22
|
+
# require "active_support/concern"
|
23
23
|
#
|
24
24
|
# module M
|
25
25
|
# extend ActiveSupport::Concern
|
@@ -76,7 +76,7 @@ module ActiveSupport
|
|
76
76
|
# is the +Bar+ module, not the +Host+ class. With <tt>ActiveSupport::Concern</tt>,
|
77
77
|
# module dependencies are properly resolved:
|
78
78
|
#
|
79
|
-
# require
|
79
|
+
# require "active_support/concern"
|
80
80
|
#
|
81
81
|
# module Foo
|
82
82
|
# extend ActiveSupport::Concern
|
@@ -99,18 +99,32 @@ module ActiveSupport
|
|
99
99
|
# class Host
|
100
100
|
# include Bar # It works, now Bar takes care of its dependencies
|
101
101
|
# end
|
102
|
+
#
|
103
|
+
# === Prepending concerns
|
104
|
+
#
|
105
|
+
# Just like <tt>include</tt>, concerns also support <tt>prepend</tt> with a corresponding
|
106
|
+
# <tt>prepended do</tt> callback. <tt>module ClassMethods</tt> or <tt>class_methods do</tt> are
|
107
|
+
# prepended as well.
|
108
|
+
#
|
109
|
+
# <tt>prepend</tt> is also used for any dependencies.
|
102
110
|
module Concern
|
103
|
-
class MultipleIncludedBlocks < StandardError
|
111
|
+
class MultipleIncludedBlocks < StandardError # :nodoc:
|
104
112
|
def initialize
|
105
113
|
super "Cannot define multiple 'included' blocks for a Concern"
|
106
114
|
end
|
107
115
|
end
|
108
116
|
|
109
|
-
|
117
|
+
class MultiplePrependBlocks < StandardError # :nodoc:
|
118
|
+
def initialize
|
119
|
+
super "Cannot define multiple 'prepended' blocks for a Concern"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def self.extended(base) # :nodoc:
|
110
124
|
base.instance_variable_set(:@_dependencies, [])
|
111
125
|
end
|
112
126
|
|
113
|
-
def append_features(base)
|
127
|
+
def append_features(base) # :nodoc:
|
114
128
|
if base.instance_variable_defined?(:@_dependencies)
|
115
129
|
base.instance_variable_get(:@_dependencies) << self
|
116
130
|
false
|
@@ -123,6 +137,22 @@ module ActiveSupport
|
|
123
137
|
end
|
124
138
|
end
|
125
139
|
|
140
|
+
def prepend_features(base) # :nodoc:
|
141
|
+
if base.instance_variable_defined?(:@_dependencies)
|
142
|
+
base.instance_variable_get(:@_dependencies).unshift self
|
143
|
+
false
|
144
|
+
else
|
145
|
+
return false if base < self
|
146
|
+
@_dependencies.each { |dep| base.prepend(dep) }
|
147
|
+
super
|
148
|
+
base.singleton_class.prepend const_get(:ClassMethods) if const_defined?(:ClassMethods)
|
149
|
+
base.class_eval(&@_prepended_block) if instance_variable_defined?(:@_prepended_block)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
# Evaluate given block in context of base class,
|
154
|
+
# so that you can write class macros here.
|
155
|
+
# When you define more than one +included+ block, it raises an exception.
|
126
156
|
def included(base = nil, &block)
|
127
157
|
if base.nil?
|
128
158
|
if instance_variable_defined?(:@_included_block)
|
@@ -137,6 +167,43 @@ module ActiveSupport
|
|
137
167
|
end
|
138
168
|
end
|
139
169
|
|
170
|
+
# Evaluate given block in context of base class,
|
171
|
+
# so that you can write class macros here.
|
172
|
+
# When you define more than one +prepended+ block, it raises an exception.
|
173
|
+
def prepended(base = nil, &block)
|
174
|
+
if base.nil?
|
175
|
+
if instance_variable_defined?(:@_prepended_block)
|
176
|
+
if @_prepended_block.source_location != block.source_location
|
177
|
+
raise MultiplePrependBlocks
|
178
|
+
end
|
179
|
+
else
|
180
|
+
@_prepended_block = block
|
181
|
+
end
|
182
|
+
else
|
183
|
+
super
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# Define class methods from given block.
|
188
|
+
# You can define private class methods as well.
|
189
|
+
#
|
190
|
+
# module Example
|
191
|
+
# extend ActiveSupport::Concern
|
192
|
+
#
|
193
|
+
# class_methods do
|
194
|
+
# def foo; puts 'foo'; end
|
195
|
+
#
|
196
|
+
# private
|
197
|
+
# def bar; puts 'bar'; end
|
198
|
+
# end
|
199
|
+
# end
|
200
|
+
#
|
201
|
+
# class Buzz
|
202
|
+
# include Example
|
203
|
+
# end
|
204
|
+
#
|
205
|
+
# Buzz.foo # => "foo"
|
206
|
+
# Buzz.bar # => private method 'bar' called for Buzz:Class(NoMethodError)
|
140
207
|
def class_methods(&class_methods_module_definition)
|
141
208
|
mod = const_defined?(:ClassMethods, false) ?
|
142
209
|
const_get(:ClassMethods) :
|
@@ -7,11 +7,27 @@ module ActiveSupport
|
|
7
7
|
# A monitor that will permit dependency loading while blocked waiting for
|
8
8
|
# the lock.
|
9
9
|
class LoadInterlockAwareMonitor < Monitor
|
10
|
+
EXCEPTION_NEVER = { Exception => :never }.freeze
|
11
|
+
EXCEPTION_IMMEDIATE = { Exception => :immediate }.freeze
|
12
|
+
private_constant :EXCEPTION_NEVER, :EXCEPTION_IMMEDIATE
|
13
|
+
|
10
14
|
# Enters an exclusive section, but allows dependency loading while blocked
|
11
15
|
def mon_enter
|
12
16
|
mon_try_enter ||
|
13
17
|
ActiveSupport::Dependencies.interlock.permit_concurrent_loads { super }
|
14
18
|
end
|
19
|
+
|
20
|
+
def synchronize(&block)
|
21
|
+
Thread.handle_interrupt(EXCEPTION_NEVER) do
|
22
|
+
mon_enter
|
23
|
+
|
24
|
+
begin
|
25
|
+
Thread.handle_interrupt(EXCEPTION_IMMEDIATE, &block)
|
26
|
+
ensure
|
27
|
+
mon_exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
15
31
|
end
|
16
32
|
end
|
17
33
|
end
|
@@ -200,7 +200,6 @@ module ActiveSupport
|
|
200
200
|
end
|
201
201
|
|
202
202
|
private
|
203
|
-
|
204
203
|
# Must be called within synchronize
|
205
204
|
def busy_for_exclusive?(purpose)
|
206
205
|
busy_for_sharing?(purpose) ||
|
@@ -216,9 +215,9 @@ module ActiveSupport
|
|
216
215
|
@waiting.any? { |t, (p, _)| compatible.include?(p) && @waiting.all? { |t2, (_, c2)| t == t2 || c2.include?(p) } }
|
217
216
|
end
|
218
217
|
|
219
|
-
def wait_for(method)
|
218
|
+
def wait_for(method, &block)
|
220
219
|
@sleeping[Thread.current] = method
|
221
|
-
@cv.wait_while
|
220
|
+
@cv.wait_while(&block)
|
222
221
|
ensure
|
223
222
|
@sleeping.delete Thread.current
|
224
223
|
end
|
@@ -2,12 +2,10 @@
|
|
2
2
|
|
3
3
|
require "active_support/concern"
|
4
4
|
require "active_support/ordered_options"
|
5
|
-
require "active_support/core_ext/array/extract_options"
|
6
|
-
require "active_support/core_ext/regexp"
|
7
5
|
|
8
6
|
module ActiveSupport
|
9
7
|
# Configurable provides a <tt>config</tt> method to store and retrieve
|
10
|
-
# configuration options as an
|
8
|
+
# configuration options as an OrderedOptions.
|
11
9
|
module Configurable
|
12
10
|
extend ActiveSupport::Concern
|
13
11
|
|
@@ -69,8 +67,8 @@ module ActiveSupport
|
|
69
67
|
# end
|
70
68
|
# # => NameError: invalid config attribute name
|
71
69
|
#
|
72
|
-
# To
|
73
|
-
# To
|
70
|
+
# To omit the instance writer method, pass <tt>instance_writer: false</tt>.
|
71
|
+
# To omit the instance reader method, pass <tt>instance_reader: false</tt>.
|
74
72
|
#
|
75
73
|
# class User
|
76
74
|
# include ActiveSupport::Configurable
|
@@ -83,7 +81,7 @@ module ActiveSupport
|
|
83
81
|
# User.new.allowed_access = true # => NoMethodError
|
84
82
|
# User.new.allowed_access # => NoMethodError
|
85
83
|
#
|
86
|
-
# Or pass <tt>instance_accessor: false</tt>, to
|
84
|
+
# Or pass <tt>instance_accessor: false</tt>, to omit both instance methods.
|
87
85
|
#
|
88
86
|
# class User
|
89
87
|
# include ActiveSupport::Configurable
|
@@ -96,19 +94,19 @@ module ActiveSupport
|
|
96
94
|
# User.new.allowed_access = true # => NoMethodError
|
97
95
|
# User.new.allowed_access # => NoMethodError
|
98
96
|
#
|
99
|
-
# Also you can pass a block to set up the attribute with a default value.
|
97
|
+
# Also you can pass <tt>default</tt> or a block to set up the attribute with a default value.
|
100
98
|
#
|
101
99
|
# class User
|
102
100
|
# include ActiveSupport::Configurable
|
101
|
+
# config_accessor :allowed_access, default: false
|
103
102
|
# config_accessor :hair_colors do
|
104
103
|
# [:brown, :black, :blonde, :red]
|
105
104
|
# end
|
106
105
|
# end
|
107
106
|
#
|
107
|
+
# User.allowed_access # => false
|
108
108
|
# User.hair_colors # => [:brown, :black, :blonde, :red]
|
109
|
-
def config_accessor(*names)
|
110
|
-
options = names.extract_options!
|
111
|
-
|
109
|
+
def config_accessor(*names, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil) # :doc:
|
112
110
|
names.each do |name|
|
113
111
|
raise NameError.new("invalid config attribute name") unless /\A[_A-Za-z]\w*\z/.match?(name)
|
114
112
|
|
@@ -118,19 +116,20 @@ module ActiveSupport
|
|
118
116
|
singleton_class.class_eval reader, __FILE__, reader_line
|
119
117
|
singleton_class.class_eval writer, __FILE__, writer_line
|
120
118
|
|
121
|
-
|
122
|
-
class_eval reader, __FILE__, reader_line
|
123
|
-
class_eval writer, __FILE__, writer_line
|
119
|
+
if instance_accessor
|
120
|
+
class_eval reader, __FILE__, reader_line if instance_reader
|
121
|
+
class_eval writer, __FILE__, writer_line if instance_writer
|
124
122
|
end
|
125
|
-
|
123
|
+
|
124
|
+
send("#{name}=", block_given? ? yield : default)
|
126
125
|
end
|
127
126
|
end
|
128
127
|
private :config_accessor
|
129
128
|
end
|
130
129
|
|
131
|
-
# Reads and writes attributes from a configuration
|
130
|
+
# Reads and writes attributes from a configuration OrderedOptions.
|
132
131
|
#
|
133
|
-
# require
|
132
|
+
# require "active_support/configurable"
|
134
133
|
#
|
135
134
|
# class User
|
136
135
|
# include ActiveSupport::Configurable
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# Reads a YAML configuration file, evaluating any ERB, then
|
5
|
+
# parsing the resulting YAML.
|
6
|
+
#
|
7
|
+
# Warns in case of YAML confusing characters, like invisible
|
8
|
+
# non-breaking spaces.
|
9
|
+
class ConfigurationFile # :nodoc:
|
10
|
+
class FormatError < StandardError; end
|
11
|
+
|
12
|
+
def initialize(content_path)
|
13
|
+
@content_path = content_path.to_s
|
14
|
+
@content = read content_path
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.parse(content_path, **options)
|
18
|
+
new(content_path).parse(**options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse(context: nil, **options)
|
22
|
+
source = render(context)
|
23
|
+
if YAML.respond_to?(:unsafe_load)
|
24
|
+
YAML.unsafe_load(source, **options) || {}
|
25
|
+
else
|
26
|
+
YAML.load(source, **options) || {}
|
27
|
+
end
|
28
|
+
rescue Psych::SyntaxError => error
|
29
|
+
raise "YAML syntax error occurred while parsing #{@content_path}. " \
|
30
|
+
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
|
31
|
+
"Error: #{error.message}"
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def read(content_path)
|
36
|
+
require "yaml"
|
37
|
+
require "erb"
|
38
|
+
|
39
|
+
File.read(content_path).tap do |content|
|
40
|
+
if content.include?("\u00A0")
|
41
|
+
warn "#{content_path} contains invisible non-breaking spaces, you may want to remove those"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def render(context)
|
47
|
+
erb = ERB.new(@content).tap { |e| e.filename = @content_path }
|
48
|
+
context ? erb.result(context) : erb.result
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -29,17 +29,25 @@ class Array
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
# Returns a
|
32
|
+
# Returns a new array that includes the passed elements.
|
33
33
|
#
|
34
|
-
#
|
35
|
-
#
|
36
|
-
|
34
|
+
# [ 1, 2, 3 ].including(4, 5) # => [ 1, 2, 3, 4, 5 ]
|
35
|
+
# [ [ 0, 1 ] ].including([ [ 1, 0 ] ]) # => [ [ 0, 1 ], [ 1, 0 ] ]
|
36
|
+
def including(*elements)
|
37
|
+
self + elements.flatten(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns a copy of the Array excluding the specified elements.
|
41
|
+
#
|
42
|
+
# ["David", "Rafael", "Aaron", "Todd"].excluding("Aaron", "Todd") # => ["David", "Rafael"]
|
43
|
+
# [ [ 0, 1 ], [ 1, 0 ] ].excluding([ [ 1, 0 ] ]) # => [ [ 0, 1 ] ]
|
37
44
|
#
|
38
|
-
# Note: This is an optimization of <tt>Enumerable#
|
45
|
+
# Note: This is an optimization of <tt>Enumerable#excluding</tt> that uses <tt>Array#-</tt>
|
39
46
|
# instead of <tt>Array#reject</tt> for performance reasons.
|
40
|
-
def
|
41
|
-
self - elements
|
47
|
+
def excluding(*elements)
|
48
|
+
self - elements.flatten(1)
|
42
49
|
end
|
50
|
+
alias :without :excluding
|
43
51
|
|
44
52
|
# Equal to <tt>self[1]</tt>.
|
45
53
|
#
|
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "active_support/xml_mini"
|
4
3
|
require "active_support/core_ext/hash/keys"
|
5
4
|
require "active_support/core_ext/string/inflections"
|
6
5
|
require "active_support/core_ext/object/to_param"
|
@@ -16,12 +15,12 @@ class Array
|
|
16
15
|
#
|
17
16
|
# ==== Options
|
18
17
|
#
|
19
|
-
# * <tt>:words_connector</tt> - The sign or word used to join the
|
20
|
-
# in arrays with
|
21
|
-
# * <tt>:two_words_connector</tt> - The sign or word used to join the elements
|
22
|
-
# in arrays with two elements (default: " and ").
|
18
|
+
# * <tt>:words_connector</tt> - The sign or word used to join all but the last
|
19
|
+
# element in arrays with three or more elements (default: ", ").
|
23
20
|
# * <tt>:last_word_connector</tt> - The sign or word used to join the last element
|
24
21
|
# in arrays with three or more elements (default: ", and ").
|
22
|
+
# * <tt>:two_words_connector</tt> - The sign or word used to join the elements
|
23
|
+
# in arrays with two elements (default: " and ").
|
25
24
|
# * <tt>:locale</tt> - If +i18n+ is available, you can set a locale and use
|
26
25
|
# the connector options defined on the 'support.array' namespace in the
|
27
26
|
# corresponding dictionary file.
|
@@ -66,7 +65,7 @@ class Array
|
|
66
65
|
two_words_connector: " and ",
|
67
66
|
last_word_connector: ", and "
|
68
67
|
}
|
69
|
-
if defined?(I18n)
|
68
|
+
if options[:locale] != false && defined?(I18n)
|
70
69
|
i18n_connectors = I18n.translate(:'support.array', locale: options[:locale], default: {})
|
71
70
|
default_connectors.merge!(i18n_connectors)
|
72
71
|
end
|
@@ -74,23 +73,25 @@ class Array
|
|
74
73
|
|
75
74
|
case length
|
76
75
|
when 0
|
77
|
-
""
|
76
|
+
+""
|
78
77
|
when 1
|
79
|
-
"#{self[0]}"
|
78
|
+
+"#{self[0]}"
|
80
79
|
when 2
|
81
|
-
"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
|
80
|
+
+"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
|
82
81
|
else
|
83
|
-
"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
|
82
|
+
+"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
|
84
83
|
end
|
85
84
|
end
|
86
85
|
|
87
86
|
# Extends <tt>Array#to_s</tt> to convert a collection of elements into a
|
88
87
|
# comma separated id list if <tt>:db</tt> argument is given as the format.
|
89
88
|
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
|
89
|
+
# This method is aliased to <tt>to_formatted_s</tt>.
|
90
|
+
#
|
91
|
+
# Blog.all.to_fs(:db) # => "1,2,3"
|
92
|
+
# Blog.none.to_fs(:db) # => "null"
|
93
|
+
# [1,2].to_fs # => "[1, 2]"
|
94
|
+
def to_fs(format = :default)
|
94
95
|
case format
|
95
96
|
when :db
|
96
97
|
if empty?
|
@@ -102,8 +103,8 @@ class Array
|
|
102
103
|
to_default_s
|
103
104
|
end
|
104
105
|
end
|
106
|
+
alias_method :to_formatted_s, :to_fs
|
105
107
|
alias_method :to_default_s, :to_s
|
106
|
-
alias_method :to_s, :to_formatted_s
|
107
108
|
|
108
109
|
# Returns a string that represents the array in XML by invoking +to_xml+
|
109
110
|
# on each element. Active Record collections delegate their representation
|
@@ -181,13 +182,13 @@ class Array
|
|
181
182
|
# </messages>
|
182
183
|
#
|
183
184
|
def to_xml(options = {})
|
184
|
-
require "active_support/builder" unless defined?(Builder)
|
185
|
+
require "active_support/builder" unless defined?(Builder::XmlMarkup)
|
185
186
|
|
186
187
|
options = options.dup
|
187
188
|
options[:indent] ||= 2
|
188
189
|
options[:builder] ||= Builder::XmlMarkup.new(indent: options[:indent])
|
189
190
|
options[:root] ||= \
|
190
|
-
if first.class != Hash && all?
|
191
|
+
if first.class != Hash && all?(first.class)
|
191
192
|
underscored = ActiveSupport::Inflector.underscore(first.class.name)
|
192
193
|
ActiveSupport::Inflector.pluralize(underscored).tr("/", "_")
|
193
194
|
else
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Array
|
4
|
+
NOT_SET = Object.new # :nodoc:
|
5
|
+
def to_s(format = NOT_SET) # :nodoc:
|
6
|
+
case format
|
7
|
+
when :db
|
8
|
+
ActiveSupport::Deprecation.warn(
|
9
|
+
"Array#to_s(#{format.inspect}) is deprecated. Please use Array#to_fs(#{format.inspect}) instead."
|
10
|
+
)
|
11
|
+
if empty?
|
12
|
+
"null"
|
13
|
+
else
|
14
|
+
collect(&:id).join(",")
|
15
|
+
end
|
16
|
+
when NOT_SET
|
17
|
+
to_default_s
|
18
|
+
else
|
19
|
+
ActiveSupport::Deprecation.warn(
|
20
|
+
"Array#to_s(#{format.inspect}) is deprecated. Please use Array#to_fs(#{format.inspect}) instead."
|
21
|
+
)
|
22
|
+
to_default_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -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
|
@@ -19,7 +19,7 @@ class Array
|
|
19
19
|
# ["1", "2"]
|
20
20
|
# ["3", "4"]
|
21
21
|
# ["5"]
|
22
|
-
def in_groups_of(number, fill_with = nil)
|
22
|
+
def in_groups_of(number, fill_with = nil, &block)
|
23
23
|
if number.to_i <= 0
|
24
24
|
raise ArgumentError,
|
25
25
|
"Group size must be a positive integer, was #{number.inspect}"
|
@@ -36,7 +36,7 @@ class Array
|
|
36
36
|
end
|
37
37
|
|
38
38
|
if block_given?
|
39
|
-
collection.each_slice(number
|
39
|
+
collection.each_slice(number, &block)
|
40
40
|
else
|
41
41
|
collection.each_slice(number).to_a
|
42
42
|
end
|
@@ -59,7 +59,7 @@ class Array
|
|
59
59
|
# ["1", "2", "3"]
|
60
60
|
# ["4", "5"]
|
61
61
|
# ["6", "7"]
|
62
|
-
def in_groups(number, fill_with = nil)
|
62
|
+
def in_groups(number, fill_with = nil, &block)
|
63
63
|
# size.div number gives minor group size;
|
64
64
|
# size % number gives how many objects need extra accommodation;
|
65
65
|
# each group hold either division or division + 1 items.
|
@@ -79,7 +79,7 @@ class Array
|
|
79
79
|
end
|
80
80
|
|
81
81
|
if block_given?
|
82
|
-
groups.each
|
82
|
+
groups.each(&block)
|
83
83
|
else
|
84
84
|
groups
|
85
85
|
end
|
@@ -90,11 +90,11 @@ class Array
|
|
90
90
|
#
|
91
91
|
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
|
92
92
|
# (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
|
93
|
-
def split(value = nil)
|
93
|
+
def split(value = nil, &block)
|
94
94
|
arr = dup
|
95
95
|
result = []
|
96
96
|
if block_given?
|
97
|
-
while (idx = arr.index
|
97
|
+
while (idx = arr.index(&block))
|
98
98
|
result << arr.shift(idx)
|
99
99
|
arr.shift
|
100
100
|
end
|
@@ -3,8 +3,8 @@
|
|
3
3
|
require "active_support/array_inquirer"
|
4
4
|
|
5
5
|
class Array
|
6
|
-
# Wraps the array in an
|
7
|
-
# to check its string-like contents.
|
6
|
+
# Wraps the array in an ActiveSupport::ArrayInquirer object, which gives a
|
7
|
+
# friendlier way to check its string-like contents.
|
8
8
|
#
|
9
9
|
# pets = [:cat, :dog].inquiry
|
10
10
|
#
|
@@ -3,7 +3,8 @@
|
|
3
3
|
require "active_support/core_ext/array/wrap"
|
4
4
|
require "active_support/core_ext/array/access"
|
5
5
|
require "active_support/core_ext/array/conversions"
|
6
|
+
require "active_support/core_ext/array/deprecated_conversions" unless ENV["RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION"]
|
7
|
+
require "active_support/core_ext/array/extract"
|
6
8
|
require "active_support/core_ext/array/extract_options"
|
7
9
|
require "active_support/core_ext/array/grouping"
|
8
|
-
require "active_support/core_ext/array/prepend_and_append"
|
9
10
|
require "active_support/core_ext/array/inquiry"
|