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,233 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/callbacks"
|
4
|
+
require "active_support/core_ext/object/with"
|
5
|
+
require "active_support/core_ext/enumerable"
|
6
|
+
require "active_support/core_ext/module/delegation"
|
7
|
+
|
8
|
+
module ActiveSupport
|
9
|
+
# = Current Attributes
|
10
|
+
#
|
11
|
+
# Abstract super class that provides a thread-isolated attributes singleton, which resets automatically
|
12
|
+
# before and after each request. This allows you to keep all the per-request attributes easily
|
13
|
+
# available to the whole system.
|
14
|
+
#
|
15
|
+
# The following full app-like example demonstrates how to use a Current class to
|
16
|
+
# facilitate easy access to the global, per-request attributes without passing them deeply
|
17
|
+
# around everywhere:
|
18
|
+
#
|
19
|
+
# # app/models/current.rb
|
20
|
+
# class Current < ActiveSupport::CurrentAttributes
|
21
|
+
# attribute :account, :user
|
22
|
+
# attribute :request_id, :user_agent, :ip_address
|
23
|
+
#
|
24
|
+
# resets { Time.zone = nil }
|
25
|
+
#
|
26
|
+
# def user=(user)
|
27
|
+
# super
|
28
|
+
# self.account = user.account
|
29
|
+
# Time.zone = user.time_zone
|
30
|
+
# end
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# # app/controllers/concerns/authentication.rb
|
34
|
+
# module Authentication
|
35
|
+
# extend ActiveSupport::Concern
|
36
|
+
#
|
37
|
+
# included do
|
38
|
+
# before_action :authenticate
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# private
|
42
|
+
# def authenticate
|
43
|
+
# if authenticated_user = User.find_by(id: cookies.encrypted[:user_id])
|
44
|
+
# Current.user = authenticated_user
|
45
|
+
# else
|
46
|
+
# redirect_to new_session_url
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# # app/controllers/concerns/set_current_request_details.rb
|
52
|
+
# module SetCurrentRequestDetails
|
53
|
+
# extend ActiveSupport::Concern
|
54
|
+
#
|
55
|
+
# included do
|
56
|
+
# before_action do
|
57
|
+
# Current.request_id = request.uuid
|
58
|
+
# Current.user_agent = request.user_agent
|
59
|
+
# Current.ip_address = request.ip
|
60
|
+
# end
|
61
|
+
# end
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# class ApplicationController < ActionController::Base
|
65
|
+
# include Authentication
|
66
|
+
# include SetCurrentRequestDetails
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# class MessagesController < ApplicationController
|
70
|
+
# def create
|
71
|
+
# Current.account.messages.create(message_params)
|
72
|
+
# end
|
73
|
+
# end
|
74
|
+
#
|
75
|
+
# class Message < ApplicationRecord
|
76
|
+
# belongs_to :creator, default: -> { Current.user }
|
77
|
+
# after_create { |message| Event.create(record: message) }
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# class Event < ApplicationRecord
|
81
|
+
# before_create do
|
82
|
+
# self.request_id = Current.request_id
|
83
|
+
# self.user_agent = Current.user_agent
|
84
|
+
# self.ip_address = Current.ip_address
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# A word of caution: It's easy to overdo a global singleton like Current and tangle your model as a result.
|
89
|
+
# Current should only be used for a few, top-level globals, like account, user, and request details.
|
90
|
+
# The attributes stuck in Current should be used by more or less all actions on all requests. If you start
|
91
|
+
# sticking controller-specific attributes in there, you're going to create a mess.
|
92
|
+
class CurrentAttributes
|
93
|
+
include ActiveSupport::Callbacks
|
94
|
+
define_callbacks :reset
|
95
|
+
|
96
|
+
INVALID_ATTRIBUTE_NAMES = [:set, :reset, :resets, :instance, :before_reset, :after_reset, :reset_all, :clear_all] # :nodoc:
|
97
|
+
|
98
|
+
NOT_SET = Object.new.freeze # :nodoc:
|
99
|
+
|
100
|
+
class << self
|
101
|
+
# Returns singleton instance for this class in this thread. If none exists, one is created.
|
102
|
+
def instance
|
103
|
+
current_instances[current_instances_key] ||= new
|
104
|
+
end
|
105
|
+
|
106
|
+
# Declares one or more attributes that will be given both class and instance accessor methods.
|
107
|
+
#
|
108
|
+
# ==== Options
|
109
|
+
#
|
110
|
+
# * <tt>:default</tt> - The default value for the attributes. If the value
|
111
|
+
# is a proc or lambda, it will be called whenever an instance is
|
112
|
+
# constructed. Otherwise, the value will be duplicated with +#dup+.
|
113
|
+
# Default values are re-assigned when the attributes are reset.
|
114
|
+
def attribute(*names, default: NOT_SET)
|
115
|
+
invalid_attribute_names = names.map(&:to_sym) & INVALID_ATTRIBUTE_NAMES
|
116
|
+
if invalid_attribute_names.any?
|
117
|
+
raise ArgumentError, "Restricted attribute names: #{invalid_attribute_names.join(", ")}"
|
118
|
+
end
|
119
|
+
|
120
|
+
ActiveSupport::CodeGenerator.batch(generated_attribute_methods, __FILE__, __LINE__) do |owner|
|
121
|
+
names.each do |name|
|
122
|
+
owner.define_cached_method(name, namespace: :current_attributes) do |batch|
|
123
|
+
batch <<
|
124
|
+
"def #{name}" <<
|
125
|
+
"attributes[:#{name}]" <<
|
126
|
+
"end"
|
127
|
+
end
|
128
|
+
owner.define_cached_method("#{name}=", namespace: :current_attributes) do |batch|
|
129
|
+
batch <<
|
130
|
+
"def #{name}=(value)" <<
|
131
|
+
"attributes[:#{name}] = value" <<
|
132
|
+
"end"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
Delegation.generate(singleton_class, names, to: :instance, nilable: false, signature: "")
|
138
|
+
Delegation.generate(singleton_class, names.map { |n| "#{n}=" }, to: :instance, nilable: false, signature: "value")
|
139
|
+
|
140
|
+
self.defaults = defaults.merge(names.index_with { default })
|
141
|
+
end
|
142
|
+
|
143
|
+
# Calls this callback before #reset is called on the instance. Used for resetting external collaborators that depend on current values.
|
144
|
+
def before_reset(*methods, &block)
|
145
|
+
set_callback :reset, :before, *methods, &block
|
146
|
+
end
|
147
|
+
|
148
|
+
# Calls this callback after #reset is called on the instance. Used for resetting external collaborators, like Time.zone.
|
149
|
+
def resets(*methods, &block)
|
150
|
+
set_callback :reset, :after, *methods, &block
|
151
|
+
end
|
152
|
+
alias_method :after_reset, :resets
|
153
|
+
|
154
|
+
delegate :set, :reset, to: :instance
|
155
|
+
|
156
|
+
def reset_all # :nodoc:
|
157
|
+
current_instances.each_value(&:reset)
|
158
|
+
end
|
159
|
+
|
160
|
+
def clear_all # :nodoc:
|
161
|
+
reset_all
|
162
|
+
current_instances.clear
|
163
|
+
end
|
164
|
+
|
165
|
+
private
|
166
|
+
def generated_attribute_methods
|
167
|
+
@generated_attribute_methods ||= Module.new.tap { |mod| include mod }
|
168
|
+
end
|
169
|
+
|
170
|
+
def current_instances
|
171
|
+
IsolatedExecutionState[:current_attributes_instances] ||= {}
|
172
|
+
end
|
173
|
+
|
174
|
+
def current_instances_key
|
175
|
+
@current_instances_key ||= name.to_sym
|
176
|
+
end
|
177
|
+
|
178
|
+
def method_missing(name, ...)
|
179
|
+
instance.public_send(name, ...)
|
180
|
+
end
|
181
|
+
|
182
|
+
def respond_to_missing?(name, _)
|
183
|
+
instance.respond_to?(name) || super
|
184
|
+
end
|
185
|
+
|
186
|
+
def method_added(name)
|
187
|
+
super
|
188
|
+
return if name == :initialize
|
189
|
+
return unless public_method_defined?(name)
|
190
|
+
return if respond_to?(name, true)
|
191
|
+
Delegation.generate(singleton_class, [name], to: :instance, as: self, nilable: false)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
class_attribute :defaults, instance_writer: false, default: {}.freeze
|
196
|
+
|
197
|
+
attr_accessor :attributes
|
198
|
+
|
199
|
+
def initialize
|
200
|
+
@attributes = resolve_defaults
|
201
|
+
end
|
202
|
+
|
203
|
+
# Expose one or more attributes within a block. Old values are returned after the block concludes.
|
204
|
+
# Example demonstrating the common use of needing to set Current attributes outside the request-cycle:
|
205
|
+
#
|
206
|
+
# class Chat::PublicationJob < ApplicationJob
|
207
|
+
# def perform(attributes, room_number, creator)
|
208
|
+
# Current.set(person: creator) do
|
209
|
+
# Chat::Publisher.publish(attributes: attributes, room_number: room_number)
|
210
|
+
# end
|
211
|
+
# end
|
212
|
+
# end
|
213
|
+
def set(attributes, &block)
|
214
|
+
with(**attributes, &block)
|
215
|
+
end
|
216
|
+
|
217
|
+
# Reset all attributes. Should be called before and after actions, when used as a per-request singleton.
|
218
|
+
def reset
|
219
|
+
run_callbacks :reset do
|
220
|
+
self.attributes = resolve_defaults
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
private
|
225
|
+
def resolve_defaults
|
226
|
+
defaults.each_with_object({}) do |(key, value), result|
|
227
|
+
if value != NOT_SET
|
228
|
+
result[key] = Proc === value ? value.call : value.dup
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# Provides +deep_merge+ and +deep_merge!+ methods. Expects the including class
|
5
|
+
# to provide a <tt>merge!(other, &block)</tt> method.
|
6
|
+
module DeepMergeable # :nodoc:
|
7
|
+
# Returns a new instance with the values from +other+ merged recursively.
|
8
|
+
#
|
9
|
+
# class Hash
|
10
|
+
# include ActiveSupport::DeepMergeable
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# hash_1 = { a: true, b: { c: [1, 2, 3] } }
|
14
|
+
# hash_2 = { a: false, b: { x: [3, 4, 5] } }
|
15
|
+
#
|
16
|
+
# hash_1.deep_merge(hash_2)
|
17
|
+
# # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
|
18
|
+
#
|
19
|
+
# A block can be provided to merge non-<tt>DeepMergeable</tt> values:
|
20
|
+
#
|
21
|
+
# hash_1 = { a: 100, b: 200, c: { c1: 100 } }
|
22
|
+
# hash_2 = { b: 250, c: { c1: 200 } }
|
23
|
+
#
|
24
|
+
# hash_1.deep_merge(hash_2) do |key, this_val, other_val|
|
25
|
+
# this_val + other_val
|
26
|
+
# end
|
27
|
+
# # => { a: 100, b: 450, c: { c1: 300 } }
|
28
|
+
#
|
29
|
+
def deep_merge(other, &block)
|
30
|
+
dup.deep_merge!(other, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Same as #deep_merge, but modifies +self+.
|
34
|
+
def deep_merge!(other, &block)
|
35
|
+
merge!(other) do |key, this_val, other_val|
|
36
|
+
if this_val.is_a?(DeepMergeable) && this_val.deep_merge?(other_val)
|
37
|
+
this_val.deep_merge(other_val, &block)
|
38
|
+
elsif block_given?
|
39
|
+
block.call(key, this_val, other_val)
|
40
|
+
else
|
41
|
+
other_val
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns true if +other+ can be deep merged into +self+. Classes may
|
47
|
+
# override this method to restrict or expand the domain of deep mergeable
|
48
|
+
# values. Defaults to checking that +other+ is of type +self.class+.
|
49
|
+
def deep_merge?(other)
|
50
|
+
other.is_a?(self.class)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,202 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "set"
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
# Error generated by +delegate+ when a method is called on +nil+ and +allow_nil+
|
7
|
+
# option is not used.
|
8
|
+
class DelegationError < NoMethodError
|
9
|
+
class << self
|
10
|
+
def nil_target(method_name, target) # :nodoc:
|
11
|
+
new("#{method_name} delegated to #{target}, but #{target} is nil")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Delegation # :nodoc:
|
17
|
+
RUBY_RESERVED_KEYWORDS = %w(__ENCODING__ __LINE__ __FILE__ alias and BEGIN begin break
|
18
|
+
case class def defined? do else elsif END end ensure false for if in module next nil
|
19
|
+
not or redo rescue retry return self super then true undef unless until when while yield)
|
20
|
+
RESERVED_METHOD_NAMES = (RUBY_RESERVED_KEYWORDS + %w(_ arg args block)).to_set.freeze
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def generate(owner, methods, location: nil, to: nil, prefix: nil, allow_nil: nil, nilable: true, private: nil, as: nil, signature: nil)
|
24
|
+
unless to
|
25
|
+
raise ArgumentError, "Delegation needs a target. Supply a keyword argument 'to' (e.g. delegate :hello, to: :greeter)."
|
26
|
+
end
|
27
|
+
|
28
|
+
if prefix == true && /^[^a-z_]/.match?(to)
|
29
|
+
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
|
30
|
+
end
|
31
|
+
|
32
|
+
method_prefix = \
|
33
|
+
if prefix
|
34
|
+
"#{prefix == true ? to : prefix}_"
|
35
|
+
else
|
36
|
+
""
|
37
|
+
end
|
38
|
+
|
39
|
+
location ||= caller_locations(1, 1).first
|
40
|
+
file, line = location.path, location.lineno
|
41
|
+
|
42
|
+
receiver = if to.is_a?(Module)
|
43
|
+
if to.name.nil?
|
44
|
+
raise ArgumentError, "Can't delegate to anonymous class or module: #{to}"
|
45
|
+
end
|
46
|
+
|
47
|
+
unless Inflector.safe_constantize(to.name).equal?(to)
|
48
|
+
raise ArgumentError, "Can't delegate to detached class or module: #{to.name}"
|
49
|
+
end
|
50
|
+
|
51
|
+
"::#{to.name}"
|
52
|
+
else
|
53
|
+
to.to_s
|
54
|
+
end
|
55
|
+
receiver = "self.#{receiver}" if RESERVED_METHOD_NAMES.include?(receiver)
|
56
|
+
|
57
|
+
explicit_receiver = false
|
58
|
+
receiver_class = if as
|
59
|
+
explicit_receiver = true
|
60
|
+
as
|
61
|
+
elsif to.is_a?(Module)
|
62
|
+
to.singleton_class
|
63
|
+
elsif receiver == "self.class"
|
64
|
+
nilable = false # self.class can't possibly be nil
|
65
|
+
owner.singleton_class
|
66
|
+
end
|
67
|
+
|
68
|
+
method_def = []
|
69
|
+
method_names = []
|
70
|
+
|
71
|
+
method_def << "self.private" if private
|
72
|
+
|
73
|
+
methods.each do |method|
|
74
|
+
method_name = prefix ? "#{method_prefix}#{method}" : method
|
75
|
+
method_names << method_name.to_sym
|
76
|
+
|
77
|
+
# Attribute writer methods only accept one argument. Makes sure []=
|
78
|
+
# methods still accept two arguments.
|
79
|
+
definition = \
|
80
|
+
if signature
|
81
|
+
signature
|
82
|
+
elsif /[^\]]=\z/.match?(method)
|
83
|
+
"arg"
|
84
|
+
else
|
85
|
+
method_object = if receiver_class
|
86
|
+
begin
|
87
|
+
receiver_class.public_instance_method(method)
|
88
|
+
rescue NameError
|
89
|
+
raise if explicit_receiver
|
90
|
+
# Do nothing. Fall back to `"..."`
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
if method_object
|
95
|
+
parameters = method_object.parameters
|
96
|
+
|
97
|
+
if parameters.map(&:first).intersect?([:opt, :rest, :keyreq, :key, :keyrest])
|
98
|
+
"..."
|
99
|
+
else
|
100
|
+
defn = parameters.filter_map { |type, arg| arg if type == :req }
|
101
|
+
defn << "&"
|
102
|
+
defn.join(", ")
|
103
|
+
end
|
104
|
+
else
|
105
|
+
"..."
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# The following generated method calls the target exactly once, storing
|
110
|
+
# the returned value in a dummy variable.
|
111
|
+
#
|
112
|
+
# Reason is twofold: On one hand doing less calls is in general better.
|
113
|
+
# On the other hand it could be that the target has side-effects,
|
114
|
+
# whereas conceptually, from the user point of view, the delegator should
|
115
|
+
# be doing one call.
|
116
|
+
if nilable == false
|
117
|
+
method_def <<
|
118
|
+
"def #{method_name}(#{definition})" <<
|
119
|
+
" (#{receiver}).#{method}(#{definition})" <<
|
120
|
+
"end"
|
121
|
+
elsif allow_nil
|
122
|
+
method = method.to_s
|
123
|
+
|
124
|
+
method_def <<
|
125
|
+
"def #{method_name}(#{definition})" <<
|
126
|
+
" _ = #{receiver}" <<
|
127
|
+
" if !_.nil? || nil.respond_to?(:#{method})" <<
|
128
|
+
" _.#{method}(#{definition})" <<
|
129
|
+
" end" <<
|
130
|
+
"end"
|
131
|
+
else
|
132
|
+
method = method.to_s
|
133
|
+
method_name = method_name.to_s
|
134
|
+
|
135
|
+
method_def <<
|
136
|
+
"def #{method_name}(#{definition})" <<
|
137
|
+
" _ = #{receiver}" <<
|
138
|
+
" _.#{method}(#{definition})" <<
|
139
|
+
"rescue NoMethodError => e" <<
|
140
|
+
" if _.nil? && e.name == :#{method}" <<
|
141
|
+
" raise ::ActiveSupport::DelegationError.nil_target(:#{method_name}, :'#{receiver}')" <<
|
142
|
+
" else" <<
|
143
|
+
" raise" <<
|
144
|
+
" end" <<
|
145
|
+
"end"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
owner.module_eval(method_def.join(";"), file, line)
|
149
|
+
method_names
|
150
|
+
end
|
151
|
+
|
152
|
+
def generate_method_missing(owner, target, allow_nil: nil)
|
153
|
+
target = target.to_s
|
154
|
+
target = "self.#{target}" if RESERVED_METHOD_NAMES.include?(target) || target == "__target"
|
155
|
+
|
156
|
+
if allow_nil
|
157
|
+
owner.module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
158
|
+
def respond_to_missing?(name, include_private = false)
|
159
|
+
# It may look like an oversight, but we deliberately do not pass
|
160
|
+
# +include_private+, because they do not get delegated.
|
161
|
+
|
162
|
+
return false if name == :marshal_dump || name == :_dump
|
163
|
+
#{target}.respond_to?(name) || super
|
164
|
+
end
|
165
|
+
|
166
|
+
def method_missing(method, ...)
|
167
|
+
__target = #{target}
|
168
|
+
if __target.nil? && !nil.respond_to?(method)
|
169
|
+
nil
|
170
|
+
elsif __target.respond_to?(method)
|
171
|
+
__target.public_send(method, ...)
|
172
|
+
else
|
173
|
+
super
|
174
|
+
end
|
175
|
+
end
|
176
|
+
RUBY
|
177
|
+
else
|
178
|
+
owner.module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
179
|
+
def respond_to_missing?(name, include_private = false)
|
180
|
+
# It may look like an oversight, but we deliberately do not pass
|
181
|
+
# +include_private+, because they do not get delegated.
|
182
|
+
|
183
|
+
return false if name == :marshal_dump || name == :_dump
|
184
|
+
#{target}.respond_to?(name) || super
|
185
|
+
end
|
186
|
+
|
187
|
+
def method_missing(method, ...)
|
188
|
+
__target = #{target}
|
189
|
+
if __target.nil? && !nil.respond_to?(method)
|
190
|
+
raise ::ActiveSupport::DelegationError.nil_target(method, :'#{target}')
|
191
|
+
elsif __target.respond_to?(method)
|
192
|
+
__target.public_send(method, ...)
|
193
|
+
else
|
194
|
+
super
|
195
|
+
end
|
196
|
+
end
|
197
|
+
RUBY
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/inflector/methods"
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
# = Active Support \Autoload
|
7
|
+
#
|
8
|
+
# Autoload and eager load conveniences for your library.
|
9
|
+
#
|
10
|
+
# This module allows you to define autoloads based on
|
11
|
+
# \Rails conventions (i.e. no need to define the path
|
12
|
+
# it is automatically guessed based on the filename)
|
13
|
+
# and also define a set of constants that needs to be
|
14
|
+
# eager loaded:
|
15
|
+
#
|
16
|
+
# module MyLib
|
17
|
+
# extend ActiveSupport::Autoload
|
18
|
+
#
|
19
|
+
# autoload :Model
|
20
|
+
#
|
21
|
+
# eager_autoload do
|
22
|
+
# autoload :Cache
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# Then your library can be eager loaded by simply calling:
|
27
|
+
#
|
28
|
+
# MyLib.eager_load!
|
29
|
+
module Autoload
|
30
|
+
def autoload(const_name, path = @_at_path)
|
31
|
+
unless path
|
32
|
+
full = [name, @_under_path, const_name.to_s].compact.join("::")
|
33
|
+
path = Inflector.underscore(full)
|
34
|
+
end
|
35
|
+
|
36
|
+
if @_eager_autoload
|
37
|
+
@_eagerloaded_constants ||= []
|
38
|
+
@_eagerloaded_constants << const_name
|
39
|
+
end
|
40
|
+
|
41
|
+
super const_name, path
|
42
|
+
end
|
43
|
+
|
44
|
+
def autoload_under(path)
|
45
|
+
@_under_path, old_path = path, @_under_path
|
46
|
+
yield
|
47
|
+
ensure
|
48
|
+
@_under_path = old_path
|
49
|
+
end
|
50
|
+
|
51
|
+
def autoload_at(path)
|
52
|
+
@_at_path, old_path = path, @_at_path
|
53
|
+
yield
|
54
|
+
ensure
|
55
|
+
@_at_path = old_path
|
56
|
+
end
|
57
|
+
|
58
|
+
def eager_autoload
|
59
|
+
old_eager, @_eager_autoload = @_eager_autoload, true
|
60
|
+
yield
|
61
|
+
ensure
|
62
|
+
@_eager_autoload = old_eager
|
63
|
+
end
|
64
|
+
|
65
|
+
def eager_load!
|
66
|
+
if @_eagerloaded_constants
|
67
|
+
@_eagerloaded_constants.each { |const_name| const_get(const_name) }
|
68
|
+
@_eagerloaded_constants = nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concurrency/share_lock"
|
4
|
+
|
5
|
+
module ActiveSupport # :nodoc:
|
6
|
+
module Dependencies # :nodoc:
|
7
|
+
class Interlock
|
8
|
+
def initialize # :nodoc:
|
9
|
+
@lock = ActiveSupport::Concurrency::ShareLock.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def loading(&block)
|
13
|
+
@lock.exclusive(purpose: :load, compatible: [:load], after_compatible: [:load], &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def unloading(&block)
|
17
|
+
@lock.exclusive(purpose: :unload, compatible: [:load, :unload], after_compatible: [:load, :unload], &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def start_unloading
|
21
|
+
@lock.start_exclusive(purpose: :unload, compatible: [:load, :unload])
|
22
|
+
end
|
23
|
+
|
24
|
+
def done_unloading
|
25
|
+
@lock.stop_exclusive(compatible: [:load, :unload])
|
26
|
+
end
|
27
|
+
|
28
|
+
def start_running
|
29
|
+
@lock.start_sharing
|
30
|
+
end
|
31
|
+
|
32
|
+
def done_running
|
33
|
+
@lock.stop_sharing
|
34
|
+
end
|
35
|
+
|
36
|
+
def running(&block)
|
37
|
+
@lock.sharing(&block)
|
38
|
+
end
|
39
|
+
|
40
|
+
def permit_concurrent_loads(&block)
|
41
|
+
@lock.yield_shares(compatible: [:load], &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
def raw_state(&block) # :nodoc:
|
45
|
+
@lock.raw_state(&block)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveSupport::Dependencies::RequireDependency
|
4
|
+
# <b>Warning:</b> This method is obsolete. The semantics of the autoloader
|
5
|
+
# match Ruby's and you do not need to be defensive with load order anymore.
|
6
|
+
# Just refer to classes and modules normally.
|
7
|
+
#
|
8
|
+
# Engines that do not control the mode in which their parent application runs
|
9
|
+
# should call +require_dependency+ where needed in case the runtime mode is
|
10
|
+
# +:classic+.
|
11
|
+
def require_dependency(filename)
|
12
|
+
filename = filename.to_path if filename.respond_to?(:to_path)
|
13
|
+
|
14
|
+
unless filename.is_a?(String)
|
15
|
+
raise ArgumentError, "the file name must be either a String or implement #to_path -- you passed #{filename.inspect}"
|
16
|
+
end
|
17
|
+
|
18
|
+
if abspath = ActiveSupport::Dependencies.search_for_file(filename)
|
19
|
+
require abspath
|
20
|
+
else
|
21
|
+
require filename
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# We could define require_dependency in Object directly, but a module makes
|
26
|
+
# the extension apparent if you list ancestors.
|
27
|
+
Object.prepend(self)
|
28
|
+
end
|