activesupport 4.2.0 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +366 -232
- data/MIT-LICENSE +2 -2
- data/README.rdoc +4 -5
- data/lib/active_support.rb +17 -7
- data/lib/active_support/all.rb +5 -3
- data/lib/active_support/array_inquirer.rb +48 -0
- data/lib/active_support/backtrace_cleaner.rb +7 -5
- data/lib/active_support/benchmarkable.rb +6 -4
- data/lib/active_support/builder.rb +3 -1
- data/lib/active_support/cache.rb +271 -177
- data/lib/active_support/cache/file_store.rb +41 -35
- data/lib/active_support/cache/mem_cache_store.rb +97 -88
- data/lib/active_support/cache/memory_store.rb +27 -30
- data/lib/active_support/cache/null_store.rb +7 -8
- data/lib/active_support/cache/redis_cache_store.rb +454 -0
- data/lib/active_support/cache/strategy/local_cache.rb +67 -34
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
- data/lib/active_support/callbacks.rb +654 -560
- data/lib/active_support/concern.rb +5 -3
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
- data/lib/active_support/concurrency/share_lock.rb +227 -0
- data/lib/active_support/configurable.rb +8 -5
- data/lib/active_support/core_ext.rb +3 -1
- data/lib/active_support/core_ext/array.rb +9 -6
- data/lib/active_support/core_ext/array/access.rb +29 -1
- data/lib/active_support/core_ext/array/conversions.rb +22 -18
- 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/prepend_and_append.rb +5 -3
- data/lib/active_support/core_ext/array/wrap.rb +7 -4
- data/lib/active_support/core_ext/benchmark.rb +3 -1
- data/lib/active_support/core_ext/big_decimal.rb +3 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
- data/lib/active_support/core_ext/class.rb +4 -3
- data/lib/active_support/core_ext/class/attribute.rb +41 -22
- data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
- data/lib/active_support/core_ext/class/subclasses.rb +20 -8
- data/lib/active_support/core_ext/date.rb +6 -4
- 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 +11 -9
- data/lib/active_support/core_ext/date/conversions.rb +31 -23
- data/lib/active_support/core_ext/date/zones.rb +4 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +179 -56
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
- data/lib/active_support/core_ext/date_time.rb +7 -4
- 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 +58 -20
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
- data/lib/active_support/core_ext/digest/uuid.rb +7 -5
- data/lib/active_support/core_ext/enumerable.rb +107 -28
- data/lib/active_support/core_ext/file.rb +3 -1
- data/lib/active_support/core_ext/file/atomic.rb +38 -31
- data/lib/active_support/core_ext/hash.rb +11 -9
- data/lib/active_support/core_ext/hash/compact.rb +24 -15
- data/lib/active_support/core_ext/hash/conversions.rb +63 -43
- data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
- data/lib/active_support/core_ext/hash/except.rb +11 -8
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
- data/lib/active_support/core_ext/hash/keys.rb +33 -27
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +8 -8
- data/lib/active_support/core_ext/hash/transform_values.rb +16 -7
- data/lib/active_support/core_ext/integer.rb +5 -3
- data/lib/active_support/core_ext/integer/inflections.rb +3 -1
- data/lib/active_support/core_ext/integer/multiple.rb +2 -0
- data/lib/active_support/core_ext/integer/time.rb +11 -33
- data/lib/active_support/core_ext/kernel.rb +6 -5
- data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
- data/lib/active_support/core_ext/kernel/concern.rb +5 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +4 -83
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/load_error.rb +3 -22
- data/lib/active_support/core_ext/marshal.rb +13 -10
- data/lib/active_support/core_ext/module.rb +14 -11
- 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 +43 -40
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
- data/lib/active_support/core_ext/module/concerning.rb +11 -12
- data/lib/active_support/core_ext/module/delegation.rb +121 -39
- data/lib/active_support/core_ext/module/deprecation.rb +4 -2
- data/lib/active_support/core_ext/module/introspection.rb +9 -9
- data/lib/active_support/core_ext/module/reachable.rb +5 -2
- data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
- data/lib/active_support/core_ext/module/remove_method.rb +8 -3
- data/lib/active_support/core_ext/name_error.rb +22 -2
- data/lib/active_support/core_ext/numeric.rb +6 -3
- data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
- data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
- data/lib/active_support/core_ext/numeric/time.rb +35 -38
- data/lib/active_support/core_ext/object.rb +14 -13
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +29 -4
- 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 +98 -45
- 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 +49 -19
- data/lib/active_support/core_ext/object/to_param.rb +3 -1
- data/lib/active_support/core_ext/object/to_query.rb +6 -4
- data/lib/active_support/core_ext/object/try.rb +70 -22
- data/lib/active_support/core_ext/object/with_options.rb +16 -3
- data/lib/active_support/core_ext/range.rb +7 -4
- data/lib/active_support/core_ext/range/conversions.rb +27 -7
- data/lib/active_support/core_ext/range/each.rb +19 -17
- data/lib/active_support/core_ext/range/include_range.rb +21 -19
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +2 -0
- data/lib/active_support/core_ext/regexp.rb +6 -0
- data/lib/active_support/core_ext/securerandom.rb +25 -0
- data/lib/active_support/core_ext/string.rb +15 -13
- data/lib/active_support/core_ext/string/access.rb +9 -7
- data/lib/active_support/core_ext/string/behavior.rb +3 -1
- data/lib/active_support/core_ext/string/conversions.rb +8 -5
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +10 -8
- data/lib/active_support/core_ext/string/indent.rb +6 -4
- data/lib/active_support/core_ext/string/inflections.rb +61 -24
- data/lib/active_support/core_ext/string/inquiry.rb +3 -1
- data/lib/active_support/core_ext/string/multibyte.rb +15 -7
- data/lib/active_support/core_ext/string/output_safety.rb +35 -35
- data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
- data/lib/active_support/core_ext/string/strip.rb +4 -5
- data/lib/active_support/core_ext/string/zones.rb +4 -2
- data/lib/active_support/core_ext/time.rb +7 -5
- data/lib/active_support/core_ext/time/acts_like.rb +3 -1
- data/lib/active_support/core_ext/time/calculations.rb +101 -51
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/time/conversions.rb +20 -13
- data/lib/active_support/core_ext/time/zones.rb +41 -7
- data/lib/active_support/core_ext/uri.rb +5 -4
- data/lib/active_support/current_attributes.rb +195 -0
- data/lib/active_support/dependencies.rb +143 -160
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/deprecation.rb +12 -9
- data/lib/active_support/deprecation/behaviors.rb +41 -12
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/instance_delegator.rb +17 -2
- data/lib/active_support/deprecation/method_wrappers.rb +54 -21
- data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
- data/lib/active_support/deprecation/reporting.rb +32 -12
- data/lib/active_support/descendants_tracker.rb +2 -0
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration.rb +326 -30
- data/lib/active_support/duration/iso8601_parser.rb +125 -0
- data/lib/active_support/duration/iso8601_serializer.rb +55 -0
- data/lib/active_support/encrypted_configuration.rb +49 -0
- data/lib/active_support/encrypted_file.rb +99 -0
- data/lib/active_support/evented_file_update_checker.rb +205 -0
- data/lib/active_support/execution_wrapper.rb +128 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +63 -37
- data/lib/active_support/gem_version.rb +4 -2
- data/lib/active_support/gzip.rb +7 -5
- data/lib/active_support/hash_with_indifferent_access.rb +130 -30
- data/lib/active_support/i18n.rb +8 -6
- data/lib/active_support/i18n_railtie.rb +34 -14
- data/lib/active_support/inflections.rb +13 -11
- data/lib/active_support/inflector.rb +7 -5
- data/lib/active_support/inflector/inflections.rb +61 -12
- data/lib/active_support/inflector/methods.rb +161 -136
- data/lib/active_support/inflector/transliterate.rb +48 -27
- data/lib/active_support/json.rb +4 -2
- data/lib/active_support/json/decoding.rb +16 -13
- data/lib/active_support/json/encoding.rb +15 -57
- data/lib/active_support/key_generator.rb +25 -25
- data/lib/active_support/lazy_load_hooks.rb +50 -20
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber.rb +13 -10
- data/lib/active_support/log_subscriber/test_helper.rb +14 -12
- data/lib/active_support/logger.rb +54 -3
- data/lib/active_support/logger_silence.rb +12 -7
- data/lib/active_support/logger_thread_safe_level.rb +33 -0
- data/lib/active_support/message_encryptor.rb +173 -51
- data/lib/active_support/message_verifier.rb +150 -17
- data/lib/active_support/messages/metadata.rb +71 -0
- data/lib/active_support/messages/rotation_configuration.rb +22 -0
- data/lib/active_support/messages/rotator.rb +56 -0
- data/lib/active_support/multibyte.rb +4 -2
- data/lib/active_support/multibyte/chars.rb +37 -24
- data/lib/active_support/multibyte/unicode.rb +100 -96
- data/lib/active_support/notifications.rb +11 -7
- data/lib/active_support/notifications/fanout.rb +10 -8
- data/lib/active_support/notifications/instrumenter.rb +27 -7
- data/lib/active_support/number_helper.rb +94 -68
- data/lib/active_support/number_helper/number_converter.rb +13 -11
- data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
- data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
- data/lib/active_support/number_helper/rounding_helper.rb +66 -0
- data/lib/active_support/option_merger.rb +3 -1
- data/lib/active_support/ordered_hash.rb +6 -4
- data/lib/active_support/ordered_options.rb +22 -4
- data/lib/active_support/per_thread_registry.rb +13 -6
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +16 -8
- data/lib/active_support/railtie.rb +43 -9
- data/lib/active_support/reloader.rb +131 -0
- data/lib/active_support/rescuable.rb +108 -53
- data/lib/active_support/security_utils.rb +17 -6
- data/lib/active_support/string_inquirer.rb +11 -3
- data/lib/active_support/subscriber.rb +15 -14
- data/lib/active_support/tagged_logging.rb +14 -11
- data/lib/active_support/test_case.rb +18 -46
- data/lib/active_support/testing/assertions.rb +137 -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 +14 -10
- data/lib/active_support/testing/file_fixtures.rb +36 -0
- data/lib/active_support/testing/isolation.rb +34 -25
- data/lib/active_support/testing/method_call_assertions.rb +43 -0
- data/lib/active_support/testing/setup_and_teardown.rb +12 -3
- data/lib/active_support/testing/stream.rb +44 -0
- data/lib/active_support/testing/tagged_logging.rb +3 -1
- data/lib/active_support/testing/time_helpers.rb +96 -27
- data/lib/active_support/time.rb +14 -12
- data/lib/active_support/time_with_zone.rb +195 -53
- data/lib/active_support/values/time_zone.rb +200 -61
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +3 -1
- data/lib/active_support/xml_mini.rb +69 -51
- data/lib/active_support/xml_mini/jdom.rb +116 -113
- data/lib/active_support/xml_mini/libxml.rb +17 -16
- data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
- data/lib/active_support/xml_mini/nokogiri.rb +15 -15
- data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
- data/lib/active_support/xml_mini/rexml.rb +17 -16
- metadata +55 -43
- data/lib/active_support/concurrency/latch.rb +0 -27
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
- 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/kernel/debugger.rb +0 -10
- data/lib/active_support/core_ext/module/method_transplanting.rb +0 -11
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- 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
@@ -1,10 +1,10 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/string/multibyte"
|
4
|
+
require "active_support/i18n"
|
4
5
|
|
5
6
|
module ActiveSupport
|
6
7
|
module Inflector
|
7
|
-
|
8
8
|
# Replaces non-ASCII characters with an ASCII approximation, or if none
|
9
9
|
# exists, a replacement character which defaults to "?".
|
10
10
|
#
|
@@ -58,40 +58,61 @@ module ActiveSupport
|
|
58
58
|
# I18n.locale = :de
|
59
59
|
# transliterate('Jürgen')
|
60
60
|
# # => "Juergen"
|
61
|
-
def transliterate(string, replacement = "?")
|
62
|
-
|
63
|
-
|
64
|
-
|
61
|
+
def transliterate(string, replacement = "?".freeze)
|
62
|
+
raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String)
|
63
|
+
|
64
|
+
I18n.transliterate(
|
65
|
+
ActiveSupport::Multibyte::Unicode.normalize(
|
66
|
+
ActiveSupport::Multibyte::Unicode.tidy_bytes(string), :c),
|
67
|
+
replacement: replacement)
|
65
68
|
end
|
66
69
|
|
67
70
|
# Replaces special characters in a string so that it may be used as part of
|
68
71
|
# a 'pretty' URL.
|
69
72
|
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
73
|
+
# parameterize("Donald E. Knuth") # => "donald-e-knuth"
|
74
|
+
# parameterize("^très|Jolie-- ") # => "tres-jolie"
|
75
|
+
#
|
76
|
+
# To use a custom separator, override the +separator+ argument.
|
77
|
+
#
|
78
|
+
# parameterize("Donald E. Knuth", separator: '_') # => "donald_e_knuth"
|
79
|
+
# parameterize("^très|Jolie__ ", separator: '_') # => "tres_jolie"
|
80
|
+
#
|
81
|
+
# To preserve the case of the characters in a string, use the +preserve_case+ argument.
|
75
82
|
#
|
76
|
-
#
|
77
|
-
#
|
83
|
+
# parameterize("Donald E. Knuth", preserve_case: true) # => "Donald-E-Knuth"
|
84
|
+
# parameterize("^très|Jolie-- ", preserve_case: true) # => "tres-Jolie"
|
78
85
|
#
|
79
|
-
#
|
80
|
-
#
|
81
|
-
|
82
|
-
|
86
|
+
# It preserves dashes and underscores unless they are used as separators:
|
87
|
+
#
|
88
|
+
# parameterize("^très|Jolie__ ") # => "tres-jolie__"
|
89
|
+
# parameterize("^très|Jolie-- ", separator: "_") # => "tres_jolie--"
|
90
|
+
# parameterize("^très_Jolie-- ", separator: ".") # => "tres_jolie--"
|
91
|
+
#
|
92
|
+
def parameterize(string, separator: "-", preserve_case: false)
|
93
|
+
# Replace accented chars with their ASCII equivalents.
|
83
94
|
parameterized_string = transliterate(string)
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
95
|
+
|
96
|
+
# Turn unwanted chars into the separator.
|
97
|
+
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, separator)
|
98
|
+
|
99
|
+
unless separator.nil? || separator.empty?
|
100
|
+
if separator == "-".freeze
|
101
|
+
re_duplicate_separator = /-{2,}/
|
102
|
+
re_leading_trailing_separator = /^-|-$/i
|
103
|
+
else
|
104
|
+
re_sep = Regexp.escape(separator)
|
105
|
+
re_duplicate_separator = /#{re_sep}{2,}/
|
106
|
+
re_leading_trailing_separator = /^#{re_sep}|#{re_sep}$/i
|
107
|
+
end
|
88
108
|
# No more than one of the separator in a row.
|
89
|
-
parameterized_string.gsub!(
|
109
|
+
parameterized_string.gsub!(re_duplicate_separator, separator)
|
90
110
|
# Remove leading/trailing separator.
|
91
|
-
parameterized_string.gsub!(
|
111
|
+
parameterized_string.gsub!(re_leading_trailing_separator, "".freeze)
|
92
112
|
end
|
93
|
-
parameterized_string.downcase
|
94
|
-
end
|
95
113
|
|
114
|
+
parameterized_string.downcase! unless preserve_case
|
115
|
+
parameterized_string
|
116
|
+
end
|
96
117
|
end
|
97
118
|
end
|
data/lib/active_support/json.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/attribute_accessors"
|
4
|
+
require "active_support/core_ext/module/delegation"
|
5
|
+
require "json"
|
4
6
|
|
5
7
|
module ActiveSupport
|
6
8
|
# Look for and parse json strings that look like ISO 8601 times.
|
@@ -8,21 +10,16 @@ module ActiveSupport
|
|
8
10
|
|
9
11
|
module JSON
|
10
12
|
# matches YAML-formatted dates
|
11
|
-
DATE_REGEX =
|
12
|
-
|
13
|
+
DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/
|
14
|
+
DATETIME_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[T \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?)$/
|
15
|
+
|
13
16
|
class << self
|
14
17
|
# Parses a JSON string (JavaScript Object Notation) into a hash.
|
15
18
|
# See http://www.json.org for more info.
|
16
19
|
#
|
17
20
|
# ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
|
18
21
|
# => {"team" => "rails", "players" => "36"}
|
19
|
-
def decode(json
|
20
|
-
if options.present?
|
21
|
-
raise ArgumentError, "In Rails 4.1, ActiveSupport::JSON.decode no longer " \
|
22
|
-
"accepts an options hash for MultiJSON. MultiJSON reached its end of life " \
|
23
|
-
"and has been removed."
|
24
|
-
end
|
25
|
-
|
22
|
+
def decode(json)
|
26
23
|
data = ::JSON.parse(json, quirks_mode: true)
|
27
24
|
|
28
25
|
if ActiveSupport.parse_json_times
|
@@ -54,7 +51,13 @@ module ActiveSupport
|
|
54
51
|
nil
|
55
52
|
when DATE_REGEX
|
56
53
|
begin
|
57
|
-
|
54
|
+
Date.parse(data)
|
55
|
+
rescue ArgumentError
|
56
|
+
data
|
57
|
+
end
|
58
|
+
when DATETIME_REGEX
|
59
|
+
begin
|
60
|
+
Time.zone.parse(data)
|
58
61
|
rescue ArgumentError
|
59
62
|
data
|
60
63
|
end
|
@@ -1,14 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/object/json"
|
4
|
+
require "active_support/core_ext/module/delegation"
|
3
5
|
|
4
6
|
module ActiveSupport
|
5
7
|
class << self
|
6
8
|
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
|
7
9
|
:time_precision, :time_precision=,
|
8
10
|
:escape_html_entities_in_json, :escape_html_entities_in_json=,
|
9
|
-
:encode_big_decimal_as_string, :encode_big_decimal_as_string=,
|
10
11
|
:json_encoder, :json_encoder=,
|
11
|
-
:
|
12
|
+
to: :'ActiveSupport::JSON::Encoding'
|
12
13
|
end
|
13
14
|
|
14
15
|
module JSON
|
@@ -41,9 +42,9 @@ module ActiveSupport
|
|
41
42
|
ESCAPED_CHARS = {
|
42
43
|
"\u2028" => '\u2028',
|
43
44
|
"\u2029" => '\u2029',
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
">" => '\u003e',
|
46
|
+
"<" => '\u003c',
|
47
|
+
"&" => '\u0026',
|
47
48
|
}
|
48
49
|
|
49
50
|
ESCAPE_REGEX_WITH_HTML_ENTITIES = /[\u2028\u2029><&]/u
|
@@ -58,6 +59,10 @@ module ActiveSupport
|
|
58
59
|
super.gsub ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, ESCAPED_CHARS
|
59
60
|
end
|
60
61
|
end
|
62
|
+
|
63
|
+
def to_s
|
64
|
+
self
|
65
|
+
end
|
61
66
|
end
|
62
67
|
|
63
68
|
# Mark these as private so we don't leak encoding-specific constructs
|
@@ -65,7 +70,8 @@ module ActiveSupport
|
|
65
70
|
:ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, :EscapedString
|
66
71
|
|
67
72
|
# Convert an object into a "JSON-ready" representation composed of
|
68
|
-
# primitives like Hash, Array, String, Numeric,
|
73
|
+
# primitives like Hash, Array, String, Numeric,
|
74
|
+
# and +true+/+false+/+nil+.
|
69
75
|
# Recursively calls #as_json to the object to recursively build a
|
70
76
|
# fully JSON-ready object.
|
71
77
|
#
|
@@ -81,7 +87,7 @@ module ActiveSupport
|
|
81
87
|
when String
|
82
88
|
EscapedString.new(value)
|
83
89
|
when Numeric, NilClass, TrueClass, FalseClass
|
84
|
-
value
|
90
|
+
value.as_json
|
85
91
|
when Hash
|
86
92
|
Hash[value.map { |k, v| [jsonify(k), jsonify(v)] }]
|
87
93
|
when Array
|
@@ -113,54 +119,6 @@ module ActiveSupport
|
|
113
119
|
# Sets the encoder used by Rails to encode Ruby objects into JSON strings
|
114
120
|
# in +Object#to_json+ and +ActiveSupport::JSON.encode+.
|
115
121
|
attr_accessor :json_encoder
|
116
|
-
|
117
|
-
def encode_big_decimal_as_string=(as_string)
|
118
|
-
message = \
|
119
|
-
"The JSON encoder in Rails 4.1 no longer supports encoding BigDecimals as JSON numbers. Instead, " \
|
120
|
-
"the new encoder will always encode them as strings.\n\n" \
|
121
|
-
"You are seeing this error because you have 'active_support.encode_big_decimal_as_string' in " \
|
122
|
-
"your configuration file. If you have been setting this to true, you can safely remove it from " \
|
123
|
-
"your configuration. Otherwise, you should add the 'activesupport-json_encoder' gem to your " \
|
124
|
-
"Gemfile in order to restore this functionality."
|
125
|
-
|
126
|
-
raise NotImplementedError, message
|
127
|
-
end
|
128
|
-
|
129
|
-
def encode_big_decimal_as_string
|
130
|
-
message = \
|
131
|
-
"The JSON encoder in Rails 4.1 no longer supports encoding BigDecimals as JSON numbers. Instead, " \
|
132
|
-
"the new encoder will always encode them as strings.\n\n" \
|
133
|
-
"You are seeing this error because you are trying to check the value of the related configuration, " \
|
134
|
-
"`active_support.encode_big_decimal_as_string`. If your application depends on this option, you should " \
|
135
|
-
"add the 'activesupport-json_encoder' gem to your Gemfile. For now, this option will always be true. " \
|
136
|
-
"In the future, it will be removed from Rails, so you should stop checking its value."
|
137
|
-
|
138
|
-
ActiveSupport::Deprecation.warn message
|
139
|
-
|
140
|
-
true
|
141
|
-
end
|
142
|
-
|
143
|
-
# Deprecate CircularReferenceError
|
144
|
-
def const_missing(name)
|
145
|
-
if name == :CircularReferenceError
|
146
|
-
message = "The JSON encoder in Rails 4.1 no longer offers protection from circular references. " \
|
147
|
-
"You are seeing this warning because you are rescuing from (or otherwise referencing) " \
|
148
|
-
"ActiveSupport::Encoding::CircularReferenceError. In the future, this error will be " \
|
149
|
-
"removed from Rails. You should remove these rescue blocks from your code and ensure " \
|
150
|
-
"that your data structures are free of circular references so they can be properly " \
|
151
|
-
"serialized into JSON.\n\n" \
|
152
|
-
"For example, the following Hash contains a circular reference to itself:\n" \
|
153
|
-
" h = {}\n" \
|
154
|
-
" h['circular'] = h\n" \
|
155
|
-
"In this case, calling h.to_json would not work properly."
|
156
|
-
|
157
|
-
ActiveSupport::Deprecation.warn message
|
158
|
-
|
159
|
-
SystemStackError
|
160
|
-
else
|
161
|
-
super
|
162
|
-
end
|
163
|
-
end
|
164
122
|
end
|
165
123
|
|
166
124
|
self.use_standard_json_time_format = true
|
@@ -1,8 +1,10 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "concurrent/map"
|
4
|
+
require "openssl"
|
3
5
|
|
4
6
|
module ActiveSupport
|
5
|
-
# KeyGenerator is a simple wrapper around OpenSSL's implementation of PBKDF2
|
7
|
+
# KeyGenerator is a simple wrapper around OpenSSL's implementation of PBKDF2.
|
6
8
|
# It can be used to derive a number of keys for various purposes from a given secret.
|
7
9
|
# This lets Rails applications have a single secure secret, but avoid reusing that
|
8
10
|
# key in multiple incompatible contexts.
|
@@ -17,25 +19,23 @@ module ActiveSupport
|
|
17
19
|
# Returns a derived key suitable for use. The default key_size is chosen
|
18
20
|
# to be compatible with the default settings of ActiveSupport::MessageVerifier.
|
19
21
|
# i.e. OpenSSL::Digest::SHA1#block_length
|
20
|
-
def generate_key(salt, key_size=64)
|
22
|
+
def generate_key(salt, key_size = 64)
|
21
23
|
OpenSSL::PKCS5.pbkdf2_hmac_sha1(@secret, salt, @iterations, key_size)
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
27
|
# CachingKeyGenerator is a wrapper around KeyGenerator which allows users to avoid
|
26
28
|
# re-executing the key generation process when it's called using the same salt and
|
27
|
-
# key_size
|
29
|
+
# key_size.
|
28
30
|
class CachingKeyGenerator
|
29
31
|
def initialize(key_generator)
|
30
32
|
@key_generator = key_generator
|
31
|
-
@cache_keys =
|
33
|
+
@cache_keys = Concurrent::Map.new
|
32
34
|
end
|
33
35
|
|
34
|
-
# Returns a derived key suitable for use.
|
35
|
-
|
36
|
-
|
37
|
-
def generate_key(salt, key_size=64)
|
38
|
-
@cache_keys["#{salt}#{key_size}"] ||= @key_generator.generate_key(salt, key_size)
|
36
|
+
# Returns a derived key suitable for use.
|
37
|
+
def generate_key(*args)
|
38
|
+
@cache_keys[args.join] ||= @key_generator.generate_key(*args)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -53,21 +53,21 @@ module ActiveSupport
|
|
53
53
|
|
54
54
|
private
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
56
|
+
# To prevent users from using something insecure like "Password" we make sure that the
|
57
|
+
# secret they've provided is at least 30 characters in length.
|
58
|
+
def ensure_secret_secure(secret)
|
59
|
+
if secret.blank?
|
60
|
+
raise ArgumentError, "A secret is required to generate an integrity hash " \
|
61
|
+
"for cookie session data. Set a secret_key_base of at least " \
|
62
|
+
"#{SECRET_MIN_LENGTH} characters in via `bin/rails credentials:edit`."
|
63
|
+
end
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
65
|
+
if secret.length < SECRET_MIN_LENGTH
|
66
|
+
raise ArgumentError, "Secret should be something secure, " \
|
67
|
+
"like \"#{SecureRandom.hex(16)}\". The value you " \
|
68
|
+
"provided, \"#{secret}\", is shorter than the minimum length " \
|
69
|
+
"of #{SECRET_MIN_LENGTH} characters."
|
70
|
+
end
|
70
71
|
end
|
71
|
-
end
|
72
72
|
end
|
73
73
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveSupport
|
2
4
|
# lazy_load_hooks allows Rails to lazily load a lot of components and thus
|
3
5
|
# making the app boot faster. Because of this feature now there is no need to
|
@@ -15,34 +17,62 @@ module ActiveSupport
|
|
15
17
|
# end
|
16
18
|
# end
|
17
19
|
#
|
18
|
-
# When the entirety of +
|
20
|
+
# When the entirety of +ActiveRecord::Base+ has been
|
19
21
|
# evaluated then +run_load_hooks+ is invoked. The very last line of
|
20
|
-
# +
|
22
|
+
# +ActiveRecord::Base+ is:
|
21
23
|
#
|
22
24
|
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
module LazyLoadHooks
|
26
|
+
def self.extended(base) # :nodoc:
|
27
|
+
base.class_eval do
|
28
|
+
@load_hooks = Hash.new { |h, k| h[k] = [] }
|
29
|
+
@loaded = Hash.new { |h, k| h[k] = [] }
|
30
|
+
@run_once = Hash.new { |h, k| h[k] = [] }
|
31
|
+
end
|
29
32
|
end
|
30
33
|
|
31
|
-
|
32
|
-
|
34
|
+
# Declares a block that will be executed when a Rails component is fully
|
35
|
+
# loaded.
|
36
|
+
#
|
37
|
+
# Options:
|
38
|
+
#
|
39
|
+
# * <tt>:yield</tt> - Yields the object that run_load_hooks to +block+.
|
40
|
+
# * <tt>:run_once</tt> - Given +block+ will run only once.
|
41
|
+
def on_load(name, options = {}, &block)
|
42
|
+
@loaded[name].each do |base|
|
43
|
+
execute_hook(name, base, options, block)
|
44
|
+
end
|
33
45
|
|
34
|
-
|
35
|
-
if options[:yield]
|
36
|
-
block.call(base)
|
37
|
-
else
|
38
|
-
base.instance_eval(&block)
|
46
|
+
@load_hooks[name] << [block, options]
|
39
47
|
end
|
40
|
-
end
|
41
48
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
49
|
+
def run_load_hooks(name, base = Object)
|
50
|
+
@loaded[name] << base
|
51
|
+
@load_hooks[name].each do |hook, options|
|
52
|
+
execute_hook(name, base, options, hook)
|
53
|
+
end
|
46
54
|
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def with_execution_control(name, block, once)
|
59
|
+
unless @run_once[name].include?(block)
|
60
|
+
@run_once[name] << block if once
|
61
|
+
|
62
|
+
yield
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def execute_hook(name, base, options, block)
|
67
|
+
with_execution_control(name, block, options[:run_once]) do
|
68
|
+
if options[:yield]
|
69
|
+
block.call(base)
|
70
|
+
else
|
71
|
+
base.instance_eval(&block)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
47
75
|
end
|
76
|
+
|
77
|
+
extend LazyLoadHooks
|
48
78
|
end
|