activesupport 5.0.7.1
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 +7 -0
- data/CHANGELOG.md +1013 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +39 -0
- data/lib/active_support.rb +99 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +103 -0
- data/lib/active_support/benchmarkable.rb +49 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +701 -0
- data/lib/active_support/cache/file_store.rb +204 -0
- data/lib/active_support/cache/mem_cache_store.rb +207 -0
- data/lib/active_support/cache/memory_store.rb +167 -0
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +172 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/callbacks.rb +791 -0
- data/lib/active_support/concern.rb +142 -0
- data/lib/active_support/concurrency/latch.rb +26 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +148 -0
- data/lib/active_support/core_ext.rb +4 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +90 -0
- data/lib/active_support/core_ext/array/conversions.rb +211 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +107 -0
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +46 -0
- data/lib/active_support/core_ext/benchmark.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +128 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/active_support/core_ext/class/subclasses.rb +41 -0
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +143 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -0
- data/lib/active_support/core_ext/date/zones.rb +6 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +146 -0
- data/lib/active_support/core_ext/file.rb +1 -0
- data/lib/active_support/core_ext/file/atomic.rb +68 -0
- data/lib/active_support/core_ext/hash.rb +9 -0
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
- data/lib/active_support/core_ext/hash/except.rb +22 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/active_support/core_ext/hash/keys.rb +170 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/active_support/core_ext/hash/slice.rb +48 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +29 -0
- data/lib/active_support/core_ext/integer/multiple.rb +10 -0
- data/lib/active_support/core_ext/integer/time.rb +29 -0
- data/lib/active_support/core_ext/kernel.rb +4 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/active_support/core_ext/load_error.rb +31 -0
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +74 -0
- data/lib/active_support/core_ext/module/anonymous.rb +28 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +216 -0
- data/lib/active_support/core_ext/module/deprecation.rb +23 -0
- data/lib/active_support/core_ext/module/introspection.rb +68 -0
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +8 -0
- data/lib/active_support/core_ext/module/remove_method.rb +35 -0
- data/lib/active_support/core_ext/name_error.rb +31 -0
- data/lib/active_support/core_ext/numeric.rb +4 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +74 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +143 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +124 -0
- data/lib/active_support/core_ext/object/inclusion.rb +27 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -0
- data/lib/active_support/core_ext/object/to_query.rb +84 -0
- data/lib/active_support/core_ext/object/try.rb +146 -0
- data/lib/active_support/core_ext/object/with_options.rb +69 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/conversions.rb +31 -0
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string.rb +13 -0
- data/lib/active_support/core_ext/string/access.rb +104 -0
- data/lib/active_support/core_ext/string/behavior.rb +6 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -0
- data/lib/active_support/core_ext/string/exclude.rb +11 -0
- data/lib/active_support/core_ext/string/filters.rb +102 -0
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +244 -0
- data/lib/active_support/core_ext/string/inquiry.rb +13 -0
- data/lib/active_support/core_ext/string/multibyte.rb +53 -0
- data/lib/active_support/core_ext/string/output_safety.rb +260 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/strip.rb +23 -0
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +290 -0
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +67 -0
- data/lib/active_support/core_ext/time/marshal.rb +3 -0
- data/lib/active_support/core_ext/time/zones.rb +111 -0
- data/lib/active_support/core_ext/uri.rb +24 -0
- data/lib/active_support/dependencies.rb +755 -0
- data/lib/active_support/dependencies/autoload.rb +77 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/deprecation.rb +43 -0
- data/lib/active_support/deprecation/behaviors.rb +90 -0
- data/lib/active_support/deprecation/instance_delegator.rb +37 -0
- data/lib/active_support/deprecation/method_wrappers.rb +70 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
- data/lib/active_support/deprecation/reporting.rb +112 -0
- data/lib/active_support/descendants_tracker.rb +60 -0
- data/lib/active_support/duration.rb +235 -0
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- 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 +157 -0
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +36 -0
- data/lib/active_support/hash_with_indifferent_access.rb +329 -0
- data/lib/active_support/i18n.rb +13 -0
- data/lib/active_support/i18n_railtie.rb +115 -0
- data/lib/active_support/inflections.rb +70 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +242 -0
- data/lib/active_support/inflector/methods.rb +390 -0
- data/lib/active_support/inflector/transliterate.rb +112 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/decoding.rb +74 -0
- data/lib/active_support/json/encoding.rb +127 -0
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +76 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +109 -0
- data/lib/active_support/log_subscriber/test_helper.rb +104 -0
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +114 -0
- data/lib/active_support/message_verifier.rb +134 -0
- data/lib/active_support/multibyte.rb +21 -0
- data/lib/active_support/multibyte/chars.rb +231 -0
- data/lib/active_support/multibyte/unicode.rb +413 -0
- data/lib/active_support/notifications.rb +212 -0
- data/lib/active_support/notifications/fanout.rb +157 -0
- data/lib/active_support/notifications/instrumenter.rb +91 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +48 -0
- data/lib/active_support/ordered_options.rb +81 -0
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +51 -0
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +173 -0
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +26 -0
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +77 -0
- data/lib/active_support/test_case.rb +88 -0
- data/lib/active_support/testing/assertions.rb +99 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +26 -0
- data/lib/active_support/testing/deprecation.rb +36 -0
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +115 -0
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +50 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +136 -0
- data/lib/active_support/time.rb +18 -0
- data/lib/active_support/time_with_zone.rb +511 -0
- data/lib/active_support/values/time_zone.rb +484 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +8 -0
- data/lib/active_support/xml_mini.rb +209 -0
- data/lib/active_support/xml_mini/jdom.rb +181 -0
- data/lib/active_support/xml_mini/libxml.rb +77 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
- data/lib/active_support/xml_mini/nokogiri.rb +81 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
- data/lib/active_support/xml_mini/rexml.rb +128 -0
- metadata +350 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
require "active_support/inflector/methods"
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# Autoload and eager load conveniences for your library.
|
5
|
+
#
|
6
|
+
# This module allows you to define autoloads based on
|
7
|
+
# Rails conventions (i.e. no need to define the path
|
8
|
+
# it is automatically guessed based on the filename)
|
9
|
+
# and also define a set of constants that needs to be
|
10
|
+
# eager loaded:
|
11
|
+
#
|
12
|
+
# module MyLib
|
13
|
+
# extend ActiveSupport::Autoload
|
14
|
+
#
|
15
|
+
# autoload :Model
|
16
|
+
#
|
17
|
+
# eager_autoload do
|
18
|
+
# autoload :Cache
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# Then your library can be eager loaded by simply calling:
|
23
|
+
#
|
24
|
+
# MyLib.eager_load!
|
25
|
+
module Autoload
|
26
|
+
def self.extended(base) # :nodoc:
|
27
|
+
base.class_eval do
|
28
|
+
@_autoloads = {}
|
29
|
+
@_under_path = nil
|
30
|
+
@_at_path = nil
|
31
|
+
@_eager_autoload = false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def autoload(const_name, path = @_at_path)
|
36
|
+
unless path
|
37
|
+
full = [name, @_under_path, const_name.to_s].compact.join("::")
|
38
|
+
path = Inflector.underscore(full)
|
39
|
+
end
|
40
|
+
|
41
|
+
if @_eager_autoload
|
42
|
+
@_autoloads[const_name] = path
|
43
|
+
end
|
44
|
+
|
45
|
+
super const_name, path
|
46
|
+
end
|
47
|
+
|
48
|
+
def autoload_under(path)
|
49
|
+
@_under_path, old_path = path, @_under_path
|
50
|
+
yield
|
51
|
+
ensure
|
52
|
+
@_under_path = old_path
|
53
|
+
end
|
54
|
+
|
55
|
+
def autoload_at(path)
|
56
|
+
@_at_path, old_path = path, @_at_path
|
57
|
+
yield
|
58
|
+
ensure
|
59
|
+
@_at_path = old_path
|
60
|
+
end
|
61
|
+
|
62
|
+
def eager_autoload
|
63
|
+
old_eager, @_eager_autoload = @_eager_autoload, true
|
64
|
+
yield
|
65
|
+
ensure
|
66
|
+
@_eager_autoload = old_eager
|
67
|
+
end
|
68
|
+
|
69
|
+
def eager_load!
|
70
|
+
@_autoloads.each_value { |file| require file }
|
71
|
+
end
|
72
|
+
|
73
|
+
def autoloads
|
74
|
+
@_autoloads
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'active_support/concurrency/share_lock'
|
2
|
+
|
3
|
+
module ActiveSupport #:nodoc:
|
4
|
+
module Dependencies #:nodoc:
|
5
|
+
class Interlock
|
6
|
+
def initialize # :nodoc:
|
7
|
+
@lock = ActiveSupport::Concurrency::ShareLock.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def loading
|
11
|
+
@lock.exclusive(purpose: :load, compatible: [:load], after_compatible: [:load]) do
|
12
|
+
yield
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def unloading
|
17
|
+
@lock.exclusive(purpose: :unload, compatible: [:load, :unload], after_compatible: [:load, :unload]) do
|
18
|
+
yield
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def start_unloading
|
23
|
+
@lock.start_exclusive(purpose: :unload, compatible: [:load, :unload])
|
24
|
+
end
|
25
|
+
|
26
|
+
def done_unloading
|
27
|
+
@lock.stop_exclusive(compatible: [:load, :unload])
|
28
|
+
end
|
29
|
+
|
30
|
+
def start_running
|
31
|
+
@lock.start_sharing
|
32
|
+
end
|
33
|
+
|
34
|
+
def done_running
|
35
|
+
@lock.stop_sharing
|
36
|
+
end
|
37
|
+
|
38
|
+
def running
|
39
|
+
@lock.sharing do
|
40
|
+
yield
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def permit_concurrent_loads
|
45
|
+
@lock.yield_shares(compatible: [:load]) do
|
46
|
+
yield
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def raw_state(&block) # :nodoc:
|
51
|
+
@lock.raw_state(&block)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# \Deprecation specifies the API used by Rails to deprecate methods, instance
|
5
|
+
# variables, objects and constants.
|
6
|
+
class Deprecation
|
7
|
+
# active_support.rb sets an autoload for ActiveSupport::Deprecation.
|
8
|
+
#
|
9
|
+
# If these requires were at the top of the file the constant would not be
|
10
|
+
# defined by the time their files were loaded. Since some of them reopen
|
11
|
+
# ActiveSupport::Deprecation its autoload would be triggered, resulting in
|
12
|
+
# a circular require warning for active_support/deprecation.rb.
|
13
|
+
#
|
14
|
+
# So, we define the constant first, and load dependencies later.
|
15
|
+
require 'active_support/deprecation/instance_delegator'
|
16
|
+
require 'active_support/deprecation/behaviors'
|
17
|
+
require 'active_support/deprecation/reporting'
|
18
|
+
require 'active_support/deprecation/method_wrappers'
|
19
|
+
require 'active_support/deprecation/proxy_wrappers'
|
20
|
+
require 'active_support/core_ext/module/deprecation'
|
21
|
+
|
22
|
+
include Singleton
|
23
|
+
include InstanceDelegator
|
24
|
+
include Behavior
|
25
|
+
include Reporting
|
26
|
+
include MethodWrapper
|
27
|
+
|
28
|
+
# The version number in which the deprecated behavior will be removed, by default.
|
29
|
+
attr_accessor :deprecation_horizon
|
30
|
+
|
31
|
+
# It accepts two parameters on initialization. The first is a version of library
|
32
|
+
# and the second is a library name
|
33
|
+
#
|
34
|
+
# ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
|
35
|
+
def initialize(deprecation_horizon = '5.1', gem_name = 'Rails')
|
36
|
+
self.gem_name = gem_name
|
37
|
+
self.deprecation_horizon = deprecation_horizon
|
38
|
+
# By default, warnings are not silenced and debugging is off.
|
39
|
+
self.silenced = false
|
40
|
+
self.debug = false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require "active_support/notifications"
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# Raised when <tt>ActiveSupport::Deprecation::Behavior#behavior</tt> is set with <tt>:raise</tt>.
|
5
|
+
# You would set <tt>:raise</tt>, as a behaviour to raise errors and proactively report exceptions from deprecations.
|
6
|
+
class DeprecationException < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
class Deprecation
|
10
|
+
# Default warning behaviors per Rails.env.
|
11
|
+
DEFAULT_BEHAVIORS = {
|
12
|
+
raise: ->(message, callstack) {
|
13
|
+
e = DeprecationException.new(message)
|
14
|
+
e.set_backtrace(callstack.map(&:to_s))
|
15
|
+
raise e
|
16
|
+
},
|
17
|
+
|
18
|
+
stderr: ->(message, callstack) {
|
19
|
+
$stderr.puts(message)
|
20
|
+
$stderr.puts callstack.join("\n ") if debug
|
21
|
+
},
|
22
|
+
|
23
|
+
log: ->(message, callstack) {
|
24
|
+
logger =
|
25
|
+
if defined?(Rails.logger) && Rails.logger
|
26
|
+
Rails.logger
|
27
|
+
else
|
28
|
+
require 'active_support/logger'
|
29
|
+
ActiveSupport::Logger.new($stderr)
|
30
|
+
end
|
31
|
+
logger.warn message
|
32
|
+
logger.debug callstack.join("\n ") if debug
|
33
|
+
},
|
34
|
+
|
35
|
+
notify: ->(message, callstack) {
|
36
|
+
ActiveSupport::Notifications.instrument("deprecation.rails",
|
37
|
+
:message => message, :callstack => callstack)
|
38
|
+
},
|
39
|
+
|
40
|
+
silence: ->(message, callstack) {},
|
41
|
+
}
|
42
|
+
|
43
|
+
# Behavior module allows to determine how to display deprecation messages.
|
44
|
+
# You can create a custom behavior or set any from the +DEFAULT_BEHAVIORS+
|
45
|
+
# constant. Available behaviors are:
|
46
|
+
#
|
47
|
+
# [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
|
48
|
+
# [+stderr+] Log all deprecation warnings to +$stderr+.
|
49
|
+
# [+log+] Log all deprecation warnings to +Rails.logger+.
|
50
|
+
# [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
|
51
|
+
# [+silence+] Do nothing.
|
52
|
+
#
|
53
|
+
# Setting behaviors only affects deprecations that happen after boot time.
|
54
|
+
# For more information you can read the documentation of the +behavior=+ method.
|
55
|
+
module Behavior
|
56
|
+
# Whether to print a backtrace along with the warning.
|
57
|
+
attr_accessor :debug
|
58
|
+
|
59
|
+
# Returns the current behavior or if one isn't set, defaults to +:stderr+.
|
60
|
+
def behavior
|
61
|
+
@behavior ||= [DEFAULT_BEHAVIORS[:stderr]]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Sets the behavior to the specified value. Can be a single value, array,
|
65
|
+
# or an object that responds to +call+.
|
66
|
+
#
|
67
|
+
# Available behaviors:
|
68
|
+
#
|
69
|
+
# [+raise+] Raise <tt>ActiveSupport::DeprecationException</tt>.
|
70
|
+
# [+stderr+] Log all deprecation warnings to +$stderr+.
|
71
|
+
# [+log+] Log all deprecation warnings to +Rails.logger+.
|
72
|
+
# [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
|
73
|
+
# [+silence+] Do nothing.
|
74
|
+
#
|
75
|
+
# Setting behaviors only affects deprecations that happen after boot time.
|
76
|
+
# Deprecation warnings raised by gems are not affected by this setting
|
77
|
+
# because they happen before Rails boots up.
|
78
|
+
#
|
79
|
+
# ActiveSupport::Deprecation.behavior = :stderr
|
80
|
+
# ActiveSupport::Deprecation.behavior = [:stderr, :log]
|
81
|
+
# ActiveSupport::Deprecation.behavior = MyCustomHandler
|
82
|
+
# ActiveSupport::Deprecation.behavior = ->(message, callstack) {
|
83
|
+
# # custom stuff
|
84
|
+
# }
|
85
|
+
def behavior=(behavior)
|
86
|
+
@behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || b }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'active_support/core_ext/kernel/singleton_class'
|
2
|
+
require 'active_support/core_ext/module/delegation'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
class Deprecation
|
6
|
+
module InstanceDelegator # :nodoc:
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
base.singleton_class.prepend(OverrideDelegators)
|
10
|
+
base.public_class_method :new
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods # :nodoc:
|
14
|
+
def include(included_module)
|
15
|
+
included_module.instance_methods.each { |m| method_added(m) }
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_added(method_name)
|
20
|
+
singleton_class.delegate(method_name, to: :instance)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module OverrideDelegators # :nodoc:
|
25
|
+
def warn(message = nil, callstack = nil)
|
26
|
+
callstack ||= caller_locations(2)
|
27
|
+
super
|
28
|
+
end
|
29
|
+
|
30
|
+
def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
|
31
|
+
caller_backtrace ||= caller_locations(2)
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'active_support/core_ext/module/aliasing'
|
2
|
+
require 'active_support/core_ext/array/extract_options'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
class Deprecation
|
6
|
+
module MethodWrapper
|
7
|
+
# Declare that a method has been deprecated.
|
8
|
+
#
|
9
|
+
# module Fred
|
10
|
+
# extend self
|
11
|
+
#
|
12
|
+
# def aaa; end
|
13
|
+
# def bbb; end
|
14
|
+
# def ccc; end
|
15
|
+
# def ddd; end
|
16
|
+
# def eee; end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# Using the default deprecator:
|
20
|
+
# ActiveSupport::Deprecation.deprecate_methods(Fred, :aaa, bbb: :zzz, ccc: 'use Bar#ccc instead')
|
21
|
+
# # => [:aaa, :bbb, :ccc]
|
22
|
+
#
|
23
|
+
# Fred.aaa
|
24
|
+
# # DEPRECATION WARNING: aaa is deprecated and will be removed from Rails 5.1. (called from irb_binding at (irb):10)
|
25
|
+
# # => nil
|
26
|
+
#
|
27
|
+
# Fred.bbb
|
28
|
+
# # DEPRECATION WARNING: bbb is deprecated and will be removed from Rails 5.1 (use zzz instead). (called from irb_binding at (irb):11)
|
29
|
+
# # => nil
|
30
|
+
#
|
31
|
+
# Fred.ccc
|
32
|
+
# # DEPRECATION WARNING: ccc is deprecated and will be removed from Rails 5.1 (use Bar#ccc instead). (called from irb_binding at (irb):12)
|
33
|
+
# # => nil
|
34
|
+
#
|
35
|
+
# Passing in a custom deprecator:
|
36
|
+
# custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem')
|
37
|
+
# ActiveSupport::Deprecation.deprecate_methods(Fred, ddd: :zzz, deprecator: custom_deprecator)
|
38
|
+
# # => [:ddd]
|
39
|
+
#
|
40
|
+
# Fred.ddd
|
41
|
+
# DEPRECATION WARNING: ddd is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):15)
|
42
|
+
# # => nil
|
43
|
+
#
|
44
|
+
# Using a custom deprecator directly:
|
45
|
+
# custom_deprecator = ActiveSupport::Deprecation.new('next-release', 'MyGem')
|
46
|
+
# custom_deprecator.deprecate_methods(Fred, eee: :zzz)
|
47
|
+
# # => [:eee]
|
48
|
+
#
|
49
|
+
# Fred.eee
|
50
|
+
# DEPRECATION WARNING: eee is deprecated and will be removed from MyGem next-release (use zzz instead). (called from irb_binding at (irb):18)
|
51
|
+
# # => nil
|
52
|
+
def deprecate_methods(target_module, *method_names)
|
53
|
+
options = method_names.extract_options!
|
54
|
+
deprecator = options.delete(:deprecator) || self
|
55
|
+
method_names += options.keys
|
56
|
+
|
57
|
+
mod = Module.new do
|
58
|
+
method_names.each do |method_name|
|
59
|
+
define_method(method_name) do |*args, &block|
|
60
|
+
deprecator.deprecation_warning(method_name, options[method_name])
|
61
|
+
super(*args, &block)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
target_module.prepend(mod)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'active_support/inflector/methods'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
class Deprecation
|
5
|
+
class DeprecationProxy #:nodoc:
|
6
|
+
def self.new(*args, &block)
|
7
|
+
object = args.first
|
8
|
+
|
9
|
+
return object unless object
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }
|
14
|
+
|
15
|
+
# Don't give a deprecation warning on inspect since test/unit and error
|
16
|
+
# logs rely on it for diagnostics.
|
17
|
+
def inspect
|
18
|
+
target.inspect
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def method_missing(called, *args, &block)
|
23
|
+
warn caller_locations, called, args
|
24
|
+
target.__send__(called, *args, &block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# DeprecatedObjectProxy transforms an object into a deprecated one. It
|
29
|
+
# takes an object, a deprecation message and optionally a deprecator. The
|
30
|
+
# deprecator defaults to +ActiveSupport::Deprecator+ if none is specified.
|
31
|
+
#
|
32
|
+
# deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, "This object is now deprecated")
|
33
|
+
# # => #<Object:0x007fb9b34c34b0>
|
34
|
+
#
|
35
|
+
# deprecated_object.to_s
|
36
|
+
# DEPRECATION WARNING: This object is now deprecated.
|
37
|
+
# (Backtrace)
|
38
|
+
# # => "#<Object:0x007fb9b34c34b0>"
|
39
|
+
class DeprecatedObjectProxy < DeprecationProxy
|
40
|
+
def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
|
41
|
+
@object = object
|
42
|
+
@message = message
|
43
|
+
@deprecator = deprecator
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def target
|
48
|
+
@object
|
49
|
+
end
|
50
|
+
|
51
|
+
def warn(callstack, called, args)
|
52
|
+
@deprecator.warn(@message, callstack)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# DeprecatedInstanceVariableProxy transforms an instance variable into a
|
57
|
+
# deprecated one. It takes an instance of a class, a method on that class
|
58
|
+
# and an instance variable. It optionally takes a deprecator as the last
|
59
|
+
# argument. The deprecator defaults to +ActiveSupport::Deprecator+ if none
|
60
|
+
# is specified.
|
61
|
+
#
|
62
|
+
# class Example
|
63
|
+
# def initialize
|
64
|
+
# @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request)
|
65
|
+
# @_request = :special_request
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# def request
|
69
|
+
# @_request
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# def old_request
|
73
|
+
# @request
|
74
|
+
# end
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# example = Example.new
|
78
|
+
# # => #<Example:0x007fb9b31090b8 @_request=:special_request, @request=:special_request>
|
79
|
+
#
|
80
|
+
# example.old_request.to_s
|
81
|
+
# # => DEPRECATION WARNING: @request is deprecated! Call request.to_s instead of
|
82
|
+
# @request.to_s
|
83
|
+
# (Backtrace information…)
|
84
|
+
# "special_request"
|
85
|
+
#
|
86
|
+
# example.request.to_s
|
87
|
+
# # => "special_request"
|
88
|
+
class DeprecatedInstanceVariableProxy < DeprecationProxy
|
89
|
+
def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
|
90
|
+
@instance = instance
|
91
|
+
@method = method
|
92
|
+
@var = var
|
93
|
+
@deprecator = deprecator
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
def target
|
98
|
+
@instance.__send__(@method)
|
99
|
+
end
|
100
|
+
|
101
|
+
def warn(callstack, called, args)
|
102
|
+
@deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# DeprecatedConstantProxy transforms a constant into a deprecated one. It
|
107
|
+
# takes the names of an old (deprecated) constant and of a new constant
|
108
|
+
# (both in string form) and optionally a deprecator. The deprecator defaults
|
109
|
+
# to +ActiveSupport::Deprecator+ if none is specified. The deprecated constant
|
110
|
+
# now returns the value of the new one.
|
111
|
+
#
|
112
|
+
# PLANETS = %w(mercury venus earth mars jupiter saturn uranus neptune pluto)
|
113
|
+
#
|
114
|
+
# (In a later update, the original implementation of `PLANETS` has been removed.)
|
115
|
+
#
|
116
|
+
# PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
|
117
|
+
# PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
|
118
|
+
#
|
119
|
+
# PLANETS.map { |planet| planet.capitalize }
|
120
|
+
# # => DEPRECATION WARNING: PLANETS is deprecated! Use PLANETS_POST_2006 instead.
|
121
|
+
# (Backtrace information…)
|
122
|
+
# ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
|
123
|
+
class DeprecatedConstantProxy < DeprecationProxy
|
124
|
+
def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
|
125
|
+
@old_const = old_const
|
126
|
+
@new_const = new_const
|
127
|
+
@deprecator = deprecator
|
128
|
+
end
|
129
|
+
|
130
|
+
# Returns the class of the new constant.
|
131
|
+
#
|
132
|
+
# PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
|
133
|
+
# PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
|
134
|
+
# PLANETS.class # => Array
|
135
|
+
def class
|
136
|
+
target.class
|
137
|
+
end
|
138
|
+
|
139
|
+
private
|
140
|
+
def target
|
141
|
+
ActiveSupport::Inflector.constantize(@new_const.to_s)
|
142
|
+
end
|
143
|
+
|
144
|
+
def warn(callstack, called, args)
|
145
|
+
@deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|