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,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/array_inquirer"
|
4
|
+
|
5
|
+
class Array
|
6
|
+
# Wraps the array in an ActiveSupport::ArrayInquirer object, which gives a
|
7
|
+
# friendlier way to check its string-like contents.
|
8
|
+
#
|
9
|
+
# pets = [:cat, :dog].inquiry
|
10
|
+
#
|
11
|
+
# pets.cat? # => true
|
12
|
+
# pets.ferret? # => false
|
13
|
+
#
|
14
|
+
# pets.any?(:cat, :ferret) # => true
|
15
|
+
# pets.any?(:ferret, :alligator) # => false
|
16
|
+
def inquiry
|
17
|
+
ActiveSupport::ArrayInquirer.new(self)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Array
|
4
|
+
# Wraps its argument in an array unless it is already an array (or array-like).
|
5
|
+
#
|
6
|
+
# Specifically:
|
7
|
+
#
|
8
|
+
# * If the argument is +nil+ an empty array is returned.
|
9
|
+
# * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
|
10
|
+
# * Otherwise, returns an array with the argument as its single element.
|
11
|
+
#
|
12
|
+
# Array.wrap(nil) # => []
|
13
|
+
# Array.wrap([1, 2, 3]) # => [1, 2, 3]
|
14
|
+
# Array.wrap(0) # => [0]
|
15
|
+
#
|
16
|
+
# This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
|
17
|
+
#
|
18
|
+
# * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
|
19
|
+
# moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
|
20
|
+
# an array with the argument as its single element right away.
|
21
|
+
# * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
|
22
|
+
# raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
|
23
|
+
# * It does not call +to_a+ on the argument, if the argument does not respond to +to_ary+
|
24
|
+
# it returns an array with the argument as its single element.
|
25
|
+
#
|
26
|
+
# The last point is easily explained with some enumerables:
|
27
|
+
#
|
28
|
+
# Array(foo: :bar) # => [[:foo, :bar]]
|
29
|
+
# Array.wrap(foo: :bar) # => [{:foo=>:bar}]
|
30
|
+
#
|
31
|
+
# There's also a related idiom that uses the splat operator:
|
32
|
+
#
|
33
|
+
# [*object]
|
34
|
+
#
|
35
|
+
# which returns <tt>[]</tt> for +nil+, but calls to <tt>Array(object)</tt> otherwise.
|
36
|
+
#
|
37
|
+
# The differences with <tt>Kernel#Array</tt> explained above
|
38
|
+
# apply to the rest of <tt>object</tt>s.
|
39
|
+
def self.wrap(object)
|
40
|
+
if object.nil?
|
41
|
+
[]
|
42
|
+
elsif object.respond_to?(:to_ary)
|
43
|
+
object.to_ary || [object]
|
44
|
+
else
|
45
|
+
[object]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/array/wrap"
|
4
|
+
require "active_support/core_ext/array/access"
|
5
|
+
require "active_support/core_ext/array/conversions"
|
6
|
+
require "active_support/core_ext/array/extract"
|
7
|
+
require "active_support/core_ext/array/extract_options"
|
8
|
+
require "active_support/core_ext/array/grouping"
|
9
|
+
require "active_support/core_ext/array/inquiry"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "benchmark"
|
4
|
+
|
5
|
+
class << Benchmark
|
6
|
+
def ms(&block) # :nodoc
|
7
|
+
# NOTE: Please also remove the Active Support `benchmark` dependency when removing this
|
8
|
+
ActiveSupport.deprecator.warn <<~TEXT
|
9
|
+
`Benchmark.ms` is deprecated and will be removed in Rails 8.1 without replacement.
|
10
|
+
TEXT
|
11
|
+
ActiveSupport::Benchmark.realtime(:float_millisecond, &block)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bigdecimal"
|
4
|
+
require "bigdecimal/util"
|
5
|
+
|
6
|
+
module ActiveSupport
|
7
|
+
module BigDecimalWithDefaultFormat # :nodoc:
|
8
|
+
def to_s(format = "F")
|
9
|
+
super(format)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
BigDecimal.prepend(ActiveSupport::BigDecimalWithDefaultFormat)
|
@@ -0,0 +1,122 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/redefine_method"
|
4
|
+
require "active_support/class_attribute"
|
5
|
+
|
6
|
+
class Class
|
7
|
+
# Declare a class-level attribute whose value is inheritable by subclasses.
|
8
|
+
# Subclasses can change their own value and it will not impact parent class.
|
9
|
+
#
|
10
|
+
# ==== Options
|
11
|
+
#
|
12
|
+
# * <tt>:instance_reader</tt> - Sets the instance reader method (defaults to true).
|
13
|
+
# * <tt>:instance_writer</tt> - Sets the instance writer method (defaults to true).
|
14
|
+
# * <tt>:instance_accessor</tt> - Sets both instance methods (defaults to true).
|
15
|
+
# * <tt>:instance_predicate</tt> - Sets a predicate method (defaults to true).
|
16
|
+
# * <tt>:default</tt> - Sets a default value for the attribute (defaults to nil).
|
17
|
+
#
|
18
|
+
# ==== Examples
|
19
|
+
#
|
20
|
+
# class Base
|
21
|
+
# class_attribute :setting
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# class Subclass < Base
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# Base.setting = true
|
28
|
+
# Subclass.setting # => true
|
29
|
+
# Subclass.setting = false
|
30
|
+
# Subclass.setting # => false
|
31
|
+
# Base.setting # => true
|
32
|
+
#
|
33
|
+
# In the above case as long as Subclass does not assign a value to setting
|
34
|
+
# by performing <tt>Subclass.setting = _something_</tt>, <tt>Subclass.setting</tt>
|
35
|
+
# would read value assigned to parent class. Once Subclass assigns a value then
|
36
|
+
# the value assigned by Subclass would be returned.
|
37
|
+
#
|
38
|
+
# This matches normal Ruby method inheritance: think of writing an attribute
|
39
|
+
# on a subclass as overriding the reader method. However, you need to be aware
|
40
|
+
# when using +class_attribute+ with mutable structures as +Array+ or +Hash+.
|
41
|
+
# In such cases, you don't want to do changes in place. Instead use setters:
|
42
|
+
#
|
43
|
+
# Base.setting = []
|
44
|
+
# Base.setting # => []
|
45
|
+
# Subclass.setting # => []
|
46
|
+
#
|
47
|
+
# # Appending in child changes both parent and child because it is the same object:
|
48
|
+
# Subclass.setting << :foo
|
49
|
+
# Base.setting # => [:foo]
|
50
|
+
# Subclass.setting # => [:foo]
|
51
|
+
#
|
52
|
+
# # Use setters to not propagate changes:
|
53
|
+
# Base.setting = []
|
54
|
+
# Subclass.setting += [:foo]
|
55
|
+
# Base.setting # => []
|
56
|
+
# Subclass.setting # => [:foo]
|
57
|
+
#
|
58
|
+
# For convenience, an instance predicate method is defined as well.
|
59
|
+
# To skip it, pass <tt>instance_predicate: false</tt>.
|
60
|
+
#
|
61
|
+
# Subclass.setting? # => false
|
62
|
+
#
|
63
|
+
# Instances may overwrite the class value in the same way:
|
64
|
+
#
|
65
|
+
# Base.setting = true
|
66
|
+
# object = Base.new
|
67
|
+
# object.setting # => true
|
68
|
+
# object.setting = false
|
69
|
+
# object.setting # => false
|
70
|
+
# Base.setting # => true
|
71
|
+
#
|
72
|
+
# To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
|
73
|
+
#
|
74
|
+
# object.setting # => NoMethodError
|
75
|
+
# object.setting? # => NoMethodError
|
76
|
+
#
|
77
|
+
# To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
|
78
|
+
#
|
79
|
+
# object.setting = false # => NoMethodError
|
80
|
+
#
|
81
|
+
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
|
82
|
+
#
|
83
|
+
# To set a default value for the attribute, pass <tt>default:</tt>, like so:
|
84
|
+
#
|
85
|
+
# class_attribute :settings, default: {}
|
86
|
+
def class_attribute(*attrs, instance_accessor: true,
|
87
|
+
instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil)
|
88
|
+
|
89
|
+
class_methods, methods = [], []
|
90
|
+
attrs.each do |name|
|
91
|
+
unless name.is_a?(Symbol) || name.is_a?(String)
|
92
|
+
raise TypeError, "#{name.inspect} is not a symbol nor a string"
|
93
|
+
end
|
94
|
+
|
95
|
+
name = name.to_sym
|
96
|
+
::ActiveSupport::ClassAttribute.redefine(self, name, default)
|
97
|
+
|
98
|
+
unless singleton_class?
|
99
|
+
methods << <<~RUBY if instance_reader
|
100
|
+
silence_redefinition_of_method def #{name}
|
101
|
+
defined?(@#{name}) ? @#{name} : self.class.#{name}
|
102
|
+
end
|
103
|
+
RUBY
|
104
|
+
end
|
105
|
+
|
106
|
+
methods << <<~RUBY if instance_writer
|
107
|
+
silence_redefinition_of_method(:#{name}=)
|
108
|
+
attr_writer :#{name}
|
109
|
+
RUBY
|
110
|
+
|
111
|
+
if instance_predicate
|
112
|
+
class_methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
113
|
+
if instance_reader
|
114
|
+
methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
location = caller_locations(1, 1).first
|
120
|
+
class_eval(["class << self", *class_methods, "end", *methods].join(";").tr("\n", ";"), location.path, location.lineno)
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# cattr_* became mattr_* aliases in 7dfbd91b0780fbd6a1dd9bfbc176e10894871d2d,
|
4
|
+
# but we keep this around for libraries that directly require it knowing they
|
5
|
+
# want cattr_*. No need to deprecate.
|
6
|
+
require "active_support/core_ext/module/attribute_accessors"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/descendants_tracker"
|
4
|
+
|
5
|
+
class Class
|
6
|
+
# Returns an array with all classes that are < than its receiver.
|
7
|
+
#
|
8
|
+
# class C; end
|
9
|
+
# C.descendants # => []
|
10
|
+
#
|
11
|
+
# class B < C; end
|
12
|
+
# C.descendants # => [B]
|
13
|
+
#
|
14
|
+
# class A < B; end
|
15
|
+
# C.descendants # => [B, A]
|
16
|
+
#
|
17
|
+
# class D < C; end
|
18
|
+
# C.descendants # => [B, A, D]
|
19
|
+
def descendants
|
20
|
+
subclasses.concat(subclasses.flat_map(&:descendants))
|
21
|
+
end
|
22
|
+
|
23
|
+
prepend ActiveSupport::DescendantsTracker::ReloadedClassesFiltering
|
24
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "date"
|
4
|
+
require "active_support/duration"
|
5
|
+
require "active_support/core_ext/object/acts_like"
|
6
|
+
require "active_support/core_ext/date/zones"
|
7
|
+
require "active_support/core_ext/time/zones"
|
8
|
+
require "active_support/core_ext/date_and_time/calculations"
|
9
|
+
|
10
|
+
class Date
|
11
|
+
include DateAndTime::Calculations
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_accessor :beginning_of_week_default
|
15
|
+
|
16
|
+
# Returns the week start (e.g. +:monday+) for the current request, if this has been set (via Date.beginning_of_week=).
|
17
|
+
# If <tt>Date.beginning_of_week</tt> has not been set for the current request, returns the week start specified in <tt>config.beginning_of_week</tt>.
|
18
|
+
# If no +config.beginning_of_week+ was specified, returns +:monday+.
|
19
|
+
def beginning_of_week
|
20
|
+
::ActiveSupport::IsolatedExecutionState[:beginning_of_week] || beginning_of_week_default || :monday
|
21
|
+
end
|
22
|
+
|
23
|
+
# Sets <tt>Date.beginning_of_week</tt> to a week start (e.g. +:monday+) for current request/thread.
|
24
|
+
#
|
25
|
+
# This method accepts any of the following day symbols:
|
26
|
+
# +:monday+, +:tuesday+, +:wednesday+, +:thursday+, +:friday+, +:saturday+, +:sunday+
|
27
|
+
def beginning_of_week=(week_start)
|
28
|
+
::ActiveSupport::IsolatedExecutionState[:beginning_of_week] = find_beginning_of_week!(week_start)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns week start day symbol (e.g. +:monday+), or raises an +ArgumentError+ for invalid day symbol.
|
32
|
+
def find_beginning_of_week!(week_start)
|
33
|
+
raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
|
34
|
+
week_start
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
|
38
|
+
def yesterday
|
39
|
+
::Date.current.yesterday
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
|
43
|
+
def tomorrow
|
44
|
+
::Date.current.tomorrow
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns Time.zone.today when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns Date.today.
|
48
|
+
def current
|
49
|
+
::Time.zone ? ::Time.zone.today : ::Date.today
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
54
|
+
# and then subtracts the specified number of seconds.
|
55
|
+
def ago(seconds)
|
56
|
+
in_time_zone.since(-seconds)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
60
|
+
# and then adds the specified number of seconds
|
61
|
+
def since(seconds)
|
62
|
+
in_time_zone.since(seconds)
|
63
|
+
end
|
64
|
+
alias :in :since
|
65
|
+
|
66
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
67
|
+
def beginning_of_day
|
68
|
+
in_time_zone
|
69
|
+
end
|
70
|
+
alias :midnight :beginning_of_day
|
71
|
+
alias :at_midnight :beginning_of_day
|
72
|
+
alias :at_beginning_of_day :beginning_of_day
|
73
|
+
|
74
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the middle of the day (12:00)
|
75
|
+
def middle_of_day
|
76
|
+
in_time_zone.middle_of_day
|
77
|
+
end
|
78
|
+
alias :midday :middle_of_day
|
79
|
+
alias :noon :middle_of_day
|
80
|
+
alias :at_midday :middle_of_day
|
81
|
+
alias :at_noon :middle_of_day
|
82
|
+
alias :at_middle_of_day :middle_of_day
|
83
|
+
|
84
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
|
85
|
+
def end_of_day
|
86
|
+
in_time_zone.end_of_day
|
87
|
+
end
|
88
|
+
alias :at_end_of_day :end_of_day
|
89
|
+
|
90
|
+
def plus_with_duration(other) # :nodoc:
|
91
|
+
if ActiveSupport::Duration === other
|
92
|
+
other.since(self)
|
93
|
+
else
|
94
|
+
plus_without_duration(other)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
alias_method :plus_without_duration, :+
|
98
|
+
alias_method :+, :plus_with_duration
|
99
|
+
|
100
|
+
def minus_with_duration(other) # :nodoc:
|
101
|
+
if ActiveSupport::Duration === other
|
102
|
+
plus_with_duration(-other)
|
103
|
+
else
|
104
|
+
minus_without_duration(other)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
alias_method :minus_without_duration, :-
|
108
|
+
alias_method :-, :minus_with_duration
|
109
|
+
|
110
|
+
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
111
|
+
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
|
112
|
+
#
|
113
|
+
# The increments are applied in order of time units from largest to smallest.
|
114
|
+
# In other words, the date is incremented first by +:years+, then by
|
115
|
+
# +:months+, then by +:weeks+, then by +:days+. This order can affect the
|
116
|
+
# result around the end of a month. For example, incrementing first by months
|
117
|
+
# then by days:
|
118
|
+
#
|
119
|
+
# Date.new(2004, 9, 30).advance(months: 1, days: 1)
|
120
|
+
# # => Sun, 31 Oct 2004
|
121
|
+
#
|
122
|
+
# Whereas incrementing first by days then by months yields a different result:
|
123
|
+
#
|
124
|
+
# Date.new(2004, 9, 30).advance(days: 1).advance(months: 1)
|
125
|
+
# # => Mon, 01 Nov 2004
|
126
|
+
#
|
127
|
+
def advance(options)
|
128
|
+
d = self
|
129
|
+
|
130
|
+
d = d >> options[:years] * 12 if options[:years]
|
131
|
+
d = d >> options[:months] if options[:months]
|
132
|
+
d = d + options[:weeks] * 7 if options[:weeks]
|
133
|
+
d = d + options[:days] if options[:days]
|
134
|
+
|
135
|
+
d
|
136
|
+
end
|
137
|
+
|
138
|
+
# Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
|
139
|
+
# The +options+ parameter is a hash with a combination of these keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>.
|
140
|
+
#
|
141
|
+
# Date.new(2007, 5, 12).change(day: 1) # => Date.new(2007, 5, 1)
|
142
|
+
# Date.new(2007, 5, 12).change(year: 2005, month: 1) # => Date.new(2005, 1, 12)
|
143
|
+
def change(options)
|
144
|
+
::Date.new(
|
145
|
+
options.fetch(:year, year),
|
146
|
+
options.fetch(:month, month),
|
147
|
+
options.fetch(:day, day)
|
148
|
+
)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there.
|
152
|
+
def compare_with_coercion(other)
|
153
|
+
if other.is_a?(Time)
|
154
|
+
to_datetime <=> other
|
155
|
+
else
|
156
|
+
compare_without_coercion(other)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
alias_method :compare_without_coercion, :<=>
|
160
|
+
alias_method :<=>, :compare_with_coercion
|
161
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "date"
|
4
|
+
require "active_support/inflector/methods"
|
5
|
+
require "active_support/core_ext/date/zones"
|
6
|
+
require "active_support/core_ext/module/redefine_method"
|
7
|
+
|
8
|
+
class Date
|
9
|
+
DATE_FORMATS = {
|
10
|
+
short: "%d %b",
|
11
|
+
long: "%B %d, %Y",
|
12
|
+
db: "%Y-%m-%d",
|
13
|
+
inspect: "%Y-%m-%d",
|
14
|
+
number: "%Y%m%d",
|
15
|
+
long_ordinal: lambda { |date|
|
16
|
+
day_format = ActiveSupport::Inflector.ordinalize(date.day)
|
17
|
+
date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
|
18
|
+
},
|
19
|
+
rfc822: "%d %b %Y",
|
20
|
+
rfc2822: "%d %b %Y",
|
21
|
+
iso8601: lambda { |date| date.iso8601 }
|
22
|
+
}
|
23
|
+
|
24
|
+
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
|
25
|
+
#
|
26
|
+
# This method is aliased to <tt>to_formatted_s</tt>.
|
27
|
+
#
|
28
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
29
|
+
#
|
30
|
+
# date.to_fs(:db) # => "2007-11-10"
|
31
|
+
# date.to_formatted_s(:db) # => "2007-11-10"
|
32
|
+
#
|
33
|
+
# date.to_fs(:short) # => "10 Nov"
|
34
|
+
# date.to_fs(:number) # => "20071110"
|
35
|
+
# date.to_fs(:long) # => "November 10, 2007"
|
36
|
+
# date.to_fs(:long_ordinal) # => "November 10th, 2007"
|
37
|
+
# date.to_fs(:rfc822) # => "10 Nov 2007"
|
38
|
+
# date.to_fs(:rfc2822) # => "10 Nov 2007"
|
39
|
+
# date.to_fs(:iso8601) # => "2007-11-10"
|
40
|
+
#
|
41
|
+
# == Adding your own date formats to to_fs
|
42
|
+
# You can add your own formats to the Date::DATE_FORMATS hash.
|
43
|
+
# Use the format name as the hash key and either a strftime string
|
44
|
+
# or Proc instance that takes a date argument as the value.
|
45
|
+
#
|
46
|
+
# # config/initializers/date_formats.rb
|
47
|
+
# Date::DATE_FORMATS[:month_and_year] = '%B %Y'
|
48
|
+
# Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") }
|
49
|
+
def to_fs(format = :default)
|
50
|
+
if formatter = DATE_FORMATS[format]
|
51
|
+
if formatter.respond_to?(:call)
|
52
|
+
formatter.call(self).to_s
|
53
|
+
else
|
54
|
+
strftime(formatter)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
to_s
|
58
|
+
end
|
59
|
+
end
|
60
|
+
alias_method :to_formatted_s, :to_fs
|
61
|
+
|
62
|
+
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
|
63
|
+
def readable_inspect
|
64
|
+
strftime("%a, %d %b %Y")
|
65
|
+
end
|
66
|
+
alias_method :default_inspect, :inspect
|
67
|
+
alias_method :inspect, :readable_inspect
|
68
|
+
|
69
|
+
silence_redefinition_of_method :to_time
|
70
|
+
|
71
|
+
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
72
|
+
# The timezone can be either +:local+ or +:utc+ (default +:local+).
|
73
|
+
#
|
74
|
+
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
75
|
+
#
|
76
|
+
# date.to_time # => 2007-11-10 00:00:00 0800
|
77
|
+
# date.to_time(:local) # => 2007-11-10 00:00:00 0800
|
78
|
+
#
|
79
|
+
# date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
|
80
|
+
#
|
81
|
+
# NOTE: The +:local+ timezone is Ruby's *process* timezone, i.e. <tt>ENV['TZ']</tt>.
|
82
|
+
# If the <b>application's</b> timezone is needed, then use +in_time_zone+ instead.
|
83
|
+
def to_time(form = :local)
|
84
|
+
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
|
85
|
+
::Time.public_send(form, year, month, day)
|
86
|
+
end
|
87
|
+
|
88
|
+
silence_redefinition_of_method :xmlschema
|
89
|
+
|
90
|
+
# Returns a string which represents the time in used time zone as DateTime
|
91
|
+
# defined by XML Schema:
|
92
|
+
#
|
93
|
+
# date = Date.new(2015, 05, 23) # => Sat, 23 May 2015
|
94
|
+
# date.xmlschema # => "2015-05-23T00:00:00+04:00"
|
95
|
+
def xmlschema
|
96
|
+
in_time_zone.xmlschema
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/date/acts_like"
|
4
|
+
require "active_support/core_ext/date/blank"
|
5
|
+
require "active_support/core_ext/date/calculations"
|
6
|
+
require "active_support/core_ext/date/conversions"
|
7
|
+
require "active_support/core_ext/date/zones"
|