activesupport 4.2.0 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +366 -232
- data/MIT-LICENSE +2 -2
- data/README.rdoc +4 -5
- data/lib/active_support.rb +17 -7
- data/lib/active_support/all.rb +5 -3
- data/lib/active_support/array_inquirer.rb +48 -0
- data/lib/active_support/backtrace_cleaner.rb +7 -5
- data/lib/active_support/benchmarkable.rb +6 -4
- data/lib/active_support/builder.rb +3 -1
- data/lib/active_support/cache.rb +271 -177
- data/lib/active_support/cache/file_store.rb +41 -35
- data/lib/active_support/cache/mem_cache_store.rb +97 -88
- data/lib/active_support/cache/memory_store.rb +27 -30
- data/lib/active_support/cache/null_store.rb +7 -8
- data/lib/active_support/cache/redis_cache_store.rb +454 -0
- data/lib/active_support/cache/strategy/local_cache.rb +67 -34
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
- data/lib/active_support/callbacks.rb +654 -560
- data/lib/active_support/concern.rb +5 -3
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
- data/lib/active_support/concurrency/share_lock.rb +227 -0
- data/lib/active_support/configurable.rb +8 -5
- data/lib/active_support/core_ext.rb +3 -1
- data/lib/active_support/core_ext/array.rb +9 -6
- data/lib/active_support/core_ext/array/access.rb +29 -1
- data/lib/active_support/core_ext/array/conversions.rb +22 -18
- data/lib/active_support/core_ext/array/extract_options.rb +2 -0
- data/lib/active_support/core_ext/array/grouping.rb +11 -18
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
- data/lib/active_support/core_ext/array/wrap.rb +7 -4
- data/lib/active_support/core_ext/benchmark.rb +3 -1
- data/lib/active_support/core_ext/big_decimal.rb +3 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
- data/lib/active_support/core_ext/class.rb +4 -3
- data/lib/active_support/core_ext/class/attribute.rb +41 -22
- data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
- data/lib/active_support/core_ext/class/subclasses.rb +20 -8
- data/lib/active_support/core_ext/date.rb +6 -4
- data/lib/active_support/core_ext/date/acts_like.rb +3 -1
- data/lib/active_support/core_ext/date/blank.rb +14 -0
- data/lib/active_support/core_ext/date/calculations.rb +11 -9
- data/lib/active_support/core_ext/date/conversions.rb +31 -23
- data/lib/active_support/core_ext/date/zones.rb +4 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +179 -56
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
- data/lib/active_support/core_ext/date_time.rb +7 -4
- data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
- data/lib/active_support/core_ext/date_time/blank.rb +14 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +58 -20
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
- data/lib/active_support/core_ext/digest/uuid.rb +7 -5
- data/lib/active_support/core_ext/enumerable.rb +107 -28
- data/lib/active_support/core_ext/file.rb +3 -1
- data/lib/active_support/core_ext/file/atomic.rb +38 -31
- data/lib/active_support/core_ext/hash.rb +11 -9
- data/lib/active_support/core_ext/hash/compact.rb +24 -15
- data/lib/active_support/core_ext/hash/conversions.rb +63 -43
- data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
- data/lib/active_support/core_ext/hash/except.rb +11 -8
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
- data/lib/active_support/core_ext/hash/keys.rb +33 -27
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +8 -8
- data/lib/active_support/core_ext/hash/transform_values.rb +16 -7
- data/lib/active_support/core_ext/integer.rb +5 -3
- data/lib/active_support/core_ext/integer/inflections.rb +3 -1
- data/lib/active_support/core_ext/integer/multiple.rb +2 -0
- data/lib/active_support/core_ext/integer/time.rb +11 -33
- data/lib/active_support/core_ext/kernel.rb +6 -5
- data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
- data/lib/active_support/core_ext/kernel/concern.rb +5 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +4 -83
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/load_error.rb +3 -22
- data/lib/active_support/core_ext/marshal.rb +13 -10
- data/lib/active_support/core_ext/module.rb +14 -11
- data/lib/active_support/core_ext/module/aliasing.rb +6 -44
- data/lib/active_support/core_ext/module/anonymous.rb +12 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
- data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
- data/lib/active_support/core_ext/module/concerning.rb +11 -12
- data/lib/active_support/core_ext/module/delegation.rb +121 -39
- data/lib/active_support/core_ext/module/deprecation.rb +4 -2
- data/lib/active_support/core_ext/module/introspection.rb +9 -9
- data/lib/active_support/core_ext/module/reachable.rb +5 -2
- data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
- data/lib/active_support/core_ext/module/remove_method.rb +8 -3
- data/lib/active_support/core_ext/name_error.rb +22 -2
- data/lib/active_support/core_ext/numeric.rb +6 -3
- data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
- data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
- data/lib/active_support/core_ext/numeric/time.rb +35 -38
- data/lib/active_support/core_ext/object.rb +14 -13
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +29 -4
- data/lib/active_support/core_ext/object/conversions.rb +6 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
- data/lib/active_support/core_ext/object/duplicable.rb +98 -45
- data/lib/active_support/core_ext/object/inclusion.rb +5 -3
- data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
- data/lib/active_support/core_ext/object/json.rb +49 -19
- data/lib/active_support/core_ext/object/to_param.rb +3 -1
- data/lib/active_support/core_ext/object/to_query.rb +6 -4
- data/lib/active_support/core_ext/object/try.rb +70 -22
- data/lib/active_support/core_ext/object/with_options.rb +16 -3
- data/lib/active_support/core_ext/range.rb +7 -4
- data/lib/active_support/core_ext/range/conversions.rb +27 -7
- data/lib/active_support/core_ext/range/each.rb +19 -17
- data/lib/active_support/core_ext/range/include_range.rb +21 -19
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +2 -0
- data/lib/active_support/core_ext/regexp.rb +6 -0
- data/lib/active_support/core_ext/securerandom.rb +25 -0
- data/lib/active_support/core_ext/string.rb +15 -13
- data/lib/active_support/core_ext/string/access.rb +9 -7
- data/lib/active_support/core_ext/string/behavior.rb +3 -1
- data/lib/active_support/core_ext/string/conversions.rb +8 -5
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +10 -8
- data/lib/active_support/core_ext/string/indent.rb +6 -4
- data/lib/active_support/core_ext/string/inflections.rb +61 -24
- data/lib/active_support/core_ext/string/inquiry.rb +3 -1
- data/lib/active_support/core_ext/string/multibyte.rb +15 -7
- data/lib/active_support/core_ext/string/output_safety.rb +35 -35
- data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
- data/lib/active_support/core_ext/string/strip.rb +4 -5
- data/lib/active_support/core_ext/string/zones.rb +4 -2
- data/lib/active_support/core_ext/time.rb +7 -5
- data/lib/active_support/core_ext/time/acts_like.rb +3 -1
- data/lib/active_support/core_ext/time/calculations.rb +101 -51
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/time/conversions.rb +20 -13
- data/lib/active_support/core_ext/time/zones.rb +41 -7
- data/lib/active_support/core_ext/uri.rb +5 -4
- data/lib/active_support/current_attributes.rb +195 -0
- data/lib/active_support/dependencies.rb +143 -160
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/deprecation.rb +12 -9
- data/lib/active_support/deprecation/behaviors.rb +41 -12
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/instance_delegator.rb +17 -2
- data/lib/active_support/deprecation/method_wrappers.rb +54 -21
- data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
- data/lib/active_support/deprecation/reporting.rb +32 -12
- data/lib/active_support/descendants_tracker.rb +2 -0
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration.rb +326 -30
- data/lib/active_support/duration/iso8601_parser.rb +125 -0
- data/lib/active_support/duration/iso8601_serializer.rb +55 -0
- data/lib/active_support/encrypted_configuration.rb +49 -0
- data/lib/active_support/encrypted_file.rb +99 -0
- data/lib/active_support/evented_file_update_checker.rb +205 -0
- data/lib/active_support/execution_wrapper.rb +128 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +63 -37
- data/lib/active_support/gem_version.rb +4 -2
- data/lib/active_support/gzip.rb +7 -5
- data/lib/active_support/hash_with_indifferent_access.rb +130 -30
- data/lib/active_support/i18n.rb +8 -6
- data/lib/active_support/i18n_railtie.rb +34 -14
- data/lib/active_support/inflections.rb +13 -11
- data/lib/active_support/inflector.rb +7 -5
- data/lib/active_support/inflector/inflections.rb +61 -12
- data/lib/active_support/inflector/methods.rb +161 -136
- data/lib/active_support/inflector/transliterate.rb +48 -27
- data/lib/active_support/json.rb +4 -2
- data/lib/active_support/json/decoding.rb +16 -13
- data/lib/active_support/json/encoding.rb +15 -57
- data/lib/active_support/key_generator.rb +25 -25
- data/lib/active_support/lazy_load_hooks.rb +50 -20
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber.rb +13 -10
- data/lib/active_support/log_subscriber/test_helper.rb +14 -12
- data/lib/active_support/logger.rb +54 -3
- data/lib/active_support/logger_silence.rb +12 -7
- data/lib/active_support/logger_thread_safe_level.rb +33 -0
- data/lib/active_support/message_encryptor.rb +173 -51
- data/lib/active_support/message_verifier.rb +150 -17
- data/lib/active_support/messages/metadata.rb +71 -0
- data/lib/active_support/messages/rotation_configuration.rb +22 -0
- data/lib/active_support/messages/rotator.rb +56 -0
- data/lib/active_support/multibyte.rb +4 -2
- data/lib/active_support/multibyte/chars.rb +37 -24
- data/lib/active_support/multibyte/unicode.rb +100 -96
- data/lib/active_support/notifications.rb +11 -7
- data/lib/active_support/notifications/fanout.rb +10 -8
- data/lib/active_support/notifications/instrumenter.rb +27 -7
- data/lib/active_support/number_helper.rb +94 -68
- data/lib/active_support/number_helper/number_converter.rb +13 -11
- data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
- data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
- data/lib/active_support/number_helper/rounding_helper.rb +66 -0
- data/lib/active_support/option_merger.rb +3 -1
- data/lib/active_support/ordered_hash.rb +6 -4
- data/lib/active_support/ordered_options.rb +22 -4
- data/lib/active_support/per_thread_registry.rb +13 -6
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +16 -8
- data/lib/active_support/railtie.rb +43 -9
- data/lib/active_support/reloader.rb +131 -0
- data/lib/active_support/rescuable.rb +108 -53
- data/lib/active_support/security_utils.rb +17 -6
- data/lib/active_support/string_inquirer.rb +11 -3
- data/lib/active_support/subscriber.rb +15 -14
- data/lib/active_support/tagged_logging.rb +14 -11
- data/lib/active_support/test_case.rb +18 -46
- data/lib/active_support/testing/assertions.rb +137 -20
- data/lib/active_support/testing/autorun.rb +4 -2
- data/lib/active_support/testing/constant_lookup.rb +2 -1
- data/lib/active_support/testing/declarative.rb +3 -1
- data/lib/active_support/testing/deprecation.rb +14 -10
- data/lib/active_support/testing/file_fixtures.rb +36 -0
- data/lib/active_support/testing/isolation.rb +34 -25
- data/lib/active_support/testing/method_call_assertions.rb +43 -0
- data/lib/active_support/testing/setup_and_teardown.rb +12 -3
- data/lib/active_support/testing/stream.rb +44 -0
- data/lib/active_support/testing/tagged_logging.rb +3 -1
- data/lib/active_support/testing/time_helpers.rb +96 -27
- data/lib/active_support/time.rb +14 -12
- data/lib/active_support/time_with_zone.rb +195 -53
- data/lib/active_support/values/time_zone.rb +200 -61
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +3 -1
- data/lib/active_support/xml_mini.rb +69 -51
- data/lib/active_support/xml_mini/jdom.rb +116 -113
- data/lib/active_support/xml_mini/libxml.rb +17 -16
- data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
- data/lib/active_support/xml_mini/nokogiri.rb +15 -15
- data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
- data/lib/active_support/xml_mini/rexml.rb +17 -16
- metadata +55 -43
- data/lib/active_support/concurrency/latch.rb +0 -27
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
- data/lib/active_support/core_ext/date_time/zones.rb +0 -6
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
- data/lib/active_support/core_ext/module/method_transplanting.rb +0 -11
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- data/lib/active_support/core_ext/struct.rb +0 -6
- data/lib/active_support/core_ext/thread.rb +0 -86
- data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Array
|
2
4
|
# Splits or iterates over the array in groups of size +number+,
|
3
5
|
# padding any remaining slots with +fill_with+ unless it is +false+.
|
@@ -89,28 +91,19 @@ class Array
|
|
89
91
|
# [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]]
|
90
92
|
# (1..10).to_a.split { |i| i % 3 == 0 } # => [[1, 2], [4, 5], [7, 8], [10]]
|
91
93
|
def split(value = nil)
|
94
|
+
arr = dup
|
95
|
+
result = []
|
92
96
|
if block_given?
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
else
|
97
|
-
results.last << element
|
98
|
-
end
|
99
|
-
|
100
|
-
results
|
97
|
+
while (idx = arr.index { |i| yield i })
|
98
|
+
result << arr.shift(idx)
|
99
|
+
arr.shift
|
101
100
|
end
|
102
101
|
else
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
results.last.concat(arr.shift(idx))
|
107
|
-
arr.shift
|
108
|
-
results << []
|
109
|
-
else
|
110
|
-
results.last.concat(arr.shift(arr.size))
|
111
|
-
end
|
102
|
+
while (idx = arr.index(value))
|
103
|
+
result << arr.shift(idx)
|
104
|
+
arr.shift
|
112
105
|
end
|
113
|
-
results
|
114
106
|
end
|
107
|
+
result << arr
|
115
108
|
end
|
116
109
|
end
|
@@ -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 +ArrayInquirer+ object, which gives a friendlier way
|
7
|
+
# 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
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Array
|
2
4
|
# The human way of thinking about adding stuff to the end of a list is with append.
|
3
|
-
alias_method :append,
|
5
|
+
alias_method :append, :push unless [].respond_to?(:append)
|
4
6
|
|
5
7
|
# The human way of thinking about adding stuff to the beginning of a list is with prepend.
|
6
|
-
alias_method :prepend, :unshift
|
7
|
-
end
|
8
|
+
alias_method :prepend, :unshift unless [].respond_to?(:prepend)
|
9
|
+
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Array
|
2
4
|
# Wraps its argument in an array unless it is already an array (or array-like).
|
3
5
|
#
|
4
6
|
# Specifically:
|
5
7
|
#
|
6
|
-
# * If the argument is +nil+ an empty
|
8
|
+
# * If the argument is +nil+ an empty array is returned.
|
7
9
|
# * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
|
8
10
|
# * Otherwise, returns an array with the argument as its single element.
|
9
11
|
#
|
@@ -15,12 +17,13 @@ class Array
|
|
15
17
|
#
|
16
18
|
# * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
|
17
19
|
# moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
|
18
|
-
#
|
20
|
+
# an array with the argument as its single element right away.
|
19
21
|
# * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
|
20
22
|
# raises an exception, while <tt>Array.wrap</tt> does not, it just returns the value.
|
21
|
-
# * It does not call +to_a+ on the argument,
|
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.
|
22
25
|
#
|
23
|
-
# The
|
26
|
+
# The last point is easily explained with some enumerables:
|
24
27
|
#
|
25
28
|
# Array(foo: :bar) # => [[:foo, :bar]]
|
26
29
|
# Array.wrap(foo: :bar) # => [{:foo=>:bar}]
|
@@ -1,16 +1,14 @@
|
|
1
|
-
|
2
|
-
require 'bigdecimal/util'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
format
|
11
|
-
_original_to_s(format)
|
3
|
+
require "bigdecimal"
|
4
|
+
require "bigdecimal/util"
|
5
|
+
|
6
|
+
module ActiveSupport
|
7
|
+
module BigDecimalWithDefaultFormat #:nodoc:
|
8
|
+
def to_s(format = "F")
|
9
|
+
super(format)
|
12
10
|
end
|
13
11
|
end
|
14
|
-
alias_method :_original_to_s, :to_s
|
15
|
-
alias_method :to_s, :to_formatted_s
|
16
12
|
end
|
13
|
+
|
14
|
+
BigDecimal.prepend(ActiveSupport::BigDecimalWithDefaultFormat)
|
@@ -1,3 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/class/attribute"
|
4
|
+
require "active_support/core_ext/class/subclasses"
|
@@ -1,11 +1,23 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/kernel/singleton_class"
|
4
|
+
require "active_support/core_ext/module/redefine_method"
|
5
|
+
require "active_support/core_ext/array/extract_options"
|
4
6
|
|
5
7
|
class Class
|
6
8
|
# Declare a class-level attribute whose value is inheritable by subclasses.
|
7
9
|
# Subclasses can change their own value and it will not impact parent class.
|
8
10
|
#
|
11
|
+
# ==== Options
|
12
|
+
#
|
13
|
+
# * <tt>:instance_reader</tt> - Sets the instance reader method (defaults to true).
|
14
|
+
# * <tt>:instance_writer</tt> - Sets the instance writer method (defaults to true).
|
15
|
+
# * <tt>:instance_accessor</tt> - Sets both instance methods (defaults to true).
|
16
|
+
# * <tt>:instance_predicate</tt> - Sets a predicate method (defaults to true).
|
17
|
+
# * <tt>:default</tt> - Sets a default value for the attribute (defaults to nil).
|
18
|
+
#
|
19
|
+
# ==== Examples
|
20
|
+
#
|
9
21
|
# class Base
|
10
22
|
# class_attribute :setting
|
11
23
|
# end
|
@@ -20,14 +32,14 @@ class Class
|
|
20
32
|
# Base.setting # => true
|
21
33
|
#
|
22
34
|
# In the above case as long as Subclass does not assign a value to setting
|
23
|
-
# by performing <tt>Subclass.setting = _something_
|
35
|
+
# by performing <tt>Subclass.setting = _something_</tt>, <tt>Subclass.setting</tt>
|
24
36
|
# would read value assigned to parent class. Once Subclass assigns a value then
|
25
37
|
# the value assigned by Subclass would be returned.
|
26
38
|
#
|
27
39
|
# This matches normal Ruby method inheritance: think of writing an attribute
|
28
40
|
# on a subclass as overriding the reader method. However, you need to be aware
|
29
41
|
# when using +class_attribute+ with mutable structures as +Array+ or +Hash+.
|
30
|
-
# In such cases, you don't want to do changes in
|
42
|
+
# In such cases, you don't want to do changes in place. Instead use setters:
|
31
43
|
#
|
32
44
|
# Base.setting = []
|
33
45
|
# Base.setting # => []
|
@@ -68,28 +80,35 @@ class Class
|
|
68
80
|
# object.setting = false # => NoMethodError
|
69
81
|
#
|
70
82
|
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
|
83
|
+
#
|
84
|
+
# To set a default value for the attribute, pass <tt>default:</tt>, like so:
|
85
|
+
#
|
86
|
+
# class_attribute :settings, default: {}
|
71
87
|
def class_attribute(*attrs)
|
72
88
|
options = attrs.extract_options!
|
73
|
-
instance_reader
|
74
|
-
instance_writer
|
89
|
+
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
|
90
|
+
instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
|
75
91
|
instance_predicate = options.fetch(:instance_predicate, true)
|
92
|
+
default_value = options.fetch(:default, nil)
|
76
93
|
|
77
94
|
attrs.each do |name|
|
95
|
+
singleton_class.silence_redefinition_of_method(name)
|
78
96
|
define_singleton_method(name) { nil }
|
97
|
+
|
98
|
+
singleton_class.silence_redefinition_of_method("#{name}?")
|
79
99
|
define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate
|
80
100
|
|
81
101
|
ivar = "@#{name}"
|
82
102
|
|
103
|
+
singleton_class.silence_redefinition_of_method("#{name}=")
|
83
104
|
define_singleton_method("#{name}=") do |val|
|
84
105
|
singleton_class.class_eval do
|
85
|
-
|
86
|
-
define_method(name) { val }
|
106
|
+
redefine_method(name) { val }
|
87
107
|
end
|
88
108
|
|
89
109
|
if singleton_class?
|
90
110
|
class_eval do
|
91
|
-
|
92
|
-
define_method(name) do
|
111
|
+
redefine_method(name) do
|
93
112
|
if instance_variable_defined? ivar
|
94
113
|
instance_variable_get ivar
|
95
114
|
else
|
@@ -102,26 +121,26 @@ class Class
|
|
102
121
|
end
|
103
122
|
|
104
123
|
if instance_reader
|
105
|
-
|
106
|
-
define_method(name) do
|
124
|
+
redefine_method(name) do
|
107
125
|
if instance_variable_defined?(ivar)
|
108
126
|
instance_variable_get ivar
|
109
127
|
else
|
110
128
|
self.class.public_send name
|
111
129
|
end
|
112
130
|
end
|
113
|
-
define_method("#{name}?") { !!public_send(name) } if instance_predicate
|
114
|
-
end
|
115
131
|
|
116
|
-
|
117
|
-
|
118
|
-
end
|
132
|
+
redefine_method("#{name}?") { !!public_send(name) } if instance_predicate
|
133
|
+
end
|
119
134
|
|
120
|
-
|
135
|
+
if instance_writer
|
136
|
+
redefine_method("#{name}=") do |val|
|
137
|
+
instance_variable_set ivar, val
|
138
|
+
end
|
139
|
+
end
|
121
140
|
|
122
|
-
|
123
|
-
|
124
|
-
ancestors.first != self
|
141
|
+
unless default_value.nil?
|
142
|
+
self.send("#{name}=", default_value)
|
125
143
|
end
|
126
144
|
end
|
145
|
+
end
|
127
146
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# cattr_* became mattr_* aliases in 7dfbd91b0780fbd6a1dd9bfbc176e10894871d2d,
|
2
4
|
# but we keep this around for libraries that directly require it knowing they
|
3
5
|
# want cattr_*. No need to deprecate.
|
4
|
-
require
|
6
|
+
require "active_support/core_ext/module/attribute_accessors"
|
@@ -1,19 +1,33 @@
|
|
1
|
-
|
2
|
-
require 'active_support/core_ext/module/reachable'
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
class Class
|
5
4
|
begin
|
6
|
-
|
5
|
+
# Test if this Ruby supports each_object against singleton_class
|
6
|
+
ObjectSpace.each_object(Numeric.singleton_class) {}
|
7
7
|
|
8
|
-
|
8
|
+
# Returns an array with all classes that are < than its receiver.
|
9
|
+
#
|
10
|
+
# class C; end
|
11
|
+
# C.descendants # => []
|
12
|
+
#
|
13
|
+
# class B < C; end
|
14
|
+
# C.descendants # => [B]
|
15
|
+
#
|
16
|
+
# class A < B; end
|
17
|
+
# C.descendants # => [B, A]
|
18
|
+
#
|
19
|
+
# class D < C; end
|
20
|
+
# C.descendants # => [B, A, D]
|
21
|
+
def descendants
|
9
22
|
descendants = []
|
10
23
|
ObjectSpace.each_object(singleton_class) do |k|
|
24
|
+
next if k.singleton_class?
|
11
25
|
descendants.unshift k unless k == self
|
12
26
|
end
|
13
27
|
descendants
|
14
28
|
end
|
15
|
-
rescue StandardError # JRuby
|
16
|
-
def descendants
|
29
|
+
rescue StandardError # JRuby 9.0.4.0 and earlier
|
30
|
+
def descendants
|
17
31
|
descendants = []
|
18
32
|
ObjectSpace.each_object(Class) do |k|
|
19
33
|
descendants.unshift k if k < self
|
@@ -25,8 +39,6 @@ class Class
|
|
25
39
|
|
26
40
|
# Returns an array with the direct children of +self+.
|
27
41
|
#
|
28
|
-
# Integer.subclasses # => [Fixnum, Bignum]
|
29
|
-
#
|
30
42
|
# class Foo; end
|
31
43
|
# class Bar < Foo; end
|
32
44
|
# class Baz < Bar; end
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
require 'active_support/core_ext/date/calculations'
|
3
|
-
require 'active_support/core_ext/date/conversions'
|
4
|
-
require 'active_support/core_ext/date/zones'
|
1
|
+
# frozen_string_literal: true
|
5
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"
|
@@ -1,9 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
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"
|
7
9
|
|
8
10
|
class Date
|
9
11
|
include DateAndTime::Calculations
|
@@ -26,7 +28,7 @@ class Date
|
|
26
28
|
Thread.current[:beginning_of_week] = find_beginning_of_week!(week_start)
|
27
29
|
end
|
28
30
|
|
29
|
-
# Returns week start day symbol (e.g. :monday), or raises an ArgumentError for invalid day symbol.
|
31
|
+
# Returns week start day symbol (e.g. :monday), or raises an +ArgumentError+ for invalid day symbol.
|
30
32
|
def find_beginning_of_week!(week_start)
|
31
33
|
raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
|
32
34
|
week_start
|
@@ -129,11 +131,11 @@ class Date
|
|
129
131
|
options.fetch(:day, day)
|
130
132
|
)
|
131
133
|
end
|
132
|
-
|
134
|
+
|
133
135
|
# Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there.
|
134
136
|
def compare_with_coercion(other)
|
135
137
|
if other.is_a?(Time)
|
136
|
-
|
138
|
+
to_datetime <=> other
|
137
139
|
else
|
138
140
|
compare_without_coercion(other)
|
139
141
|
end
|
@@ -1,30 +1,24 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
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"
|
5
7
|
|
6
8
|
class Date
|
7
9
|
DATE_FORMATS = {
|
8
|
-
:
|
9
|
-
:
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
10
|
+
short: "%d %b",
|
11
|
+
long: "%B %d, %Y",
|
12
|
+
db: "%Y-%m-%d",
|
13
|
+
number: "%Y%m%d",
|
14
|
+
long_ordinal: lambda { |date|
|
13
15
|
day_format = ActiveSupport::Inflector.ordinalize(date.day)
|
14
16
|
date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
|
15
17
|
},
|
16
|
-
:
|
17
|
-
:
|
18
|
+
rfc822: "%d %b %Y",
|
19
|
+
iso8601: lambda { |date| date.iso8601 }
|
18
20
|
}
|
19
21
|
|
20
|
-
# Ruby 1.9 has Date#to_time which converts to localtime only.
|
21
|
-
remove_method :to_time
|
22
|
-
|
23
|
-
# Ruby 1.9 has Date#xmlschema which converts to a string without the time
|
24
|
-
# component. This removal may generate an issue on FreeBSD, that's why we
|
25
|
-
# need to use remove_possible_method here
|
26
|
-
remove_possible_method :xmlschema
|
27
|
-
|
28
22
|
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
|
29
23
|
#
|
30
24
|
# This method is aliased to <tt>to_s</tt>.
|
@@ -35,6 +29,7 @@ class Date
|
|
35
29
|
# date.to_s(:db) # => "2007-11-10"
|
36
30
|
#
|
37
31
|
# date.to_formatted_s(:short) # => "10 Nov"
|
32
|
+
# date.to_formatted_s(:number) # => "20071110"
|
38
33
|
# date.to_formatted_s(:long) # => "November 10, 2007"
|
39
34
|
# date.to_formatted_s(:long_ordinal) # => "November 10th, 2007"
|
40
35
|
# date.to_formatted_s(:rfc822) # => "10 Nov 2007"
|
@@ -64,24 +59,37 @@ class Date
|
|
64
59
|
|
65
60
|
# Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
|
66
61
|
def readable_inspect
|
67
|
-
strftime(
|
62
|
+
strftime("%a, %d %b %Y")
|
68
63
|
end
|
69
64
|
alias_method :default_inspect, :inspect
|
70
65
|
alias_method :inspect, :readable_inspect
|
71
66
|
|
67
|
+
silence_redefinition_of_method :to_time
|
68
|
+
|
72
69
|
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
73
70
|
# The timezone can be either :local or :utc (default :local).
|
74
71
|
#
|
75
72
|
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
|
76
73
|
#
|
77
|
-
# date.to_time # =>
|
78
|
-
# date.to_time(:local) # =>
|
74
|
+
# date.to_time # => 2007-11-10 00:00:00 0800
|
75
|
+
# date.to_time(:local) # => 2007-11-10 00:00:00 0800
|
79
76
|
#
|
80
|
-
# date.to_time(:utc) # =>
|
77
|
+
# date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
|
78
|
+
#
|
79
|
+
# NOTE: The :local timezone is Ruby's *process* timezone, i.e. ENV['TZ'].
|
80
|
+
# If the *application's* timezone is needed, then use +in_time_zone+ instead.
|
81
81
|
def to_time(form = :local)
|
82
|
+
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
|
82
83
|
::Time.send(form, year, month, day)
|
83
84
|
end
|
84
85
|
|
86
|
+
silence_redefinition_of_method :xmlschema
|
87
|
+
|
88
|
+
# Returns a string which represents the time in used time zone as DateTime
|
89
|
+
# defined by XML Schema:
|
90
|
+
#
|
91
|
+
# date = Date.new(2015, 05, 23) # => Sat, 23 May 2015
|
92
|
+
# date.xmlschema # => "2015-05-23T00:00:00+04:00"
|
85
93
|
def xmlschema
|
86
94
|
in_time_zone.xmlschema
|
87
95
|
end
|