activesupport 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +572 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +40 -0
- data/lib/active_support.rb +96 -0
- data/lib/active_support/actionable_error.rb +48 -0
- data/lib/active_support/all.rb +5 -0
- data/lib/active_support/array_inquirer.rb +48 -0
- data/lib/active_support/backtrace_cleaner.rb +132 -0
- data/lib/active_support/benchmarkable.rb +51 -0
- data/lib/active_support/builder.rb +8 -0
- data/lib/active_support/cache.rb +830 -0
- data/lib/active_support/cache/file_store.rb +196 -0
- data/lib/active_support/cache/mem_cache_store.rb +212 -0
- data/lib/active_support/cache/memory_store.rb +174 -0
- data/lib/active_support/cache/null_store.rb +48 -0
- data/lib/active_support/cache/redis_cache_store.rb +488 -0
- data/lib/active_support/cache/strategy/local_cache.rb +194 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
- data/lib/active_support/callbacks.rb +856 -0
- data/lib/active_support/concern.rb +171 -0
- 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 +146 -0
- data/lib/active_support/core_ext.rb +5 -0
- data/lib/active_support/core_ext/array.rb +9 -0
- data/lib/active_support/core_ext/array/access.rb +104 -0
- data/lib/active_support/core_ext/array/conversions.rb +213 -0
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/extract_options.rb +31 -0
- data/lib/active_support/core_ext/array/grouping.rb +109 -0
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -0
- data/lib/active_support/core_ext/array/wrap.rb +48 -0
- data/lib/active_support/core_ext/benchmark.rb +16 -0
- data/lib/active_support/core_ext/big_decimal.rb +3 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/class/attribute.rb +141 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/class/subclasses.rb +54 -0
- data/lib/active_support/core_ext/date.rb +7 -0
- data/lib/active_support/core_ext/date/acts_like.rb +10 -0
- data/lib/active_support/core_ext/date/blank.rb +14 -0
- data/lib/active_support/core_ext/date/calculations.rb +146 -0
- data/lib/active_support/core_ext/date/conversions.rb +96 -0
- data/lib/active_support/core_ext/date/zones.rb +8 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +351 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +41 -0
- data/lib/active_support/core_ext/date_time.rb +7 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
- data/lib/active_support/core_ext/date_time/blank.rb +14 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +211 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +107 -0
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/digest/uuid.rb +53 -0
- data/lib/active_support/core_ext/enumerable.rb +188 -0
- data/lib/active_support/core_ext/file.rb +3 -0
- data/lib/active_support/core_ext/file/atomic.rb +70 -0
- data/lib/active_support/core_ext/hash.rb +10 -0
- data/lib/active_support/core_ext/hash/compact.rb +5 -0
- data/lib/active_support/core_ext/hash/conversions.rb +263 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +34 -0
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +24 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
- data/lib/active_support/core_ext/hash/keys.rb +143 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
- data/lib/active_support/core_ext/hash/slice.rb +26 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +5 -0
- data/lib/active_support/core_ext/integer.rb +5 -0
- data/lib/active_support/core_ext/integer/inflections.rb +31 -0
- data/lib/active_support/core_ext/integer/multiple.rb +12 -0
- data/lib/active_support/core_ext/integer/time.rb +22 -0
- data/lib/active_support/core_ext/kernel.rb +5 -0
- data/lib/active_support/core_ext/kernel/concern.rb +14 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
- data/lib/active_support/core_ext/load_error.rb +9 -0
- data/lib/active_support/core_ext/marshal.rb +24 -0
- data/lib/active_support/core_ext/module.rb +13 -0
- data/lib/active_support/core_ext/module/aliasing.rb +31 -0
- data/lib/active_support/core_ext/module/anonymous.rb +30 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +38 -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 +144 -0
- data/lib/active_support/core_ext/module/concerning.rb +134 -0
- data/lib/active_support/core_ext/module/delegation.rb +313 -0
- data/lib/active_support/core_ext/module/deprecation.rb +25 -0
- data/lib/active_support/core_ext/module/introspection.rb +86 -0
- data/lib/active_support/core_ext/module/reachable.rb +6 -0
- data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
- data/lib/active_support/core_ext/module/remove_method.rb +17 -0
- data/lib/active_support/core_ext/name_error.rb +38 -0
- data/lib/active_support/core_ext/numeric.rb +5 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +66 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +136 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
- data/lib/active_support/core_ext/numeric/time.rb +66 -0
- data/lib/active_support/core_ext/object.rb +16 -0
- data/lib/active_support/core_ext/object/acts_like.rb +21 -0
- data/lib/active_support/core_ext/object/blank.rb +155 -0
- data/lib/active_support/core_ext/object/conversions.rb +6 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +55 -0
- data/lib/active_support/core_ext/object/duplicable.rb +49 -0
- data/lib/active_support/core_ext/object/inclusion.rb +29 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +30 -0
- data/lib/active_support/core_ext/object/json.rb +228 -0
- data/lib/active_support/core_ext/object/to_param.rb +3 -0
- data/lib/active_support/core_ext/object/to_query.rb +89 -0
- data/lib/active_support/core_ext/object/try.rb +156 -0
- data/lib/active_support/core_ext/object/with_options.rb +82 -0
- data/lib/active_support/core_ext/range.rb +7 -0
- data/lib/active_support/core_ext/range/compare_range.rb +70 -0
- data/lib/active_support/core_ext/range/conversions.rb +41 -0
- data/lib/active_support/core_ext/range/each.rb +25 -0
- data/lib/active_support/core_ext/range/include_range.rb +9 -0
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +10 -0
- data/lib/active_support/core_ext/regexp.rb +7 -0
- data/lib/active_support/core_ext/securerandom.rb +45 -0
- data/lib/active_support/core_ext/string.rb +15 -0
- data/lib/active_support/core_ext/string/access.rb +114 -0
- data/lib/active_support/core_ext/string/behavior.rb +8 -0
- data/lib/active_support/core_ext/string/conversions.rb +59 -0
- data/lib/active_support/core_ext/string/exclude.rb +13 -0
- data/lib/active_support/core_ext/string/filters.rb +145 -0
- data/lib/active_support/core_ext/string/indent.rb +45 -0
- data/lib/active_support/core_ext/string/inflections.rb +259 -0
- data/lib/active_support/core_ext/string/inquiry.rb +15 -0
- data/lib/active_support/core_ext/string/multibyte.rb +58 -0
- data/lib/active_support/core_ext/string/output_safety.rb +314 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/string/strip.rb +27 -0
- data/lib/active_support/core_ext/string/zones.rb +16 -0
- data/lib/active_support/core_ext/time.rb +7 -0
- data/lib/active_support/core_ext/time/acts_like.rb +10 -0
- data/lib/active_support/core_ext/time/calculations.rb +344 -0
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/time/conversions.rb +72 -0
- data/lib/active_support/core_ext/time/zones.rb +113 -0
- data/lib/active_support/core_ext/uri.rb +25 -0
- data/lib/active_support/current_attributes.rb +203 -0
- data/lib/active_support/dependencies.rb +806 -0
- data/lib/active_support/dependencies/autoload.rb +79 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/dependencies/zeitwerk_integration.rb +110 -0
- data/lib/active_support/deprecation.rb +46 -0
- data/lib/active_support/deprecation/behaviors.rb +109 -0
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/instance_delegator.rb +39 -0
- data/lib/active_support/deprecation/method_wrappers.rb +78 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +173 -0
- data/lib/active_support/deprecation/reporting.rb +114 -0
- data/lib/active_support/descendants_tracker.rb +109 -0
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration.rb +433 -0
- data/lib/active_support/duration/iso8601_parser.rb +124 -0
- data/lib/active_support/duration/iso8601_serializer.rb +54 -0
- data/lib/active_support/encrypted_configuration.rb +45 -0
- data/lib/active_support/encrypted_file.rb +100 -0
- data/lib/active_support/evented_file_update_checker.rb +235 -0
- data/lib/active_support/execution_wrapper.rb +129 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +163 -0
- data/lib/active_support/gem_version.rb +17 -0
- data/lib/active_support/gzip.rb +38 -0
- data/lib/active_support/hash_with_indifferent_access.rb +399 -0
- data/lib/active_support/i18n.rb +16 -0
- data/lib/active_support/i18n_railtie.rb +126 -0
- data/lib/active_support/inflections.rb +72 -0
- data/lib/active_support/inflector.rb +9 -0
- data/lib/active_support/inflector/inflections.rb +257 -0
- data/lib/active_support/inflector/methods.rb +398 -0
- data/lib/active_support/inflector/transliterate.rb +147 -0
- data/lib/active_support/json.rb +4 -0
- data/lib/active_support/json/decoding.rb +76 -0
- data/lib/active_support/json/encoding.rb +134 -0
- data/lib/active_support/key_generator.rb +41 -0
- data/lib/active_support/lazy_load_hooks.rb +82 -0
- data/lib/active_support/locale/en.rb +31 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +135 -0
- data/lib/active_support/log_subscriber/test_helper.rb +106 -0
- data/lib/active_support/logger.rb +93 -0
- data/lib/active_support/logger_silence.rb +45 -0
- data/lib/active_support/logger_thread_safe_level.rb +56 -0
- data/lib/active_support/message_encryptor.rb +227 -0
- data/lib/active_support/message_verifier.rb +205 -0
- 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 +23 -0
- data/lib/active_support/multibyte/chars.rb +216 -0
- data/lib/active_support/multibyte/unicode.rb +157 -0
- data/lib/active_support/notifications.rb +253 -0
- data/lib/active_support/notifications/fanout.rb +244 -0
- data/lib/active_support/notifications/instrumenter.rb +164 -0
- data/lib/active_support/number_helper.rb +378 -0
- data/lib/active_support/number_helper/number_converter.rb +184 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +31 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +70 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +61 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +56 -0
- data/lib/active_support/number_helper/rounding_helper.rb +66 -0
- data/lib/active_support/option_merger.rb +27 -0
- data/lib/active_support/ordered_hash.rb +50 -0
- data/lib/active_support/ordered_options.rb +85 -0
- data/lib/active_support/parameter_filter.rb +129 -0
- data/lib/active_support/per_thread_registry.rb +60 -0
- data/lib/active_support/proxy_object.rb +15 -0
- data/lib/active_support/rails.rb +29 -0
- data/lib/active_support/railtie.rb +80 -0
- data/lib/active_support/reloader.rb +130 -0
- data/lib/active_support/rescuable.rb +174 -0
- data/lib/active_support/security_utils.rb +31 -0
- data/lib/active_support/string_inquirer.rb +34 -0
- data/lib/active_support/subscriber.rb +169 -0
- data/lib/active_support/tagged_logging.rb +88 -0
- data/lib/active_support/test_case.rb +163 -0
- data/lib/active_support/testing/assertions.rb +228 -0
- data/lib/active_support/testing/autorun.rb +7 -0
- data/lib/active_support/testing/constant_lookup.rb +51 -0
- data/lib/active_support/testing/declarative.rb +28 -0
- data/lib/active_support/testing/deprecation.rb +38 -0
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +110 -0
- data/lib/active_support/testing/method_call_assertions.rb +70 -0
- data/lib/active_support/testing/parallelization.rb +128 -0
- data/lib/active_support/testing/setup_and_teardown.rb +55 -0
- data/lib/active_support/testing/stream.rb +44 -0
- data/lib/active_support/testing/tagged_logging.rb +27 -0
- data/lib/active_support/testing/time_helpers.rb +200 -0
- data/lib/active_support/time.rb +20 -0
- data/lib/active_support/time_with_zone.rb +561 -0
- data/lib/active_support/values/time_zone.rb +570 -0
- data/lib/active_support/version.rb +10 -0
- data/lib/active_support/xml_mini.rb +202 -0
- data/lib/active_support/xml_mini/jdom.rb +183 -0
- data/lib/active_support/xml_mini/libxml.rb +80 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
- data/lib/active_support/xml_mini/nokogiri.rb +83 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
- data/lib/active_support/xml_mini/rexml.rb +130 -0
- metadata +385 -0
|
@@ -0,0 +1,76 @@
|
|
|
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"
|
|
6
|
+
|
|
7
|
+
module ActiveSupport
|
|
8
|
+
# Look for and parse json strings that look like ISO 8601 times.
|
|
9
|
+
mattr_accessor :parse_json_times
|
|
10
|
+
|
|
11
|
+
module JSON
|
|
12
|
+
# matches YAML-formatted dates
|
|
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
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# Parses a JSON string (JavaScript Object Notation) into a hash.
|
|
18
|
+
# See http://www.json.org for more info.
|
|
19
|
+
#
|
|
20
|
+
# ActiveSupport::JSON.decode("{\"team\":\"rails\",\"players\":\"36\"}")
|
|
21
|
+
# => {"team" => "rails", "players" => "36"}
|
|
22
|
+
def decode(json)
|
|
23
|
+
data = ::JSON.parse(json, quirks_mode: true)
|
|
24
|
+
|
|
25
|
+
if ActiveSupport.parse_json_times
|
|
26
|
+
convert_dates_from(data)
|
|
27
|
+
else
|
|
28
|
+
data
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Returns the class of the error that will be raised when there is an
|
|
33
|
+
# error in decoding JSON. Using this method means you won't directly
|
|
34
|
+
# depend on the ActiveSupport's JSON implementation, in case it changes
|
|
35
|
+
# in the future.
|
|
36
|
+
#
|
|
37
|
+
# begin
|
|
38
|
+
# obj = ActiveSupport::JSON.decode(some_string)
|
|
39
|
+
# rescue ActiveSupport::JSON.parse_error
|
|
40
|
+
# Rails.logger.warn("Attempted to decode invalid JSON: #{some_string}")
|
|
41
|
+
# end
|
|
42
|
+
def parse_error
|
|
43
|
+
::JSON::ParserError
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def convert_dates_from(data)
|
|
49
|
+
case data
|
|
50
|
+
when nil
|
|
51
|
+
nil
|
|
52
|
+
when DATE_REGEX
|
|
53
|
+
begin
|
|
54
|
+
Date.parse(data)
|
|
55
|
+
rescue ArgumentError
|
|
56
|
+
data
|
|
57
|
+
end
|
|
58
|
+
when DATETIME_REGEX
|
|
59
|
+
begin
|
|
60
|
+
Time.zone.parse(data)
|
|
61
|
+
rescue ArgumentError
|
|
62
|
+
data
|
|
63
|
+
end
|
|
64
|
+
when Array
|
|
65
|
+
data.map! { |d| convert_dates_from(d) }
|
|
66
|
+
when Hash
|
|
67
|
+
data.each do |key, value|
|
|
68
|
+
data[key] = convert_dates_from(value)
|
|
69
|
+
end
|
|
70
|
+
else
|
|
71
|
+
data
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/core_ext/object/json"
|
|
4
|
+
require "active_support/core_ext/module/delegation"
|
|
5
|
+
|
|
6
|
+
module ActiveSupport
|
|
7
|
+
class << self
|
|
8
|
+
delegate :use_standard_json_time_format, :use_standard_json_time_format=,
|
|
9
|
+
:time_precision, :time_precision=,
|
|
10
|
+
:escape_html_entities_in_json, :escape_html_entities_in_json=,
|
|
11
|
+
:json_encoder, :json_encoder=,
|
|
12
|
+
to: :'ActiveSupport::JSON::Encoding'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module JSON
|
|
16
|
+
# Dumps objects in JSON (JavaScript Object Notation).
|
|
17
|
+
# See http://www.json.org for more info.
|
|
18
|
+
#
|
|
19
|
+
# ActiveSupport::JSON.encode({ team: 'rails', players: '36' })
|
|
20
|
+
# # => "{\"team\":\"rails\",\"players\":\"36\"}"
|
|
21
|
+
def self.encode(value, options = nil)
|
|
22
|
+
Encoding.json_encoder.new(options).encode(value)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
module Encoding #:nodoc:
|
|
26
|
+
class JSONGemEncoder #:nodoc:
|
|
27
|
+
attr_reader :options
|
|
28
|
+
|
|
29
|
+
def initialize(options = nil)
|
|
30
|
+
@options = options || {}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Encode the given object into a JSON string
|
|
34
|
+
def encode(value)
|
|
35
|
+
stringify jsonify value.as_json(options.dup)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
# Rails does more escaping than the JSON gem natively does (we
|
|
40
|
+
# escape \u2028 and \u2029 and optionally >, <, & to work around
|
|
41
|
+
# certain browser problems).
|
|
42
|
+
ESCAPED_CHARS = {
|
|
43
|
+
"\u2028" => '\u2028',
|
|
44
|
+
"\u2029" => '\u2029',
|
|
45
|
+
">" => '\u003e',
|
|
46
|
+
"<" => '\u003c',
|
|
47
|
+
"&" => '\u0026',
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
ESCAPE_REGEX_WITH_HTML_ENTITIES = /[\u2028\u2029><&]/u
|
|
51
|
+
ESCAPE_REGEX_WITHOUT_HTML_ENTITIES = /[\u2028\u2029]/u
|
|
52
|
+
|
|
53
|
+
# This class wraps all the strings we see and does the extra escaping
|
|
54
|
+
class EscapedString < String #:nodoc:
|
|
55
|
+
def to_json(*)
|
|
56
|
+
if Encoding.escape_html_entities_in_json
|
|
57
|
+
s = super
|
|
58
|
+
s.gsub! ESCAPE_REGEX_WITH_HTML_ENTITIES, ESCAPED_CHARS
|
|
59
|
+
s
|
|
60
|
+
else
|
|
61
|
+
s = super
|
|
62
|
+
s.gsub! ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, ESCAPED_CHARS
|
|
63
|
+
s
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def to_s
|
|
68
|
+
self
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Mark these as private so we don't leak encoding-specific constructs
|
|
73
|
+
private_constant :ESCAPED_CHARS, :ESCAPE_REGEX_WITH_HTML_ENTITIES,
|
|
74
|
+
:ESCAPE_REGEX_WITHOUT_HTML_ENTITIES, :EscapedString
|
|
75
|
+
|
|
76
|
+
# Convert an object into a "JSON-ready" representation composed of
|
|
77
|
+
# primitives like Hash, Array, String, Numeric,
|
|
78
|
+
# and +true+/+false+/+nil+.
|
|
79
|
+
# Recursively calls #as_json to the object to recursively build a
|
|
80
|
+
# fully JSON-ready object.
|
|
81
|
+
#
|
|
82
|
+
# This allows developers to implement #as_json without having to
|
|
83
|
+
# worry about what base types of objects they are allowed to return
|
|
84
|
+
# or having to remember to call #as_json recursively.
|
|
85
|
+
#
|
|
86
|
+
# Note: the +options+ hash passed to +object.to_json+ is only passed
|
|
87
|
+
# to +object.as_json+, not any of this method's recursive +#as_json+
|
|
88
|
+
# calls.
|
|
89
|
+
def jsonify(value)
|
|
90
|
+
case value
|
|
91
|
+
when String
|
|
92
|
+
EscapedString.new(value)
|
|
93
|
+
when Numeric, NilClass, TrueClass, FalseClass
|
|
94
|
+
value.as_json
|
|
95
|
+
when Hash
|
|
96
|
+
Hash[value.map { |k, v| [jsonify(k), jsonify(v)] }]
|
|
97
|
+
when Array
|
|
98
|
+
value.map { |v| jsonify(v) }
|
|
99
|
+
else
|
|
100
|
+
jsonify value.as_json
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Encode a "jsonified" Ruby data structure using the JSON gem
|
|
105
|
+
def stringify(jsonified)
|
|
106
|
+
::JSON.generate(jsonified, quirks_mode: true, max_nesting: false)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
class << self
|
|
111
|
+
# If true, use ISO 8601 format for dates and times. Otherwise, fall back
|
|
112
|
+
# to the Active Support legacy format.
|
|
113
|
+
attr_accessor :use_standard_json_time_format
|
|
114
|
+
|
|
115
|
+
# If true, encode >, <, & as escaped unicode sequences (e.g. > as \u003e)
|
|
116
|
+
# as a safety measure.
|
|
117
|
+
attr_accessor :escape_html_entities_in_json
|
|
118
|
+
|
|
119
|
+
# Sets the precision of encoded time values.
|
|
120
|
+
# Defaults to 3 (equivalent to millisecond precision)
|
|
121
|
+
attr_accessor :time_precision
|
|
122
|
+
|
|
123
|
+
# Sets the encoder used by Rails to encode Ruby objects into JSON strings
|
|
124
|
+
# in +Object#to_json+ and +ActiveSupport::JSON.encode+.
|
|
125
|
+
attr_accessor :json_encoder
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
self.use_standard_json_time_format = true
|
|
129
|
+
self.escape_html_entities_in_json = true
|
|
130
|
+
self.json_encoder = JSONGemEncoder
|
|
131
|
+
self.time_precision = 3
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "concurrent/map"
|
|
4
|
+
require "openssl"
|
|
5
|
+
|
|
6
|
+
module ActiveSupport
|
|
7
|
+
# KeyGenerator is a simple wrapper around OpenSSL's implementation of PBKDF2.
|
|
8
|
+
# It can be used to derive a number of keys for various purposes from a given secret.
|
|
9
|
+
# This lets Rails applications have a single secure secret, but avoid reusing that
|
|
10
|
+
# key in multiple incompatible contexts.
|
|
11
|
+
class KeyGenerator
|
|
12
|
+
def initialize(secret, options = {})
|
|
13
|
+
@secret = secret
|
|
14
|
+
# The default iterations are higher than required for our key derivation uses
|
|
15
|
+
# on the off chance someone uses this for password storage
|
|
16
|
+
@iterations = options[:iterations] || 2**16
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns a derived key suitable for use. The default key_size is chosen
|
|
20
|
+
# to be compatible with the default settings of ActiveSupport::MessageVerifier.
|
|
21
|
+
# i.e. OpenSSL::Digest::SHA1#block_length
|
|
22
|
+
def generate_key(salt, key_size = 64)
|
|
23
|
+
OpenSSL::PKCS5.pbkdf2_hmac_sha1(@secret, salt, @iterations, key_size)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# CachingKeyGenerator is a wrapper around KeyGenerator which allows users to avoid
|
|
28
|
+
# re-executing the key generation process when it's called using the same salt and
|
|
29
|
+
# key_size.
|
|
30
|
+
class CachingKeyGenerator
|
|
31
|
+
def initialize(key_generator)
|
|
32
|
+
@key_generator = key_generator
|
|
33
|
+
@cache_keys = Concurrent::Map.new
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Returns a derived key suitable for use.
|
|
37
|
+
def generate_key(*args)
|
|
38
|
+
@cache_keys[args.join] ||= @key_generator.generate_key(*args)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveSupport
|
|
4
|
+
# lazy_load_hooks allows Rails to lazily load a lot of components and thus
|
|
5
|
+
# making the app boot faster. Because of this feature now there is no need to
|
|
6
|
+
# require <tt>ActiveRecord::Base</tt> at boot time purely to apply
|
|
7
|
+
# configuration. Instead a hook is registered that applies configuration once
|
|
8
|
+
# <tt>ActiveRecord::Base</tt> is loaded. Here <tt>ActiveRecord::Base</tt> is
|
|
9
|
+
# used as example but this feature can be applied elsewhere too.
|
|
10
|
+
#
|
|
11
|
+
# Here is an example where +on_load+ method is called to register a hook.
|
|
12
|
+
#
|
|
13
|
+
# initializer 'active_record.initialize_timezone' do
|
|
14
|
+
# ActiveSupport.on_load(:active_record) do
|
|
15
|
+
# self.time_zone_aware_attributes = true
|
|
16
|
+
# self.default_timezone = :utc
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# When the entirety of +ActiveRecord::Base+ has been
|
|
21
|
+
# evaluated then +run_load_hooks+ is invoked. The very last line of
|
|
22
|
+
# +ActiveRecord::Base+ is:
|
|
23
|
+
#
|
|
24
|
+
# ActiveSupport.run_load_hooks(:active_record, ActiveRecord::Base)
|
|
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
|
|
32
|
+
end
|
|
33
|
+
|
|
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
|
|
45
|
+
|
|
46
|
+
@load_hooks[name] << [block, options]
|
|
47
|
+
end
|
|
48
|
+
|
|
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
|
|
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
|
+
if base.is_a?(Module)
|
|
72
|
+
base.class_eval(&block)
|
|
73
|
+
else
|
|
74
|
+
base.instance_eval(&block)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
extend LazyLoadHooks
|
|
82
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
{
|
|
4
|
+
en: {
|
|
5
|
+
number: {
|
|
6
|
+
nth: {
|
|
7
|
+
ordinals: lambda do |_key, number:, **_options|
|
|
8
|
+
case number
|
|
9
|
+
when 1; "st"
|
|
10
|
+
when 2; "nd"
|
|
11
|
+
when 3; "rd"
|
|
12
|
+
when 4, 5, 6, 7, 8, 9, 10, 11, 12, 13; "th"
|
|
13
|
+
else
|
|
14
|
+
num_modulo = number.to_i.abs % 100
|
|
15
|
+
num_modulo %= 10 if num_modulo > 13
|
|
16
|
+
case num_modulo
|
|
17
|
+
when 1; "st"
|
|
18
|
+
when 2; "nd"
|
|
19
|
+
when 3; "rd"
|
|
20
|
+
else "th"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end,
|
|
24
|
+
|
|
25
|
+
ordinalized: lambda do |_key, number:, **_options|
|
|
26
|
+
"#{number}#{ActiveSupport::Inflector.ordinal(number)}"
|
|
27
|
+
end
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
en:
|
|
2
|
+
date:
|
|
3
|
+
formats:
|
|
4
|
+
# Use the strftime parameters for formats.
|
|
5
|
+
# When no format has been given, it uses default.
|
|
6
|
+
# You can provide other formats here if you like!
|
|
7
|
+
default: "%Y-%m-%d"
|
|
8
|
+
short: "%b %d"
|
|
9
|
+
long: "%B %d, %Y"
|
|
10
|
+
|
|
11
|
+
day_names: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
|
|
12
|
+
abbr_day_names: [Sun, Mon, Tue, Wed, Thu, Fri, Sat]
|
|
13
|
+
|
|
14
|
+
# Don't forget the nil at the beginning; there's no such thing as a 0th month
|
|
15
|
+
month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December]
|
|
16
|
+
abbr_month_names: [~, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]
|
|
17
|
+
# Used in date_select and datetime_select.
|
|
18
|
+
order:
|
|
19
|
+
- year
|
|
20
|
+
- month
|
|
21
|
+
- day
|
|
22
|
+
|
|
23
|
+
time:
|
|
24
|
+
formats:
|
|
25
|
+
default: "%a, %d %b %Y %H:%M:%S %z"
|
|
26
|
+
short: "%d %b %H:%M"
|
|
27
|
+
long: "%B %d, %Y %H:%M"
|
|
28
|
+
am: "am"
|
|
29
|
+
pm: "pm"
|
|
30
|
+
|
|
31
|
+
# Used in array.to_sentence.
|
|
32
|
+
support:
|
|
33
|
+
array:
|
|
34
|
+
words_connector: ", "
|
|
35
|
+
two_words_connector: " and "
|
|
36
|
+
last_word_connector: ", and "
|
|
37
|
+
number:
|
|
38
|
+
# Used in NumberHelper.number_to_delimited()
|
|
39
|
+
# These are also the defaults for 'currency', 'percentage', 'precision', and 'human'
|
|
40
|
+
format:
|
|
41
|
+
# Sets the separator between the units, for more precision (e.g. 1.0 / 2.0 == 0.5)
|
|
42
|
+
separator: "."
|
|
43
|
+
# Delimits thousands (e.g. 1,000,000 is a million) (always in groups of three)
|
|
44
|
+
delimiter: ","
|
|
45
|
+
# Number of decimals, behind the separator (the number 1 with a precision of 2 gives: 1.00)
|
|
46
|
+
precision: 3
|
|
47
|
+
# If set to true, precision will mean the number of significant digits instead
|
|
48
|
+
# of the number of decimal digits (1234 with precision 2 becomes 1200, 1.23543 becomes 1.2)
|
|
49
|
+
significant: false
|
|
50
|
+
# If set, the zeros after the decimal separator will always be stripped (eg.: 1.200 will be 1.2)
|
|
51
|
+
strip_insignificant_zeros: false
|
|
52
|
+
|
|
53
|
+
# Used in NumberHelper.number_to_currency()
|
|
54
|
+
currency:
|
|
55
|
+
format:
|
|
56
|
+
# Where is the currency sign? %u is the currency unit, %n the number (default: $5.00)
|
|
57
|
+
format: "%u%n"
|
|
58
|
+
unit: "$"
|
|
59
|
+
# These five are to override number.format and are optional
|
|
60
|
+
separator: "."
|
|
61
|
+
delimiter: ","
|
|
62
|
+
precision: 2
|
|
63
|
+
significant: false
|
|
64
|
+
strip_insignificant_zeros: false
|
|
65
|
+
|
|
66
|
+
# Used in NumberHelper.number_to_percentage()
|
|
67
|
+
percentage:
|
|
68
|
+
format:
|
|
69
|
+
# These five are to override number.format and are optional
|
|
70
|
+
# separator:
|
|
71
|
+
delimiter: ""
|
|
72
|
+
# precision:
|
|
73
|
+
# significant: false
|
|
74
|
+
# strip_insignificant_zeros: false
|
|
75
|
+
format: "%n%"
|
|
76
|
+
|
|
77
|
+
# Used in NumberHelper.number_to_rounded()
|
|
78
|
+
precision:
|
|
79
|
+
format:
|
|
80
|
+
# These five are to override number.format and are optional
|
|
81
|
+
# separator:
|
|
82
|
+
delimiter: ""
|
|
83
|
+
# precision:
|
|
84
|
+
# significant: false
|
|
85
|
+
# strip_insignificant_zeros: false
|
|
86
|
+
|
|
87
|
+
# Used in NumberHelper.number_to_human_size() and NumberHelper.number_to_human()
|
|
88
|
+
human:
|
|
89
|
+
format:
|
|
90
|
+
# These five are to override number.format and are optional
|
|
91
|
+
# separator:
|
|
92
|
+
delimiter: ""
|
|
93
|
+
precision: 3
|
|
94
|
+
significant: true
|
|
95
|
+
strip_insignificant_zeros: true
|
|
96
|
+
# Used in number_to_human_size()
|
|
97
|
+
storage_units:
|
|
98
|
+
# Storage units output formatting.
|
|
99
|
+
# %u is the storage unit, %n is the number (default: 2 MB)
|
|
100
|
+
format: "%n %u"
|
|
101
|
+
units:
|
|
102
|
+
byte:
|
|
103
|
+
one: "Byte"
|
|
104
|
+
other: "Bytes"
|
|
105
|
+
kb: "KB"
|
|
106
|
+
mb: "MB"
|
|
107
|
+
gb: "GB"
|
|
108
|
+
tb: "TB"
|
|
109
|
+
pb: "PB"
|
|
110
|
+
eb: "EB"
|
|
111
|
+
# Used in NumberHelper.number_to_human()
|
|
112
|
+
decimal_units:
|
|
113
|
+
format: "%n %u"
|
|
114
|
+
# Decimal units output formatting
|
|
115
|
+
# By default we will only quantify some of the exponents
|
|
116
|
+
# but the commented ones might be defined or overridden
|
|
117
|
+
# by the user.
|
|
118
|
+
units:
|
|
119
|
+
# femto: Quadrillionth
|
|
120
|
+
# pico: Trillionth
|
|
121
|
+
# nano: Billionth
|
|
122
|
+
# micro: Millionth
|
|
123
|
+
# mili: Thousandth
|
|
124
|
+
# centi: Hundredth
|
|
125
|
+
# deci: Tenth
|
|
126
|
+
unit: ""
|
|
127
|
+
# ten:
|
|
128
|
+
# one: Ten
|
|
129
|
+
# other: Tens
|
|
130
|
+
# hundred: Hundred
|
|
131
|
+
thousand: Thousand
|
|
132
|
+
million: Million
|
|
133
|
+
billion: Billion
|
|
134
|
+
trillion: Trillion
|
|
135
|
+
quadrillion: Quadrillion
|