activesupport 7.0.0 → 7.2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +156 -255
- data/MIT-LICENSE +1 -1
- data/README.rdoc +6 -6
- data/lib/active_support/actionable_error.rb +3 -1
- data/lib/active_support/array_inquirer.rb +3 -1
- data/lib/active_support/backtrace_cleaner.rb +41 -9
- data/lib/active_support/benchmarkable.rb +1 -0
- data/lib/active_support/broadcast_logger.rb +251 -0
- data/lib/active_support/builder.rb +1 -1
- 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 +49 -17
- data/lib/active_support/cache/mem_cache_store.rb +111 -129
- data/lib/active_support/cache/memory_store.rb +81 -26
- data/lib/active_support/cache/null_store.rb +6 -0
- data/lib/active_support/cache/redis_cache_store.rb +175 -154
- data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
- data/lib/active_support/cache/strategy/local_cache.rb +31 -13
- data/lib/active_support/cache.rb +457 -377
- data/lib/active_support/callbacks.rb +123 -139
- data/lib/active_support/code_generator.rb +15 -10
- data/lib/active_support/concern.rb +4 -2
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +42 -3
- data/lib/active_support/concurrency/null_lock.rb +13 -0
- data/lib/active_support/configurable.rb +12 -2
- data/lib/active_support/core_ext/array/conversions.rb +7 -9
- data/lib/active_support/core_ext/array/inquiry.rb +2 -2
- data/lib/active_support/core_ext/array.rb +0 -1
- data/lib/active_support/core_ext/class/subclasses.rb +4 -15
- data/lib/active_support/core_ext/date/blank.rb +4 -0
- data/lib/active_support/core_ext/date/calculations.rb +20 -5
- data/lib/active_support/core_ext/date/conversions.rb +15 -16
- data/lib/active_support/core_ext/date.rb +0 -1
- data/lib/active_support/core_ext/date_and_time/calculations.rb +14 -4
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +29 -2
- data/lib/active_support/core_ext/date_time/blank.rb +4 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +4 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +15 -15
- data/lib/active_support/core_ext/date_time.rb +0 -1
- data/lib/active_support/core_ext/digest/uuid.rb +7 -10
- data/lib/active_support/core_ext/enumerable.rb +51 -101
- data/lib/active_support/core_ext/erb/util.rb +201 -0
- data/lib/active_support/core_ext/file/atomic.rb +2 -0
- data/lib/active_support/core_ext/hash/conversions.rb +1 -2
- data/lib/active_support/core_ext/hash/deep_merge.rb +22 -14
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +3 -3
- data/lib/active_support/core_ext/hash/indifferent_access.rb +3 -3
- data/lib/active_support/core_ext/hash/keys.rb +7 -7
- data/lib/active_support/core_ext/integer/inflections.rb +12 -12
- data/lib/active_support/core_ext/kernel/singleton_class.rb +1 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +17 -6
- data/lib/active_support/core_ext/module/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +38 -20
- data/lib/active_support/core_ext/module/concerning.rb +6 -6
- data/lib/active_support/core_ext/module/delegation.rb +20 -119
- data/lib/active_support/core_ext/module/deprecation.rb +12 -12
- data/lib/active_support/core_ext/module/introspection.rb +0 -1
- data/lib/active_support/core_ext/numeric/bytes.rb +9 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +77 -75
- data/lib/active_support/core_ext/numeric.rb +0 -1
- data/lib/active_support/core_ext/object/acts_like.rb +29 -5
- data/lib/active_support/core_ext/object/blank.rb +45 -1
- data/lib/active_support/core_ext/object/deep_dup.rb +16 -0
- data/lib/active_support/core_ext/object/duplicable.rb +25 -16
- data/lib/active_support/core_ext/object/inclusion.rb +13 -5
- data/lib/active_support/core_ext/object/instance_variables.rb +4 -2
- data/lib/active_support/core_ext/object/json.rb +17 -7
- data/lib/active_support/core_ext/object/to_query.rb +0 -2
- data/lib/active_support/core_ext/object/with.rb +46 -0
- data/lib/active_support/core_ext/object/with_options.rb +9 -9
- data/lib/active_support/core_ext/object.rb +1 -0
- data/lib/active_support/core_ext/pathname/blank.rb +20 -0
- data/lib/active_support/core_ext/pathname/existence.rb +2 -0
- data/lib/active_support/core_ext/pathname.rb +1 -0
- data/lib/active_support/core_ext/range/conversions.rb +32 -11
- data/lib/active_support/core_ext/range/overlap.rb +40 -0
- data/lib/active_support/core_ext/range.rb +1 -2
- data/lib/active_support/core_ext/securerandom.rb +2 -6
- data/lib/active_support/core_ext/string/conversions.rb +3 -3
- data/lib/active_support/core_ext/string/filters.rb +21 -15
- data/lib/active_support/core_ext/string/indent.rb +1 -1
- data/lib/active_support/core_ext/string/inflections.rb +16 -9
- data/lib/active_support/core_ext/string/inquiry.rb +1 -1
- data/lib/active_support/core_ext/string/multibyte.rb +1 -1
- data/lib/active_support/core_ext/string/output_safety.rb +39 -150
- data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
- data/lib/active_support/core_ext/time/calculations.rb +42 -32
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/time/conversions.rb +13 -15
- data/lib/active_support/core_ext/time/zones.rb +8 -9
- data/lib/active_support/core_ext/time.rb +0 -1
- data/lib/active_support/core_ext.rb +0 -1
- data/lib/active_support/current_attributes.rb +53 -46
- data/lib/active_support/deep_mergeable.rb +53 -0
- data/lib/active_support/delegation.rb +202 -0
- data/lib/active_support/dependencies/autoload.rb +9 -16
- data/lib/active_support/deprecation/behaviors.rb +65 -42
- data/lib/active_support/deprecation/constant_accessor.rb +47 -25
- data/lib/active_support/deprecation/deprecators.rb +104 -0
- data/lib/active_support/deprecation/disallowed.rb +6 -8
- data/lib/active_support/deprecation/method_wrappers.rb +6 -23
- data/lib/active_support/deprecation/proxy_wrappers.rb +34 -22
- data/lib/active_support/deprecation/reporting.rb +49 -27
- data/lib/active_support/deprecation.rb +39 -9
- data/lib/active_support/deprecator.rb +7 -0
- data/lib/active_support/descendants_tracker.rb +66 -175
- data/lib/active_support/duration/iso8601_parser.rb +2 -2
- data/lib/active_support/duration/iso8601_serializer.rb +1 -4
- data/lib/active_support/duration.rb +13 -7
- data/lib/active_support/encrypted_configuration.rb +63 -10
- data/lib/active_support/encrypted_file.rb +29 -13
- data/lib/active_support/environment_inquirer.rb +22 -2
- data/lib/active_support/error_reporter/test_helper.rb +15 -0
- data/lib/active_support/error_reporter.rb +160 -36
- data/lib/active_support/evented_file_update_checker.rb +19 -7
- data/lib/active_support/execution_wrapper.rb +23 -28
- data/lib/active_support/file_update_checker.rb +5 -3
- data/lib/active_support/fork_tracker.rb +4 -32
- data/lib/active_support/gem_version.rb +4 -4
- data/lib/active_support/gzip.rb +2 -0
- data/lib/active_support/hash_with_indifferent_access.rb +41 -25
- data/lib/active_support/html_safe_translation.rb +19 -6
- data/lib/active_support/i18n.rb +1 -1
- data/lib/active_support/i18n_railtie.rb +20 -13
- data/lib/active_support/inflector/inflections.rb +2 -0
- data/lib/active_support/inflector/methods.rb +28 -18
- data/lib/active_support/inflector/transliterate.rb +4 -2
- data/lib/active_support/isolated_execution_state.rb +39 -19
- data/lib/active_support/json/decoding.rb +2 -1
- data/lib/active_support/json/encoding.rb +25 -43
- data/lib/active_support/key_generator.rb +13 -5
- data/lib/active_support/lazy_load_hooks.rb +33 -7
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber/test_helper.rb +2 -2
- data/lib/active_support/log_subscriber.rb +76 -36
- data/lib/active_support/logger.rb +22 -60
- data/lib/active_support/logger_thread_safe_level.rb +10 -32
- data/lib/active_support/message_encryptor.rb +200 -55
- 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 +220 -89
- 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 +111 -45
- data/lib/active_support/messages/rotation_coordinator.rb +93 -0
- data/lib/active_support/messages/rotator.rb +34 -32
- data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
- data/lib/active_support/multibyte/chars.rb +4 -2
- data/lib/active_support/multibyte/unicode.rb +9 -37
- data/lib/active_support/notifications/fanout.rb +248 -87
- data/lib/active_support/notifications/instrumenter.rb +93 -25
- data/lib/active_support/notifications.rb +38 -31
- data/lib/active_support/number_helper/number_converter.rb +16 -7
- data/lib/active_support/number_helper/number_to_currency_converter.rb +6 -6
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +3 -3
- data/lib/active_support/number_helper/number_to_phone_converter.rb +1 -0
- data/lib/active_support/number_helper.rb +379 -317
- data/lib/active_support/option_merger.rb +4 -4
- data/lib/active_support/ordered_hash.rb +3 -3
- data/lib/active_support/ordered_options.rb +68 -16
- data/lib/active_support/parameter_filter.rb +103 -84
- data/lib/active_support/proxy_object.rb +8 -3
- data/lib/active_support/railtie.rb +30 -25
- data/lib/active_support/reloader.rb +13 -5
- data/lib/active_support/rescuable.rb +12 -10
- data/lib/active_support/secure_compare_rotator.rb +17 -10
- data/lib/active_support/string_inquirer.rb +4 -2
- data/lib/active_support/subscriber.rb +10 -27
- data/lib/active_support/syntax_error_proxy.rb +60 -0
- data/lib/active_support/tagged_logging.rb +64 -25
- data/lib/active_support/test_case.rb +160 -7
- data/lib/active_support/testing/assertions.rb +29 -13
- data/lib/active_support/testing/autorun.rb +0 -2
- data/lib/active_support/testing/constant_stubbing.rb +54 -0
- data/lib/active_support/testing/deprecation.rb +20 -27
- data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
- data/lib/active_support/testing/isolation.rb +46 -33
- data/lib/active_support/testing/method_call_assertions.rb +7 -8
- data/lib/active_support/testing/parallelization/server.rb +3 -0
- data/lib/active_support/testing/parallelize_executor.rb +8 -3
- data/lib/active_support/testing/setup_and_teardown.rb +2 -0
- data/lib/active_support/testing/stream.rb +1 -1
- data/lib/active_support/testing/strict_warnings.rb +43 -0
- data/lib/active_support/testing/tests_without_assertions.rb +19 -0
- data/lib/active_support/testing/time_helpers.rb +38 -16
- data/lib/active_support/time_with_zone.rb +28 -54
- data/lib/active_support/values/time_zone.rb +26 -15
- data/lib/active_support/version.rb +1 -1
- data/lib/active_support/xml_mini/jdom.rb +3 -10
- data/lib/active_support/xml_mini/nokogiri.rb +1 -1
- data/lib/active_support/xml_mini/nokogirisax.rb +1 -1
- data/lib/active_support/xml_mini/rexml.rb +1 -1
- data/lib/active_support/xml_mini.rb +13 -4
- data/lib/active_support.rb +15 -3
- metadata +142 -21
- data/lib/active_support/core_ext/array/deprecated_conversions.rb +0 -25
- data/lib/active_support/core_ext/date/deprecated_conversions.rb +0 -26
- data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +0 -22
- data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +0 -60
- data/lib/active_support/core_ext/range/deprecated_conversions.rb +0 -26
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +0 -7
- data/lib/active_support/core_ext/range/overlaps.rb +0 -10
- data/lib/active_support/core_ext/time/deprecated_conversions.rb +0 -22
- data/lib/active_support/core_ext/uri.rb +0 -5
- data/lib/active_support/deprecation/instance_delegator.rb +0 -38
- data/lib/active_support/per_thread_registry.rb +0 -65
- data/lib/active_support/ruby_features.rb +0 -7
@@ -1,6 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "active_support/xml_mini"
|
4
3
|
require "active_support/core_ext/hash/keys"
|
5
4
|
require "active_support/core_ext/string/inflections"
|
6
5
|
require "active_support/core_ext/object/to_param"
|
@@ -87,12 +86,12 @@ class Array
|
|
87
86
|
# Extends <tt>Array#to_s</tt> to convert a collection of elements into a
|
88
87
|
# comma separated id list if <tt>:db</tt> argument is given as the format.
|
89
88
|
#
|
90
|
-
# This method is aliased to <tt>
|
89
|
+
# This method is aliased to <tt>to_formatted_s</tt>.
|
91
90
|
#
|
92
|
-
# Blog.all.
|
93
|
-
# Blog.none.
|
94
|
-
# [1,2].
|
95
|
-
def
|
91
|
+
# Blog.all.to_fs(:db) # => "1,2,3"
|
92
|
+
# Blog.none.to_fs(:db) # => "null"
|
93
|
+
# [1,2].to_fs # => "[1, 2]"
|
94
|
+
def to_fs(format = :default)
|
96
95
|
case format
|
97
96
|
when :db
|
98
97
|
if empty?
|
@@ -101,11 +100,10 @@ class Array
|
|
101
100
|
collect(&:id).join(",")
|
102
101
|
end
|
103
102
|
else
|
104
|
-
|
103
|
+
to_s
|
105
104
|
end
|
106
105
|
end
|
107
|
-
alias_method :
|
108
|
-
alias_method :to_default_s, :to_s
|
106
|
+
alias_method :to_formatted_s, :to_fs
|
109
107
|
|
110
108
|
# Returns a string that represents the array in XML by invoking +to_xml+
|
111
109
|
# on each element. Active Record collections delegate their representation
|
@@ -3,8 +3,8 @@
|
|
3
3
|
require "active_support/array_inquirer"
|
4
4
|
|
5
5
|
class Array
|
6
|
-
# Wraps the array in an
|
7
|
-
# to check its string-like contents.
|
6
|
+
# Wraps the array in an ActiveSupport::ArrayInquirer object, which gives a
|
7
|
+
# friendlier way to check its string-like contents.
|
8
8
|
#
|
9
9
|
# pets = [:cat, :dog].inquiry
|
10
10
|
#
|
@@ -3,7 +3,6 @@
|
|
3
3
|
require "active_support/core_ext/array/wrap"
|
4
4
|
require "active_support/core_ext/array/access"
|
5
5
|
require "active_support/core_ext/array/conversions"
|
6
|
-
require "active_support/core_ext/array/deprecated_conversions" unless ENV["RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION"]
|
7
6
|
require "active_support/core_ext/array/extract"
|
8
7
|
require "active_support/core_ext/array/extract_options"
|
9
8
|
require "active_support/core_ext/array/grouping"
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "active_support/
|
3
|
+
require "active_support/descendants_tracker"
|
4
4
|
|
5
5
|
class Class
|
6
6
|
# Returns an array with all classes that are < than its receiver.
|
@@ -17,19 +17,8 @@ class Class
|
|
17
17
|
# class D < C; end
|
18
18
|
# C.descendants # => [B, A, D]
|
19
19
|
def descendants
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end unless ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
|
20
|
+
subclasses.concat(subclasses.flat_map(&:descendants))
|
21
|
+
end
|
24
22
|
|
25
|
-
|
26
|
-
#
|
27
|
-
# class Foo; end
|
28
|
-
# class Bar < Foo; end
|
29
|
-
# class Baz < Bar; end
|
30
|
-
#
|
31
|
-
# Foo.subclasses # => [Bar]
|
32
|
-
def subclasses
|
33
|
-
descendants.select { |descendant| descendant.superclass == self }
|
34
|
-
end unless ActiveSupport::RubyFeatures::CLASS_SUBCLASSES
|
23
|
+
prepend ActiveSupport::DescendantsTracker::ReloadedClassesFiltering
|
35
24
|
end
|
@@ -13,22 +13,22 @@ class Date
|
|
13
13
|
class << self
|
14
14
|
attr_accessor :beginning_of_week_default
|
15
15
|
|
16
|
-
# Returns the week start (e.g.
|
16
|
+
# Returns the week start (e.g. +:monday+) for the current request, if this has been set (via Date.beginning_of_week=).
|
17
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
|
18
|
+
# If no +config.beginning_of_week+ was specified, returns +:monday+.
|
19
19
|
def beginning_of_week
|
20
20
|
::ActiveSupport::IsolatedExecutionState[:beginning_of_week] || beginning_of_week_default || :monday
|
21
21
|
end
|
22
22
|
|
23
|
-
# Sets <tt>Date.beginning_of_week</tt> to a week start (e.g.
|
23
|
+
# Sets <tt>Date.beginning_of_week</tt> to a week start (e.g. +:monday+) for current request/thread.
|
24
24
|
#
|
25
25
|
# This method accepts any of the following day symbols:
|
26
|
-
#
|
26
|
+
# +:monday+, +:tuesday+, +:wednesday+, +:thursday+, +:friday+, +:saturday+, +:sunday+
|
27
27
|
def beginning_of_week=(week_start)
|
28
28
|
::ActiveSupport::IsolatedExecutionState[:beginning_of_week] = find_beginning_of_week!(week_start)
|
29
29
|
end
|
30
30
|
|
31
|
-
# Returns week start day symbol (e.g.
|
31
|
+
# Returns week start day symbol (e.g. +:monday+), or raises an +ArgumentError+ for invalid day symbol.
|
32
32
|
def find_beginning_of_week!(week_start)
|
33
33
|
raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
|
34
34
|
week_start
|
@@ -109,6 +109,21 @@ class Date
|
|
109
109
|
|
110
110
|
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
111
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
|
+
#
|
112
127
|
def advance(options)
|
113
128
|
d = self
|
114
129
|
|
@@ -22,21 +22,21 @@ class Date
|
|
22
22
|
|
23
23
|
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
|
24
24
|
#
|
25
|
-
# This method is aliased to <tt>
|
25
|
+
# This method is aliased to <tt>to_formatted_s</tt>.
|
26
26
|
#
|
27
27
|
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
28
28
|
#
|
29
|
-
# date.to_formatted_s(:db) # => "2007-11-10"
|
30
29
|
# date.to_fs(:db) # => "2007-11-10"
|
30
|
+
# date.to_formatted_s(:db) # => "2007-11-10"
|
31
31
|
#
|
32
|
-
# date.
|
33
|
-
# date.
|
34
|
-
# date.
|
35
|
-
# date.
|
36
|
-
# date.
|
37
|
-
# date.
|
32
|
+
# date.to_fs(:short) # => "10 Nov"
|
33
|
+
# date.to_fs(:number) # => "20071110"
|
34
|
+
# date.to_fs(:long) # => "November 10, 2007"
|
35
|
+
# date.to_fs(:long_ordinal) # => "November 10th, 2007"
|
36
|
+
# date.to_fs(:rfc822) # => "10 Nov 2007"
|
37
|
+
# date.to_fs(:iso8601) # => "2007-11-10"
|
38
38
|
#
|
39
|
-
# == Adding your own date formats to
|
39
|
+
# == Adding your own date formats to to_fs
|
40
40
|
# You can add your own formats to the Date::DATE_FORMATS hash.
|
41
41
|
# Use the format name as the hash key and either a strftime string
|
42
42
|
# or Proc instance that takes a date argument as the value.
|
@@ -44,7 +44,7 @@ class Date
|
|
44
44
|
# # config/initializers/date_formats.rb
|
45
45
|
# Date::DATE_FORMATS[:month_and_year] = '%B %Y'
|
46
46
|
# Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") }
|
47
|
-
def
|
47
|
+
def to_fs(format = :default)
|
48
48
|
if formatter = DATE_FORMATS[format]
|
49
49
|
if formatter.respond_to?(:call)
|
50
50
|
formatter.call(self).to_s
|
@@ -52,11 +52,10 @@ class Date
|
|
52
52
|
strftime(formatter)
|
53
53
|
end
|
54
54
|
else
|
55
|
-
|
55
|
+
to_s
|
56
56
|
end
|
57
57
|
end
|
58
|
-
alias_method :
|
59
|
-
alias_method :to_default_s, :to_s
|
58
|
+
alias_method :to_formatted_s, :to_fs
|
60
59
|
|
61
60
|
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
|
62
61
|
def readable_inspect
|
@@ -68,7 +67,7 @@ class Date
|
|
68
67
|
silence_redefinition_of_method :to_time
|
69
68
|
|
70
69
|
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
71
|
-
# The timezone can be either
|
70
|
+
# The timezone can be either +:local+ or +:utc+ (default +:local+).
|
72
71
|
#
|
73
72
|
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
74
73
|
#
|
@@ -77,8 +76,8 @@ class Date
|
|
77
76
|
#
|
78
77
|
# date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
|
79
78
|
#
|
80
|
-
# NOTE: The
|
81
|
-
#
|
79
|
+
# NOTE: The +:local+ timezone is Ruby's *process* timezone, i.e. <tt>ENV['TZ']</tt>.
|
80
|
+
# If the <b>application's</b> timezone is needed, then use +in_time_zone+ instead.
|
82
81
|
def to_time(form = :local)
|
83
82
|
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
|
84
83
|
::Time.public_send(form, year, month, day)
|
@@ -4,5 +4,4 @@ require "active_support/core_ext/date/acts_like"
|
|
4
4
|
require "active_support/core_ext/date/blank"
|
5
5
|
require "active_support/core_ext/date/calculations"
|
6
6
|
require "active_support/core_ext/date/conversions"
|
7
|
-
require "active_support/core_ext/date/deprecated_conversions" unless ENV["RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION"]
|
8
7
|
require "active_support/core_ext/date/zones"
|
@@ -157,6 +157,16 @@ module DateAndTime
|
|
157
157
|
end
|
158
158
|
alias :at_end_of_quarter :end_of_quarter
|
159
159
|
|
160
|
+
# Returns the quarter for a date/time.
|
161
|
+
#
|
162
|
+
# Date.new(2010, 1, 31).quarter # => 1
|
163
|
+
# Date.new(2010, 4, 12).quarter # => 2
|
164
|
+
# Date.new(2010, 9, 15).quarter # => 3
|
165
|
+
# Date.new(2010, 12, 25).quarter # => 4
|
166
|
+
def quarter
|
167
|
+
(month / 3.0).ceil
|
168
|
+
end
|
169
|
+
|
160
170
|
# Returns a new date/time at the beginning of the year.
|
161
171
|
#
|
162
172
|
# today = Date.today # => Fri, 10 Jul 2015
|
@@ -201,7 +211,7 @@ module DateAndTime
|
|
201
211
|
end
|
202
212
|
end
|
203
213
|
|
204
|
-
# Short-hand for months_since(3)
|
214
|
+
# Short-hand for <tt>months_since(3)</tt>.
|
205
215
|
def next_quarter
|
206
216
|
months_since(3)
|
207
217
|
end
|
@@ -226,18 +236,18 @@ module DateAndTime
|
|
226
236
|
end
|
227
237
|
alias_method :last_weekday, :prev_weekday
|
228
238
|
|
229
|
-
# Short-hand for months_ago(1)
|
239
|
+
# Short-hand for <tt>months_ago(1)</tt>.
|
230
240
|
def last_month
|
231
241
|
months_ago(1)
|
232
242
|
end
|
233
243
|
|
234
|
-
# Short-hand for months_ago(3)
|
244
|
+
# Short-hand for <tt>months_ago(3)</tt>.
|
235
245
|
def prev_quarter
|
236
246
|
months_ago(3)
|
237
247
|
end
|
238
248
|
alias_method :last_quarter, :prev_quarter
|
239
249
|
|
240
|
-
# Short-hand for years_ago(1)
|
250
|
+
# Short-hand for <tt>years_ago(1)</tt>.
|
241
251
|
def last_year
|
242
252
|
years_ago(1)
|
243
253
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "active_support/core_ext/module/attribute_accessors"
|
4
|
+
require "active_support/core_ext/module/redefine_method"
|
4
5
|
|
5
6
|
module DateAndTime
|
6
7
|
module Compatibility
|
@@ -11,11 +12,37 @@ module DateAndTime
|
|
11
12
|
# of the receiver. For backwards compatibility we're overriding
|
12
13
|
# this behavior, but new apps will have an initializer that sets
|
13
14
|
# this to true, because the new behavior is preferred.
|
14
|
-
mattr_accessor :preserve_timezone,
|
15
|
+
mattr_accessor :preserve_timezone, instance_accessor: false, default: nil
|
16
|
+
|
17
|
+
singleton_class.silence_redefinition_of_method :preserve_timezone
|
18
|
+
|
19
|
+
#--
|
20
|
+
# This re-implements the behaviour of the mattr_reader, instead
|
21
|
+
# of prepending on to it, to avoid overcomplicating a module that
|
22
|
+
# is in turn included in several places. This will all go away in
|
23
|
+
# Rails 8.0 anyway.
|
24
|
+
def self.preserve_timezone # :nodoc:
|
25
|
+
if @@preserve_timezone.nil?
|
26
|
+
# Only warn once, the first time the value is used (which should
|
27
|
+
# be the first time #to_time is called).
|
28
|
+
ActiveSupport.deprecator.warn(
|
29
|
+
"to_time will always preserve the timezone offset of the receiver in Rails 8.0. " \
|
30
|
+
"To opt in to the new behavior, set `ActiveSupport.to_time_preserves_timezone = true`."
|
31
|
+
)
|
32
|
+
|
33
|
+
@@preserve_timezone = false
|
34
|
+
end
|
35
|
+
|
36
|
+
@@preserve_timezone
|
37
|
+
end
|
38
|
+
|
39
|
+
def preserve_timezone # :nodoc:
|
40
|
+
Compatibility.preserve_timezone
|
41
|
+
end
|
15
42
|
|
16
43
|
# Change the output of <tt>ActiveSupport::TimeZone.utc_to_local</tt>.
|
17
44
|
#
|
18
|
-
# When
|
45
|
+
# When +true+, it returns local times with a UTC offset, with +false+ local
|
19
46
|
# times are returned as UTC.
|
20
47
|
#
|
21
48
|
# # Given this zone:
|
@@ -75,6 +75,10 @@ class DateTime
|
|
75
75
|
# The +options+ parameter takes a hash with any of these keys: <tt>:years</tt>,
|
76
76
|
# <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>, <tt>:hours</tt>,
|
77
77
|
# <tt>:minutes</tt>, <tt>:seconds</tt>.
|
78
|
+
#
|
79
|
+
# Just like Date#advance, increments are applied in order of time units from
|
80
|
+
# largest to smallest. This order can affect the result around the end of a
|
81
|
+
# month.
|
78
82
|
def advance(options)
|
79
83
|
unless options[:weeks].nil?
|
80
84
|
options[:weeks], partial_weeks = options[:weeks].divmod(1)
|
@@ -9,21 +9,21 @@ require "active_support/values/time_zone"
|
|
9
9
|
class DateTime
|
10
10
|
# Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
|
11
11
|
#
|
12
|
-
# This method is aliased to <tt>
|
12
|
+
# This method is aliased to <tt>to_formatted_s</tt>.
|
13
13
|
#
|
14
14
|
# === Examples
|
15
15
|
# datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
|
16
16
|
#
|
17
|
-
# datetime.
|
18
|
-
# datetime.
|
19
|
-
# datetime.
|
20
|
-
# datetime.
|
21
|
-
# datetime.
|
22
|
-
# datetime.
|
23
|
-
# datetime.
|
24
|
-
# datetime.
|
17
|
+
# datetime.to_fs(:db) # => "2007-12-04 00:00:00"
|
18
|
+
# datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
|
19
|
+
# datetime.to_fs(:number) # => "20071204000000"
|
20
|
+
# datetime.to_fs(:short) # => "04 Dec 00:00"
|
21
|
+
# datetime.to_fs(:long) # => "December 04, 2007 00:00"
|
22
|
+
# datetime.to_fs(:long_ordinal) # => "December 4th, 2007 00:00"
|
23
|
+
# datetime.to_fs(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
|
24
|
+
# datetime.to_fs(:iso8601) # => "2007-12-04T00:00:00+00:00"
|
25
25
|
#
|
26
|
-
# == Adding your own datetime formats to
|
26
|
+
# == Adding your own datetime formats to to_fs
|
27
27
|
# DateTime formats are shared with Time. You can add your own to the
|
28
28
|
# Time::DATE_FORMATS hash. Use the format name as the hash key and
|
29
29
|
# either a strftime string or Proc instance that takes a time or
|
@@ -32,15 +32,15 @@ class DateTime
|
|
32
32
|
# # config/initializers/time_formats.rb
|
33
33
|
# Time::DATE_FORMATS[:month_and_year] = '%B %Y'
|
34
34
|
# Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
|
35
|
-
def
|
35
|
+
def to_fs(format = :default)
|
36
36
|
if formatter = ::Time::DATE_FORMATS[format]
|
37
37
|
formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
|
38
38
|
else
|
39
|
-
|
39
|
+
to_s
|
40
40
|
end
|
41
41
|
end
|
42
|
-
alias_method :
|
43
|
-
|
42
|
+
alias_method :to_formatted_s, :to_fs
|
43
|
+
|
44
44
|
|
45
45
|
# Returns a formatted string of the offset from UTC, or an alternative
|
46
46
|
# string if the time zone is already UTC.
|
@@ -54,7 +54,7 @@ class DateTime
|
|
54
54
|
|
55
55
|
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000".
|
56
56
|
def readable_inspect
|
57
|
-
|
57
|
+
to_fs(:rfc822)
|
58
58
|
end
|
59
59
|
alias_method :default_inspect, :inspect
|
60
60
|
alias_method :inspect, :readable_inspect
|
@@ -5,4 +5,3 @@ require "active_support/core_ext/date_time/blank"
|
|
5
5
|
require "active_support/core_ext/date_time/calculations"
|
6
6
|
require "active_support/core_ext/date_time/compatibility"
|
7
7
|
require "active_support/core_ext/date_time/conversions"
|
8
|
-
require "active_support/core_ext/date_time/deprecated_conversions" unless ENV["RAILS_DISABLE_DEPRECATED_TO_S_CONVERSION"]
|
@@ -10,8 +10,6 @@ module Digest
|
|
10
10
|
OID_NAMESPACE = "k\xA7\xB8\x12\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" # :nodoc:
|
11
11
|
X500_NAMESPACE = "k\xA7\xB8\x14\x9D\xAD\x11\xD1\x80\xB4\x00\xC0O\xD40\xC8" # :nodoc:
|
12
12
|
|
13
|
-
mattr_accessor :use_rfc4122_namespaced_uuids, instance_accessor: false, default: false
|
14
|
-
|
15
13
|
# Generates a v5 non-random UUID (Universally Unique IDentifier).
|
16
14
|
#
|
17
15
|
# Using OpenSSL::Digest::MD5 generates version 3 UUIDs; OpenSSL::Digest::SHA1 generates version 5 UUIDs.
|
@@ -55,22 +53,21 @@ module Digest
|
|
55
53
|
SecureRandom.uuid
|
56
54
|
end
|
57
55
|
|
56
|
+
# Returns the nil UUID. This is a special form of UUID that is specified to
|
57
|
+
# have all 128 bits set to zero.
|
58
|
+
def self.nil_uuid
|
59
|
+
"00000000-0000-0000-0000-000000000000"
|
60
|
+
end
|
61
|
+
|
58
62
|
def self.pack_uuid_namespace(namespace)
|
59
63
|
if [DNS_NAMESPACE, OID_NAMESPACE, URL_NAMESPACE, X500_NAMESPACE].include?(namespace)
|
60
64
|
namespace
|
61
|
-
|
65
|
+
else
|
62
66
|
match_data = namespace.match(/\A(\h{8})-(\h{4})-(\h{4})-(\h{4})-(\h{4})(\h{8})\z/)
|
63
67
|
|
64
68
|
raise ArgumentError, "Only UUIDs are valid namespace identifiers" unless match_data.present?
|
65
69
|
|
66
70
|
match_data.captures.map { |s| s.to_i(16) }.pack("NnnnnN")
|
67
|
-
else
|
68
|
-
ActiveSupport::Deprecation.warn <<~WARNING.squish
|
69
|
-
Providing a namespace ID that is not one of the constants defined on Digest::UUID generates an incorrect UUID value according to RFC 4122.
|
70
|
-
To enable the correct behavior, set the Rails.application.config.active_support.use_rfc4122_namespaced_uuids configuration option to true.
|
71
|
-
WARNING
|
72
|
-
|
73
|
-
namespace
|
74
71
|
end
|
75
72
|
end
|
76
73
|
|