omg-activesupport 8.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +86 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +40 -0
- data/lib/active_support/actionable_error.rb +50 -0
- data/lib/active_support/all.rb +5 -0
- data/lib/active_support/array_inquirer.rb +50 -0
- data/lib/active_support/backtrace_cleaner.rb +163 -0
- data/lib/active_support/benchmark.rb +21 -0
- data/lib/active_support/benchmarkable.rb +53 -0
- data/lib/active_support/broadcast_logger.rb +251 -0
- data/lib/active_support/builder.rb +8 -0
- data/lib/active_support/cache/coder.rb +153 -0
- data/lib/active_support/cache/entry.rb +134 -0
- data/lib/active_support/cache/file_store.rb +244 -0
- data/lib/active_support/cache/mem_cache_store.rb +290 -0
- data/lib/active_support/cache/memory_store.rb +262 -0
- data/lib/active_support/cache/null_store.rb +62 -0
- data/lib/active_support/cache/redis_cache_store.rb +492 -0
- data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
- data/lib/active_support/cache/strategy/local_cache.rb +201 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
- data/lib/active_support/cache.rb +1104 -0
- data/lib/active_support/callbacks.rb +944 -0
- data/lib/active_support/class_attribute.rb +26 -0
- data/lib/active_support/code_generator.rb +79 -0
- data/lib/active_support/concern.rb +217 -0
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +72 -0
- data/lib/active_support/concurrency/null_lock.rb +13 -0
- data/lib/active_support/concurrency/share_lock.rb +225 -0
- data/lib/active_support/configurable.rb +159 -0
- data/lib/active_support/configuration_file.rb +60 -0
- data/lib/active_support/core_ext/array/access.rb +100 -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/wrap.rb +48 -0
- data/lib/active_support/core_ext/array.rb +9 -0
- data/lib/active_support/core_ext/benchmark.rb +13 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +3 -0
- data/lib/active_support/core_ext/class/attribute.rb +122 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/class/subclasses.rb +24 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/date/acts_like.rb +10 -0
- data/lib/active_support/core_ext/date/blank.rb +18 -0
- data/lib/active_support/core_ext/date/calculations.rb +161 -0
- data/lib/active_support/core_ext/date/conversions.rb +98 -0
- data/lib/active_support/core_ext/date/zones.rb +8 -0
- data/lib/active_support/core_ext/date.rb +7 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +58 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
- data/lib/active_support/core_ext/date_time/blank.rb +18 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +106 -0
- data/lib/active_support/core_ext/date_time.rb +7 -0
- data/lib/active_support/core_ext/digest/uuid.rb +76 -0
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +267 -0
- data/lib/active_support/core_ext/erb/util.rb +201 -0
- data/lib/active_support/core_ext/file/atomic.rb +72 -0
- data/lib/active_support/core_ext/file.rb +3 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +42 -0
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +12 -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 +27 -0
- data/lib/active_support/core_ext/hash.rb +10 -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/integer.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/kernel.rb +5 -0
- data/lib/active_support/core_ext/load_error.rb +9 -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 +49 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
- data/lib/active_support/core_ext/module/concerning.rb +140 -0
- data/lib/active_support/core_ext/module/delegation.rb +225 -0
- data/lib/active_support/core_ext/module/deprecation.rb +25 -0
- data/lib/active_support/core_ext/module/introspection.rb +62 -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/module.rb +13 -0
- data/lib/active_support/core_ext/name_error.rb +59 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +75 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
- data/lib/active_support/core_ext/numeric/time.rb +66 -0
- data/lib/active_support/core_ext/numeric.rb +5 -0
- data/lib/active_support/core_ext/object/acts_like.rb +45 -0
- data/lib/active_support/core_ext/object/blank.rb +199 -0
- data/lib/active_support/core_ext/object/conversions.rb +6 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
- data/lib/active_support/core_ext/object/duplicable.rb +69 -0
- data/lib/active_support/core_ext/object/inclusion.rb +37 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
- data/lib/active_support/core_ext/object/json.rb +260 -0
- data/lib/active_support/core_ext/object/to_param.rb +3 -0
- data/lib/active_support/core_ext/object/to_query.rb +87 -0
- data/lib/active_support/core_ext/object/try.rb +158 -0
- data/lib/active_support/core_ext/object/with.rb +46 -0
- data/lib/active_support/core_ext/object/with_options.rb +101 -0
- data/lib/active_support/core_ext/object.rb +17 -0
- data/lib/active_support/core_ext/pathname/blank.rb +20 -0
- data/lib/active_support/core_ext/pathname/existence.rb +23 -0
- data/lib/active_support/core_ext/pathname.rb +4 -0
- data/lib/active_support/core_ext/range/compare_range.rb +57 -0
- data/lib/active_support/core_ext/range/conversions.rb +62 -0
- data/lib/active_support/core_ext/range/each.rb +24 -0
- data/lib/active_support/core_ext/range/overlap.rb +40 -0
- data/lib/active_support/core_ext/range.rb +6 -0
- data/lib/active_support/core_ext/regexp.rb +14 -0
- data/lib/active_support/core_ext/securerandom.rb +41 -0
- data/lib/active_support/core_ext/string/access.rb +95 -0
- data/lib/active_support/core_ext/string/behavior.rb +8 -0
- data/lib/active_support/core_ext/string/conversions.rb +60 -0
- data/lib/active_support/core_ext/string/exclude.rb +13 -0
- data/lib/active_support/core_ext/string/filters.rb +151 -0
- data/lib/active_support/core_ext/string/indent.rb +45 -0
- data/lib/active_support/core_ext/string/inflections.rb +300 -0
- data/lib/active_support/core_ext/string/inquiry.rb +16 -0
- data/lib/active_support/core_ext/string/multibyte.rb +58 -0
- data/lib/active_support/core_ext/string/output_safety.rb +228 -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/string.rb +15 -0
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
- data/lib/active_support/core_ext/time/acts_like.rb +10 -0
- data/lib/active_support/core_ext/time/calculations.rb +386 -0
- data/lib/active_support/core_ext/time/compatibility.rb +32 -0
- data/lib/active_support/core_ext/time/conversions.rb +75 -0
- data/lib/active_support/core_ext/time/zones.rb +97 -0
- data/lib/active_support/core_ext/time.rb +7 -0
- data/lib/active_support/core_ext.rb +5 -0
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +233 -0
- data/lib/active_support/deep_mergeable.rb +53 -0
- data/lib/active_support/delegation.rb +202 -0
- data/lib/active_support/dependencies/autoload.rb +72 -0
- data/lib/active_support/dependencies/interlock.rb +49 -0
- data/lib/active_support/dependencies/require_dependency.rb +28 -0
- data/lib/active_support/dependencies.rb +98 -0
- data/lib/active_support/deprecation/behaviors.rb +148 -0
- data/lib/active_support/deprecation/constant_accessor.rb +74 -0
- data/lib/active_support/deprecation/deprecators.rb +104 -0
- data/lib/active_support/deprecation/disallowed.rb +54 -0
- data/lib/active_support/deprecation/method_wrappers.rb +68 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
- data/lib/active_support/deprecation/reporting.rb +179 -0
- data/lib/active_support/deprecation.rb +81 -0
- data/lib/active_support/deprecator.rb +7 -0
- data/lib/active_support/descendants_tracker.rb +112 -0
- data/lib/active_support/digest.rb +22 -0
- data/lib/active_support/duration/iso8601_parser.rb +123 -0
- data/lib/active_support/duration/iso8601_serializer.rb +64 -0
- data/lib/active_support/duration.rb +520 -0
- data/lib/active_support/encrypted_configuration.rb +126 -0
- data/lib/active_support/encrypted_file.rb +133 -0
- data/lib/active_support/environment_inquirer.rb +40 -0
- data/lib/active_support/error_reporter/test_helper.rb +15 -0
- data/lib/active_support/error_reporter.rb +265 -0
- data/lib/active_support/evented_file_update_checker.rb +182 -0
- data/lib/active_support/execution_context/test_helper.rb +13 -0
- data/lib/active_support/execution_context.rb +53 -0
- data/lib/active_support/execution_wrapper.rb +150 -0
- data/lib/active_support/executor/test_helper.rb +7 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +164 -0
- data/lib/active_support/fork_tracker.rb +43 -0
- data/lib/active_support/gem_version.rb +17 -0
- data/lib/active_support/gzip.rb +40 -0
- data/lib/active_support/hash_with_indifferent_access.rb +445 -0
- data/lib/active_support/html_safe_translation.rb +56 -0
- data/lib/active_support/i18n.rb +17 -0
- data/lib/active_support/i18n_railtie.rb +138 -0
- data/lib/active_support/inflections.rb +72 -0
- data/lib/active_support/inflector/inflections.rb +273 -0
- data/lib/active_support/inflector/methods.rb +387 -0
- data/lib/active_support/inflector/transliterate.rb +149 -0
- data/lib/active_support/inflector.rb +9 -0
- data/lib/active_support/isolated_execution_state.rb +75 -0
- data/lib/active_support/json/decoding.rb +76 -0
- data/lib/active_support/json/encoding.rb +120 -0
- data/lib/active_support/json.rb +4 -0
- data/lib/active_support/key_generator.rb +66 -0
- data/lib/active_support/lazy_load_hooks.rb +107 -0
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +141 -0
- data/lib/active_support/log_subscriber/test_helper.rb +106 -0
- data/lib/active_support/log_subscriber.rb +192 -0
- data/lib/active_support/logger.rb +55 -0
- data/lib/active_support/logger_silence.rb +21 -0
- data/lib/active_support/logger_thread_safe_level.rb +47 -0
- data/lib/active_support/message_encryptor.rb +374 -0
- data/lib/active_support/message_encryptors.rb +141 -0
- data/lib/active_support/message_pack/cache_serializer.rb +23 -0
- data/lib/active_support/message_pack/extensions.rb +305 -0
- data/lib/active_support/message_pack/serializer.rb +63 -0
- data/lib/active_support/message_pack.rb +50 -0
- data/lib/active_support/message_verifier.rb +368 -0
- data/lib/active_support/message_verifiers.rb +135 -0
- data/lib/active_support/messages/codec.rb +65 -0
- data/lib/active_support/messages/metadata.rb +146 -0
- data/lib/active_support/messages/rotation_configuration.rb +23 -0
- data/lib/active_support/messages/rotation_coordinator.rb +93 -0
- data/lib/active_support/messages/rotator.rb +59 -0
- data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
- data/lib/active_support/multibyte/chars.rb +178 -0
- data/lib/active_support/multibyte/unicode.rb +42 -0
- data/lib/active_support/multibyte.rb +23 -0
- data/lib/active_support/notifications/fanout.rb +446 -0
- data/lib/active_support/notifications/instrumenter.rb +240 -0
- data/lib/active_support/notifications.rb +281 -0
- data/lib/active_support/number_helper/number_converter.rb +190 -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 +30 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -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 +59 -0
- data/lib/active_support/number_helper/rounding_helper.rb +46 -0
- data/lib/active_support/number_helper.rb +479 -0
- data/lib/active_support/option_merger.rb +38 -0
- data/lib/active_support/ordered_hash.rb +50 -0
- data/lib/active_support/ordered_options.rb +147 -0
- data/lib/active_support/parameter_filter.rb +157 -0
- data/lib/active_support/proxy_object.rb +20 -0
- data/lib/active_support/rails.rb +26 -0
- data/lib/active_support/railtie.rb +161 -0
- data/lib/active_support/reloader.rb +138 -0
- data/lib/active_support/rescuable.rb +176 -0
- data/lib/active_support/secure_compare_rotator.rb +58 -0
- data/lib/active_support/security_utils.rb +38 -0
- data/lib/active_support/string_inquirer.rb +35 -0
- data/lib/active_support/subscriber.rb +146 -0
- data/lib/active_support/syntax_error_proxy.rb +60 -0
- data/lib/active_support/tagged_logging.rb +152 -0
- data/lib/active_support/test_case.rb +304 -0
- data/lib/active_support/testing/assertions.rb +332 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +51 -0
- data/lib/active_support/testing/constant_stubbing.rb +54 -0
- data/lib/active_support/testing/declarative.rb +28 -0
- data/lib/active_support/testing/deprecation.rb +82 -0
- data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +121 -0
- data/lib/active_support/testing/method_call_assertions.rb +69 -0
- data/lib/active_support/testing/parallelization/server.rb +85 -0
- data/lib/active_support/testing/parallelization/worker.rb +103 -0
- data/lib/active_support/testing/parallelization.rb +55 -0
- data/lib/active_support/testing/parallelize_executor.rb +81 -0
- data/lib/active_support/testing/setup_and_teardown.rb +57 -0
- data/lib/active_support/testing/stream.rb +41 -0
- data/lib/active_support/testing/strict_warnings.rb +43 -0
- data/lib/active_support/testing/tagged_logging.rb +27 -0
- data/lib/active_support/testing/tests_without_assertions.rb +19 -0
- data/lib/active_support/testing/time_helpers.rb +269 -0
- data/lib/active_support/time.rb +20 -0
- data/lib/active_support/time_with_zone.rb +609 -0
- data/lib/active_support/values/time_zone.rb +614 -0
- data/lib/active_support/version.rb +10 -0
- data/lib/active_support/xml_mini/jdom.rb +175 -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 +137 -0
- data/lib/active_support/xml_mini.rb +211 -0
- data/lib/active_support.rb +144 -0
- metadata +526 -0
@@ -0,0 +1,260 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Hack to load JSON gem first so we can override its to_json.
|
4
|
+
require "json"
|
5
|
+
require "bigdecimal"
|
6
|
+
require "ipaddr"
|
7
|
+
require "uri/generic"
|
8
|
+
require "pathname"
|
9
|
+
require "active_support/core_ext/big_decimal/conversions" # for #to_s
|
10
|
+
require "active_support/core_ext/hash/except"
|
11
|
+
require "active_support/core_ext/hash/slice"
|
12
|
+
require "active_support/core_ext/object/instance_variables"
|
13
|
+
require "time"
|
14
|
+
require "active_support/core_ext/time/conversions"
|
15
|
+
require "active_support/core_ext/date_time/conversions"
|
16
|
+
require "active_support/core_ext/date/conversions"
|
17
|
+
|
18
|
+
#--
|
19
|
+
# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
|
20
|
+
# their default behavior. That said, we need to define the basic to_json method in all of them,
|
21
|
+
# otherwise they will always use to_json gem implementation, which is backwards incompatible in
|
22
|
+
# several cases (for instance, the JSON implementation for Hash does not work) with inheritance.
|
23
|
+
#
|
24
|
+
# On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
|
25
|
+
# JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
|
26
|
+
# passes a ::JSON::State object as the only argument to to_json, we can detect that and forward the
|
27
|
+
# calls to the original to_json method.
|
28
|
+
#
|
29
|
+
# It should be noted that when using ::JSON.{generate,dump} directly, ActiveSupport's encoder is
|
30
|
+
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
|
31
|
+
# ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
|
32
|
+
# should give exactly the same results with or without Active Support.
|
33
|
+
|
34
|
+
module ActiveSupport
|
35
|
+
module ToJsonWithActiveSupportEncoder # :nodoc:
|
36
|
+
def to_json(options = nil)
|
37
|
+
if options.is_a?(::JSON::State)
|
38
|
+
# Called from JSON.{generate,dump}, forward it to JSON gem's to_json
|
39
|
+
super(options)
|
40
|
+
else
|
41
|
+
# to_json is being invoked directly, use ActiveSupport's encoder
|
42
|
+
ActiveSupport::JSON.encode(self, options)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
[Enumerable, Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].reverse_each do |klass|
|
49
|
+
klass.include(ActiveSupport::ToJsonWithActiveSupportEncoder)
|
50
|
+
end
|
51
|
+
|
52
|
+
class Module
|
53
|
+
def as_json(options = nil) # :nodoc:
|
54
|
+
name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Object
|
59
|
+
def as_json(options = nil) # :nodoc:
|
60
|
+
if respond_to?(:to_hash)
|
61
|
+
to_hash.as_json(options)
|
62
|
+
else
|
63
|
+
instance_values.as_json(options)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if RUBY_VERSION >= "3.2"
|
69
|
+
class Data # :nodoc:
|
70
|
+
def as_json(options = nil)
|
71
|
+
to_h.as_json(options)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Struct # :nodoc:
|
77
|
+
def as_json(options = nil)
|
78
|
+
to_h.as_json(options)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class TrueClass
|
83
|
+
def as_json(options = nil) # :nodoc:
|
84
|
+
self
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class FalseClass
|
89
|
+
def as_json(options = nil) # :nodoc:
|
90
|
+
self
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class NilClass
|
95
|
+
def as_json(options = nil) # :nodoc:
|
96
|
+
self
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class String
|
101
|
+
def as_json(options = nil) # :nodoc:
|
102
|
+
self
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Symbol
|
107
|
+
def as_json(options = nil) # :nodoc:
|
108
|
+
name
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class Numeric
|
113
|
+
def as_json(options = nil) # :nodoc:
|
114
|
+
self
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Float
|
119
|
+
# Encoding Infinity or NaN to JSON should return "null". The default returns
|
120
|
+
# "Infinity" or "NaN" which are not valid JSON.
|
121
|
+
def as_json(options = nil) # :nodoc:
|
122
|
+
finite? ? self : nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
class BigDecimal
|
127
|
+
# A BigDecimal would be naturally represented as a JSON number. Most libraries,
|
128
|
+
# however, parse non-integer JSON numbers directly as floats. Clients using
|
129
|
+
# those libraries would get in general a wrong number and no way to recover
|
130
|
+
# other than manually inspecting the string with the JSON code itself.
|
131
|
+
#
|
132
|
+
# That's why a JSON string is returned. The JSON literal is not numeric, but
|
133
|
+
# if the other end knows by contract that the data is supposed to be a
|
134
|
+
# BigDecimal, it still has the chance to post-process the string and get the
|
135
|
+
# real value.
|
136
|
+
def as_json(options = nil) # :nodoc:
|
137
|
+
finite? ? to_s : nil
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class Regexp
|
142
|
+
def as_json(options = nil) # :nodoc:
|
143
|
+
to_s
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
module Enumerable
|
148
|
+
def as_json(options = nil) # :nodoc:
|
149
|
+
to_a.as_json(options)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
class IO
|
154
|
+
def as_json(options = nil) # :nodoc:
|
155
|
+
to_s
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
class Range
|
160
|
+
def as_json(options = nil) # :nodoc:
|
161
|
+
to_s
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
class Array
|
166
|
+
def as_json(options = nil) # :nodoc:
|
167
|
+
if options
|
168
|
+
options = options.dup.freeze unless options.frozen?
|
169
|
+
map { |v| v.as_json(options) }
|
170
|
+
else
|
171
|
+
map { |v| v.as_json }
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
class Hash
|
177
|
+
def as_json(options = nil) # :nodoc:
|
178
|
+
# create a subset of the hash by applying :only or :except
|
179
|
+
subset = if options
|
180
|
+
if attrs = options[:only]
|
181
|
+
slice(*Array(attrs))
|
182
|
+
elsif attrs = options[:except]
|
183
|
+
except(*Array(attrs))
|
184
|
+
else
|
185
|
+
self
|
186
|
+
end
|
187
|
+
else
|
188
|
+
self
|
189
|
+
end
|
190
|
+
|
191
|
+
result = {}
|
192
|
+
if options
|
193
|
+
options = options.dup.freeze unless options.frozen?
|
194
|
+
subset.each { |k, v| result[k.to_s] = v.as_json(options) }
|
195
|
+
else
|
196
|
+
subset.each { |k, v| result[k.to_s] = v.as_json }
|
197
|
+
end
|
198
|
+
result
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
class Time
|
203
|
+
def as_json(options = nil) # :nodoc:
|
204
|
+
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
|
205
|
+
xmlschema(ActiveSupport::JSON::Encoding.time_precision)
|
206
|
+
else
|
207
|
+
%(#{strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)})
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
class Date
|
213
|
+
def as_json(options = nil) # :nodoc:
|
214
|
+
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
|
215
|
+
strftime("%Y-%m-%d")
|
216
|
+
else
|
217
|
+
strftime("%Y/%m/%d")
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
class DateTime
|
223
|
+
def as_json(options = nil) # :nodoc:
|
224
|
+
if ActiveSupport::JSON::Encoding.use_standard_json_time_format
|
225
|
+
xmlschema(ActiveSupport::JSON::Encoding.time_precision)
|
226
|
+
else
|
227
|
+
strftime("%Y/%m/%d %H:%M:%S %z")
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
class URI::Generic # :nodoc:
|
233
|
+
def as_json(options = nil)
|
234
|
+
to_s
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
class Pathname # :nodoc:
|
239
|
+
def as_json(options = nil)
|
240
|
+
to_s
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
class IPAddr # :nodoc:
|
245
|
+
def as_json(options = nil)
|
246
|
+
to_s
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
class Process::Status # :nodoc:
|
251
|
+
def as_json(options = nil)
|
252
|
+
{ exitstatus: exitstatus, pid: pid }
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
class Exception
|
257
|
+
def as_json(options = nil)
|
258
|
+
to_s
|
259
|
+
end
|
260
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "cgi"
|
4
|
+
|
5
|
+
class Object
|
6
|
+
# Alias of <tt>to_s</tt>.
|
7
|
+
def to_param
|
8
|
+
to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
# Converts an object into a string suitable for use as a URL query string,
|
12
|
+
# using the given <tt>key</tt> as the param name.
|
13
|
+
def to_query(key)
|
14
|
+
"#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class NilClass
|
19
|
+
# Returns +self+.
|
20
|
+
def to_param
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class TrueClass
|
26
|
+
# Returns +self+.
|
27
|
+
def to_param
|
28
|
+
self
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class FalseClass
|
33
|
+
# Returns +self+.
|
34
|
+
def to_param
|
35
|
+
self
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Array
|
40
|
+
# Calls <tt>to_param</tt> on all its elements and joins the result with
|
41
|
+
# slashes. This is used by <tt>url_for</tt> in Action Pack.
|
42
|
+
def to_param
|
43
|
+
collect(&:to_param).join "/"
|
44
|
+
end
|
45
|
+
|
46
|
+
# Converts an array into a string suitable for use as a URL query string,
|
47
|
+
# using the given +key+ as the param name.
|
48
|
+
#
|
49
|
+
# ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
|
50
|
+
def to_query(key)
|
51
|
+
prefix = "#{key}[]"
|
52
|
+
|
53
|
+
if empty?
|
54
|
+
nil.to_query(prefix)
|
55
|
+
else
|
56
|
+
collect { |value| value.to_query(prefix) }.join "&"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class Hash
|
62
|
+
# Returns a string representation of the receiver suitable for use as a URL
|
63
|
+
# query string:
|
64
|
+
#
|
65
|
+
# {name: 'David', nationality: 'Danish'}.to_query
|
66
|
+
# # => "name=David&nationality=Danish"
|
67
|
+
#
|
68
|
+
# An optional namespace can be passed to enclose key names:
|
69
|
+
#
|
70
|
+
# {name: 'David', nationality: 'Danish'}.to_query('user')
|
71
|
+
# # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
|
72
|
+
#
|
73
|
+
# The string pairs "key=value" that conform the query string
|
74
|
+
# are sorted lexicographically in ascending order.
|
75
|
+
def to_query(namespace = nil)
|
76
|
+
query = filter_map do |key, value|
|
77
|
+
unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
|
78
|
+
value.to_query(namespace ? "#{namespace}[#{key}]" : key)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
query.sort! unless namespace.to_s.include?("[]")
|
83
|
+
query.join("&")
|
84
|
+
end
|
85
|
+
|
86
|
+
alias_method :to_param, :to_query
|
87
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "delegate"
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
module Tryable # :nodoc:
|
7
|
+
def try(*args, &block)
|
8
|
+
if args.empty? && block_given?
|
9
|
+
if block.arity == 0
|
10
|
+
instance_eval(&block)
|
11
|
+
else
|
12
|
+
yield self
|
13
|
+
end
|
14
|
+
elsif respond_to?(args.first)
|
15
|
+
public_send(*args, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
ruby2_keywords(:try)
|
19
|
+
|
20
|
+
def try!(*args, &block)
|
21
|
+
if args.empty? && block_given?
|
22
|
+
if block.arity == 0
|
23
|
+
instance_eval(&block)
|
24
|
+
else
|
25
|
+
yield self
|
26
|
+
end
|
27
|
+
else
|
28
|
+
public_send(*args, &block)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
ruby2_keywords(:try!)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Object
|
36
|
+
include ActiveSupport::Tryable
|
37
|
+
|
38
|
+
##
|
39
|
+
# :method: try
|
40
|
+
#
|
41
|
+
# :call-seq:
|
42
|
+
# try(*args, &block)
|
43
|
+
#
|
44
|
+
# Invokes the public method whose name goes as first argument just like
|
45
|
+
# +public_send+ does, except that if the receiver does not respond to it the
|
46
|
+
# call returns +nil+ rather than raising an exception.
|
47
|
+
#
|
48
|
+
# This method is defined to be able to write
|
49
|
+
#
|
50
|
+
# @person.try(:name)
|
51
|
+
#
|
52
|
+
# instead of
|
53
|
+
#
|
54
|
+
# @person.name if @person
|
55
|
+
#
|
56
|
+
# +try+ calls can be chained:
|
57
|
+
#
|
58
|
+
# @person.try(:spouse).try(:name)
|
59
|
+
#
|
60
|
+
# instead of
|
61
|
+
#
|
62
|
+
# @person.spouse.name if @person && @person.spouse
|
63
|
+
#
|
64
|
+
# +try+ will also return +nil+ if the receiver does not respond to the method:
|
65
|
+
#
|
66
|
+
# @person.try(:non_existing_method) # => nil
|
67
|
+
#
|
68
|
+
# instead of
|
69
|
+
#
|
70
|
+
# @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
|
71
|
+
#
|
72
|
+
# +try+ returns +nil+ when called on +nil+ regardless of whether it responds
|
73
|
+
# to the method:
|
74
|
+
#
|
75
|
+
# nil.try(:to_i) # => nil, rather than 0
|
76
|
+
#
|
77
|
+
# Arguments and blocks are forwarded to the method if invoked:
|
78
|
+
#
|
79
|
+
# @posts.try(:each_slice, 2) do |a, b|
|
80
|
+
# ...
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# The number of arguments in the signature must match. If the object responds
|
84
|
+
# to the method the call is attempted and +ArgumentError+ is still raised
|
85
|
+
# in case of argument mismatch.
|
86
|
+
#
|
87
|
+
# If +try+ is called without arguments it yields the receiver to a given
|
88
|
+
# block unless it is +nil+:
|
89
|
+
#
|
90
|
+
# @person.try do |p|
|
91
|
+
# ...
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# You can also call try with a block without accepting an argument, and the block
|
95
|
+
# will be instance_eval'ed instead:
|
96
|
+
#
|
97
|
+
# @person.try { upcase.truncate(50) }
|
98
|
+
#
|
99
|
+
# Please also note that +try+ is defined on +Object+. Therefore, it won't work
|
100
|
+
# with instances of classes that do not have +Object+ among their ancestors,
|
101
|
+
# like direct subclasses of +BasicObject+.
|
102
|
+
|
103
|
+
##
|
104
|
+
# :method: try!
|
105
|
+
#
|
106
|
+
# :call-seq:
|
107
|
+
# try!(*args, &block)
|
108
|
+
#
|
109
|
+
# Same as #try, but raises a +NoMethodError+ exception if the receiver is
|
110
|
+
# not +nil+ and does not implement the tried method.
|
111
|
+
#
|
112
|
+
# "a".try!(:upcase) # => "A"
|
113
|
+
# nil.try!(:upcase) # => nil
|
114
|
+
# 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer
|
115
|
+
end
|
116
|
+
|
117
|
+
class Delegator
|
118
|
+
include ActiveSupport::Tryable
|
119
|
+
|
120
|
+
##
|
121
|
+
# :method: try
|
122
|
+
#
|
123
|
+
# :call-seq:
|
124
|
+
# try(*args, &block)
|
125
|
+
#
|
126
|
+
# See Object#try
|
127
|
+
|
128
|
+
##
|
129
|
+
# :method: try!
|
130
|
+
#
|
131
|
+
# :call-seq:
|
132
|
+
# try!(*args, &block)
|
133
|
+
#
|
134
|
+
# See Object#try!
|
135
|
+
end
|
136
|
+
|
137
|
+
class NilClass
|
138
|
+
# Calling +try+ on +nil+ always returns +nil+.
|
139
|
+
# It becomes especially helpful when navigating through associations that may return +nil+.
|
140
|
+
#
|
141
|
+
# nil.try(:name) # => nil
|
142
|
+
#
|
143
|
+
# Without +try+
|
144
|
+
# @person && @person.children.any? && @person.children.first.name
|
145
|
+
#
|
146
|
+
# With +try+
|
147
|
+
# @person.try(:children).try(:first).try(:name)
|
148
|
+
def try(*)
|
149
|
+
nil
|
150
|
+
end
|
151
|
+
|
152
|
+
# Calling +try!+ on +nil+ always returns +nil+.
|
153
|
+
#
|
154
|
+
# nil.try!(:name) # => nil
|
155
|
+
def try!(*)
|
156
|
+
nil
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Object
|
4
|
+
# Set and restore public attributes around a block.
|
5
|
+
#
|
6
|
+
# client.timeout # => 5
|
7
|
+
# client.with(timeout: 1) do |c|
|
8
|
+
# c.timeout # => 1
|
9
|
+
# end
|
10
|
+
# client.timeout # => 5
|
11
|
+
#
|
12
|
+
# The receiver is yielded to the provided block.
|
13
|
+
#
|
14
|
+
# This method is a shorthand for the common begin/ensure pattern:
|
15
|
+
#
|
16
|
+
# old_value = object.attribute
|
17
|
+
# begin
|
18
|
+
# object.attribute = new_value
|
19
|
+
# # do things
|
20
|
+
# ensure
|
21
|
+
# object.attribute = old_value
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# It can be used on any object as long as both the reader and writer methods
|
25
|
+
# are public.
|
26
|
+
def with(**attributes)
|
27
|
+
old_values = {}
|
28
|
+
begin
|
29
|
+
attributes.each do |key, value|
|
30
|
+
old_values[key] = public_send(key)
|
31
|
+
public_send("#{key}=", value)
|
32
|
+
end
|
33
|
+
yield self
|
34
|
+
ensure
|
35
|
+
old_values.each do |key, old_value|
|
36
|
+
public_send("#{key}=", old_value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# #with isn't usable on immediates, so we might as well undefine the
|
43
|
+
# method in common immediate classes to avoid potential confusion.
|
44
|
+
[NilClass, TrueClass, FalseClass, Integer, Float, Symbol].each do |klass|
|
45
|
+
klass.undef_method(:with)
|
46
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/option_merger"
|
4
|
+
|
5
|
+
class Object
|
6
|
+
# An elegant way to factor duplication out of options passed to a series of
|
7
|
+
# method calls. Each method called in the block, with the block variable as
|
8
|
+
# the receiver, will have its options merged with the default +options+
|
9
|
+
# <tt>Hash</tt> or <tt>Hash</tt>-like object provided. Each method called on
|
10
|
+
# the block variable must take an options hash as its final argument.
|
11
|
+
#
|
12
|
+
# Without <tt>with_options</tt>, this code contains duplication:
|
13
|
+
#
|
14
|
+
# class Account < ActiveRecord::Base
|
15
|
+
# has_many :customers, dependent: :destroy
|
16
|
+
# has_many :products, dependent: :destroy
|
17
|
+
# has_many :invoices, dependent: :destroy
|
18
|
+
# has_many :expenses, dependent: :destroy
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# Using <tt>with_options</tt>, we can remove the duplication:
|
22
|
+
#
|
23
|
+
# class Account < ActiveRecord::Base
|
24
|
+
# with_options dependent: :destroy do |assoc|
|
25
|
+
# assoc.has_many :customers
|
26
|
+
# assoc.has_many :products
|
27
|
+
# assoc.has_many :invoices
|
28
|
+
# assoc.has_many :expenses
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# It can also be used with an explicit receiver:
|
33
|
+
#
|
34
|
+
# I18n.with_options locale: user.locale, scope: 'newsletter' do |i18n|
|
35
|
+
# subject i18n.t :subject
|
36
|
+
# body i18n.t :body, user_name: user.name
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# When you don't pass an explicit receiver, it executes the whole block
|
40
|
+
# in merging options context:
|
41
|
+
#
|
42
|
+
# class Account < ActiveRecord::Base
|
43
|
+
# with_options dependent: :destroy do
|
44
|
+
# has_many :customers
|
45
|
+
# has_many :products
|
46
|
+
# has_many :invoices
|
47
|
+
# has_many :expenses
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# <tt>with_options</tt> can also be nested since the call is forwarded to its receiver.
|
52
|
+
#
|
53
|
+
# NOTE: Each nesting level will merge inherited defaults in addition to their own.
|
54
|
+
#
|
55
|
+
# class Post < ActiveRecord::Base
|
56
|
+
# with_options if: :persisted?, length: { minimum: 50 } do
|
57
|
+
# validates :content, if: -> { content.present? }
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# The code is equivalent to:
|
62
|
+
#
|
63
|
+
# validates :content, length: { minimum: 50 }, if: -> { content.present? }
|
64
|
+
#
|
65
|
+
# Hence the inherited default for +if+ key is ignored.
|
66
|
+
#
|
67
|
+
# NOTE: You cannot call class methods implicitly inside of +with_options+.
|
68
|
+
# You can access these methods using the class name instead:
|
69
|
+
#
|
70
|
+
# class Phone < ActiveRecord::Base
|
71
|
+
# enum :phone_number_type, { home: 0, office: 1, mobile: 2 }
|
72
|
+
#
|
73
|
+
# with_options presence: true do
|
74
|
+
# validates :phone_number_type, inclusion: { in: Phone.phone_number_types.keys }
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# When the block argument is omitted, the decorated Object instance is returned:
|
79
|
+
#
|
80
|
+
# module MyStyledHelpers
|
81
|
+
# def styled
|
82
|
+
# with_options style: "color: red;"
|
83
|
+
# end
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# styled.link_to "I'm red", "/"
|
87
|
+
# # => <a href="/" style="color: red;">I'm red</a>
|
88
|
+
#
|
89
|
+
# styled.button_tag "I'm red too!"
|
90
|
+
# # => <button style="color: red;">I'm red too!</button>
|
91
|
+
#
|
92
|
+
def with_options(options, &block)
|
93
|
+
option_merger = ActiveSupport::OptionMerger.new(self, options)
|
94
|
+
|
95
|
+
if block
|
96
|
+
block.arity.zero? ? option_merger.instance_eval(&block) : block.call(option_merger)
|
97
|
+
else
|
98
|
+
option_merger
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/object/acts_like"
|
4
|
+
require "active_support/core_ext/object/blank"
|
5
|
+
require "active_support/core_ext/object/duplicable"
|
6
|
+
require "active_support/core_ext/object/deep_dup"
|
7
|
+
require "active_support/core_ext/object/try"
|
8
|
+
require "active_support/core_ext/object/inclusion"
|
9
|
+
|
10
|
+
require "active_support/core_ext/object/conversions"
|
11
|
+
require "active_support/core_ext/object/instance_variables"
|
12
|
+
|
13
|
+
require "active_support/core_ext/object/json"
|
14
|
+
require "active_support/core_ext/object/to_param"
|
15
|
+
require "active_support/core_ext/object/to_query"
|
16
|
+
require "active_support/core_ext/object/with"
|
17
|
+
require "active_support/core_ext/object/with_options"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pathname"
|
4
|
+
|
5
|
+
class Pathname
|
6
|
+
# An Pathname is blank if it's empty:
|
7
|
+
#
|
8
|
+
# Pathname.new("").blank? # => true
|
9
|
+
# Pathname.new(" ").blank? # => false
|
10
|
+
# Pathname.new("test").blank? # => false
|
11
|
+
#
|
12
|
+
# @return [true, false]
|
13
|
+
def blank?
|
14
|
+
to_s.empty?
|
15
|
+
end
|
16
|
+
|
17
|
+
def present? # :nodoc:
|
18
|
+
!to_s.empty?
|
19
|
+
end
|
20
|
+
end
|