activesupport 5.1.1 → 6.1.1
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 +360 -442
- data/MIT-LICENSE +1 -1
- data/README.rdoc +5 -4
- data/lib/active_support/actionable_error.rb +48 -0
- data/lib/active_support/all.rb +2 -0
- data/lib/active_support/array_inquirer.rb +6 -2
- data/lib/active_support/backtrace_cleaner.rb +31 -3
- data/lib/active_support/benchmarkable.rb +3 -1
- data/lib/active_support/builder.rb +2 -0
- data/lib/active_support/cache/file_store.rb +37 -36
- data/lib/active_support/cache/mem_cache_store.rb +65 -53
- data/lib/active_support/cache/memory_store.rb +61 -33
- data/lib/active_support/cache/null_store.rb +10 -3
- data/lib/active_support/cache/redis_cache_store.rb +493 -0
- data/lib/active_support/cache/strategy/local_cache.rb +68 -22
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +2 -0
- data/lib/active_support/cache.rb +305 -127
- data/lib/active_support/callbacks.rb +106 -98
- data/lib/active_support/concern.rb +79 -6
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +35 -0
- data/lib/active_support/concurrency/share_lock.rb +2 -1
- data/lib/active_support/configurable.rb +12 -14
- data/lib/active_support/configuration_file.rb +46 -0
- data/lib/active_support/core_ext/array/access.rb +21 -7
- data/lib/active_support/core_ext/array/conversions.rb +7 -5
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/extract_options.rb +2 -0
- data/lib/active_support/core_ext/array/grouping.rb +2 -0
- data/lib/active_support/core_ext/array/inquiry.rb +2 -0
- data/lib/active_support/core_ext/array/wrap.rb +2 -0
- data/lib/active_support/core_ext/array.rb +3 -1
- data/lib/active_support/core_ext/benchmark.rb +4 -2
- data/lib/active_support/core_ext/big_decimal/conversions.rb +2 -0
- data/lib/active_support/core_ext/big_decimal.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +50 -47
- data/lib/active_support/core_ext/class/attribute_accessors.rb +2 -0
- data/lib/active_support/core_ext/class/subclasses.rb +18 -40
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/date/acts_like.rb +2 -0
- data/lib/active_support/core_ext/date/blank.rb +2 -0
- data/lib/active_support/core_ext/date/calculations.rb +8 -5
- data/lib/active_support/core_ext/date/conversions.rb +12 -10
- data/lib/active_support/core_ext/date/zones.rb +2 -0
- data/lib/active_support/core_ext/date.rb +2 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +61 -37
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -1
- data/lib/active_support/core_ext/date_and_time/zones.rb +2 -1
- data/lib/active_support/core_ext/date_time/acts_like.rb +2 -0
- data/lib/active_support/core_ext/date_time/blank.rb +2 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +3 -1
- data/lib/active_support/core_ext/date_time/compatibility.rb +7 -5
- data/lib/active_support/core_ext/date_time/conversions.rb +2 -1
- data/lib/active_support/core_ext/date_time.rb +2 -0
- data/lib/active_support/core_ext/digest/uuid.rb +3 -1
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +174 -71
- data/lib/active_support/core_ext/file/atomic.rb +3 -1
- data/lib/active_support/core_ext/file.rb +2 -0
- data/lib/active_support/core_ext/hash/conversions.rb +7 -5
- data/lib/active_support/core_ext/hash/deep_merge.rb +8 -12
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +4 -2
- data/lib/active_support/core_ext/hash/indifferent_access.rb +2 -0
- data/lib/active_support/core_ext/hash/keys.rb +3 -30
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +8 -29
- data/lib/active_support/core_ext/hash.rb +3 -2
- data/lib/active_support/core_ext/integer/inflections.rb +2 -0
- data/lib/active_support/core_ext/integer/multiple.rb +3 -1
- data/lib/active_support/core_ext/integer/time.rb +7 -14
- data/lib/active_support/core_ext/integer.rb +2 -0
- data/lib/active_support/core_ext/kernel/concern.rb +2 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +2 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/kernel.rb +2 -1
- data/lib/active_support/core_ext/load_error.rb +3 -8
- data/lib/active_support/core_ext/marshal.rb +4 -0
- data/lib/active_support/core_ext/module/aliasing.rb +2 -0
- data/lib/active_support/core_ext/module/anonymous.rb +2 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +4 -2
- data/lib/active_support/core_ext/module/attribute_accessors.rb +44 -56
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +18 -18
- data/lib/active_support/core_ext/module/concerning.rb +15 -10
- data/lib/active_support/core_ext/module/delegation.rb +103 -58
- data/lib/active_support/core_ext/module/deprecation.rb +2 -0
- data/lib/active_support/core_ext/module/introspection.rb +18 -15
- data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
- data/lib/active_support/core_ext/module/remove_method.rb +5 -23
- data/lib/active_support/core_ext/module.rb +3 -1
- data/lib/active_support/core_ext/name_error.rb +36 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +2 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +131 -129
- data/lib/active_support/core_ext/numeric/time.rb +7 -15
- data/lib/active_support/core_ext/numeric.rb +2 -1
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +13 -3
- data/lib/active_support/core_ext/object/conversions.rb +2 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +3 -1
- data/lib/active_support/core_ext/object/duplicable.rb +6 -101
- data/lib/active_support/core_ext/object/inclusion.rb +2 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +2 -0
- data/lib/active_support/core_ext/object/json.rb +22 -2
- data/lib/active_support/core_ext/object/to_param.rb +2 -0
- data/lib/active_support/core_ext/object/to_query.rb +7 -2
- data/lib/active_support/core_ext/object/try.rb +19 -7
- data/lib/active_support/core_ext/object/with_options.rb +4 -2
- data/lib/active_support/core_ext/object.rb +2 -0
- data/lib/active_support/core_ext/range/compare_range.rb +82 -0
- data/lib/active_support/core_ext/range/conversions.rb +35 -25
- data/lib/active_support/core_ext/range/each.rb +5 -2
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +28 -0
- data/lib/active_support/core_ext/range/overlaps.rb +2 -0
- data/lib/active_support/core_ext/range.rb +4 -1
- data/lib/active_support/core_ext/regexp.rb +10 -5
- data/lib/active_support/core_ext/securerandom.rb +25 -3
- data/lib/active_support/core_ext/string/access.rb +7 -16
- data/lib/active_support/core_ext/string/behavior.rb +2 -0
- data/lib/active_support/core_ext/string/conversions.rb +3 -0
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +44 -1
- data/lib/active_support/core_ext/string/indent.rb +2 -0
- data/lib/active_support/core_ext/string/inflections.rb +69 -16
- data/lib/active_support/core_ext/string/inquiry.rb +3 -0
- data/lib/active_support/core_ext/string/multibyte.rb +9 -4
- data/lib/active_support/core_ext/string/output_safety.rb +76 -20
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
- data/lib/active_support/core_ext/string/strip.rb +5 -1
- data/lib/active_support/core_ext/string/zones.rb +2 -0
- data/lib/active_support/core_ext/string.rb +2 -0
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +14 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/time/acts_like.rb +2 -0
- data/lib/active_support/core_ext/time/calculations.rb +73 -18
- data/lib/active_support/core_ext/time/compatibility.rb +4 -2
- data/lib/active_support/core_ext/time/conversions.rb +4 -0
- data/lib/active_support/core_ext/time/zones.rb +6 -4
- data/lib/active_support/core_ext/time.rb +2 -0
- data/lib/active_support/core_ext/uri.rb +11 -6
- data/lib/active_support/core_ext.rb +3 -1
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +208 -0
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +2 -0
- data/lib/active_support/dependencies/zeitwerk_integration.rb +117 -0
- data/lib/active_support/dependencies.rb +135 -60
- data/lib/active_support/deprecation/behaviors.rb +43 -11
- data/lib/active_support/deprecation/constant_accessor.rb +4 -2
- data/lib/active_support/deprecation/disallowed.rb +56 -0
- data/lib/active_support/deprecation/instance_delegator.rb +2 -1
- data/lib/active_support/deprecation/method_wrappers.rb +30 -15
- data/lib/active_support/deprecation/proxy_wrappers.rb +32 -6
- data/lib/active_support/deprecation/reporting.rb +54 -9
- data/lib/active_support/deprecation.rb +9 -2
- data/lib/active_support/descendants_tracker.rb +61 -9
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration/iso8601_parser.rb +6 -6
- data/lib/active_support/duration/iso8601_serializer.rb +20 -14
- data/lib/active_support/duration.rb +179 -41
- data/lib/active_support/encrypted_configuration.rb +45 -0
- data/lib/active_support/encrypted_file.rb +117 -0
- data/lib/active_support/environment_inquirer.rb +20 -0
- data/lib/active_support/evented_file_update_checker.rb +84 -117
- data/lib/active_support/execution_wrapper.rb +3 -0
- data/lib/active_support/executor.rb +2 -0
- data/lib/active_support/file_update_checker.rb +2 -1
- data/lib/active_support/fork_tracker.rb +62 -0
- data/lib/active_support/gem_version.rb +3 -1
- data/lib/active_support/gzip.rb +2 -0
- data/lib/active_support/hash_with_indifferent_access.rb +134 -37
- data/lib/active_support/i18n.rb +4 -1
- data/lib/active_support/i18n_railtie.rb +20 -11
- data/lib/active_support/inflections.rb +2 -0
- data/lib/active_support/inflector/inflections.rb +19 -8
- data/lib/active_support/inflector/methods.rb +87 -77
- data/lib/active_support/inflector/transliterate.rb +56 -18
- data/lib/active_support/inflector.rb +2 -0
- data/lib/active_support/json/decoding.rb +27 -26
- data/lib/active_support/json/encoding.rb +13 -3
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/key_generator.rb +3 -33
- data/lib/active_support/lazy_load_hooks.rb +33 -10
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +7 -3
- data/lib/active_support/log_subscriber/test_helper.rb +2 -0
- data/lib/active_support/log_subscriber.rb +46 -13
- data/lib/active_support/logger.rb +4 -17
- data/lib/active_support/logger_silence.rb +13 -20
- data/lib/active_support/logger_thread_safe_level.rb +54 -7
- data/lib/active_support/message_encryptor.rb +101 -33
- data/lib/active_support/message_verifier.rb +85 -14
- data/lib/active_support/messages/metadata.rb +80 -0
- data/lib/active_support/messages/rotation_configuration.rb +23 -0
- data/lib/active_support/messages/rotator.rb +57 -0
- data/lib/active_support/multibyte/chars.rb +12 -68
- data/lib/active_support/multibyte/unicode.rb +17 -327
- data/lib/active_support/multibyte.rb +2 -0
- data/lib/active_support/notifications/fanout.rb +118 -16
- data/lib/active_support/notifications/instrumenter.rb +73 -9
- data/lib/active_support/notifications.rb +74 -8
- data/lib/active_support/number_helper/number_converter.rb +7 -6
- data/lib/active_support/number_helper/number_to_currency_converter.rb +6 -9
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +5 -2
- data/lib/active_support/number_helper/number_to_human_converter.rb +8 -7
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +6 -3
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +5 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +5 -2
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +16 -53
- data/lib/active_support/number_helper/rounding_helper.rb +50 -0
- data/lib/active_support/number_helper.rb +41 -12
- data/lib/active_support/option_merger.rb +24 -3
- data/lib/active_support/ordered_hash.rb +3 -1
- data/lib/active_support/ordered_options.rb +17 -5
- data/lib/active_support/parameter_filter.rb +133 -0
- data/lib/active_support/per_thread_registry.rb +3 -1
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +3 -10
- data/lib/active_support/railtie.rb +60 -9
- data/lib/active_support/reloader.rb +11 -10
- data/lib/active_support/rescuable.rb +7 -6
- data/lib/active_support/secure_compare_rotator.rb +51 -0
- data/lib/active_support/security_utils.rb +26 -15
- data/lib/active_support/string_inquirer.rb +6 -3
- data/lib/active_support/subscriber.rb +74 -24
- data/lib/active_support/tagged_logging.rb +44 -8
- data/lib/active_support/test_case.rb +94 -2
- data/lib/active_support/testing/assertions.rb +58 -20
- data/lib/active_support/testing/autorun.rb +2 -4
- data/lib/active_support/testing/constant_lookup.rb +2 -0
- data/lib/active_support/testing/declarative.rb +2 -0
- data/lib/active_support/testing/deprecation.rb +2 -1
- data/lib/active_support/testing/file_fixtures.rb +4 -0
- data/lib/active_support/testing/isolation.rb +8 -4
- data/lib/active_support/testing/method_call_assertions.rb +30 -1
- data/lib/active_support/testing/parallelization/server.rb +78 -0
- data/lib/active_support/testing/parallelization/worker.rb +100 -0
- data/lib/active_support/testing/parallelization.rb +51 -0
- data/lib/active_support/testing/setup_and_teardown.rb +12 -7
- data/lib/active_support/testing/stream.rb +3 -2
- data/lib/active_support/testing/tagged_logging.rb +2 -0
- data/lib/active_support/testing/time_helpers.rb +78 -13
- data/lib/active_support/time.rb +2 -0
- data/lib/active_support/time_with_zone.rb +113 -41
- data/lib/active_support/values/time_zone.rb +55 -25
- data/lib/active_support/version.rb +2 -0
- data/lib/active_support/xml_mini/jdom.rb +5 -4
- data/lib/active_support/xml_mini/libxml.rb +4 -2
- data/lib/active_support/xml_mini/libxmlsax.rb +6 -4
- data/lib/active_support/xml_mini/nokogiri.rb +4 -2
- data/lib/active_support/xml_mini/nokogirisax.rb +5 -3
- data/lib/active_support/xml_mini/rexml.rb +12 -3
- data/lib/active_support/xml_mini.rb +5 -11
- data/lib/active_support.rb +18 -13
- metadata +81 -35
- data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
- data/lib/active_support/core_ext/hash/compact.rb +0 -27
- data/lib/active_support/core_ext/hash/transform_values.rb +0 -30
- data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
- data/lib/active_support/core_ext/module/reachable.rb +0 -8
- data/lib/active_support/core_ext/numeric/inquiry.rb +0 -26
- data/lib/active_support/core_ext/range/include_range.rb +0 -23
- data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
# Reads a YAML configuration file, evaluating any ERB, then
|
5
|
+
# parsing the resulting YAML.
|
6
|
+
#
|
7
|
+
# Warns in case of YAML confusing characters, like invisible
|
8
|
+
# non-breaking spaces.
|
9
|
+
class ConfigurationFile # :nodoc:
|
10
|
+
class FormatError < StandardError; end
|
11
|
+
|
12
|
+
def initialize(content_path)
|
13
|
+
@content_path = content_path.to_s
|
14
|
+
@content = read content_path
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.parse(content_path, **options)
|
18
|
+
new(content_path).parse(**options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse(context: nil, **options)
|
22
|
+
YAML.load(render(context), **options) || {}
|
23
|
+
rescue Psych::SyntaxError => error
|
24
|
+
raise "YAML syntax error occurred while parsing #{@content_path}. " \
|
25
|
+
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
|
26
|
+
"Error: #{error.message}"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def read(content_path)
|
31
|
+
require "yaml"
|
32
|
+
require "erb"
|
33
|
+
|
34
|
+
File.read(content_path).tap do |content|
|
35
|
+
if content.include?("\u00A0")
|
36
|
+
warn "File contains invisible non-breaking spaces, you may want to remove those"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def render(context)
|
42
|
+
erb = ERB.new(@content).tap { |e| e.filename = @content_path }
|
43
|
+
context ? erb.result(context) : erb.result
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Array
|
2
4
|
# Returns the tail of the array from +position+.
|
3
5
|
#
|
@@ -27,16 +29,28 @@ class Array
|
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
|
-
# Returns a
|
32
|
+
# Returns a new array that includes the passed elements.
|
33
|
+
#
|
34
|
+
# [ 1, 2, 3 ].including(4, 5) # => [ 1, 2, 3, 4, 5 ]
|
35
|
+
# [ [ 0, 1 ] ].including([ [ 1, 0 ] ]) # => [ [ 0, 1 ], [ 1, 0 ] ]
|
36
|
+
def including(*elements)
|
37
|
+
self + elements.flatten(1)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns a copy of the Array excluding the specified elements.
|
31
41
|
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
# # => ["David", "Rafael"]
|
42
|
+
# ["David", "Rafael", "Aaron", "Todd"].excluding("Aaron", "Todd") # => ["David", "Rafael"]
|
43
|
+
# [ [ 0, 1 ], [ 1, 0 ] ].excluding([ [ 1, 0 ] ]) # => [ [ 0, 1 ] ]
|
35
44
|
#
|
36
|
-
# Note: This is an optimization of
|
37
|
-
# instead of
|
45
|
+
# Note: This is an optimization of <tt>Enumerable#excluding</tt> that uses <tt>Array#-</tt>
|
46
|
+
# instead of <tt>Array#reject</tt> for performance reasons.
|
47
|
+
def excluding(*elements)
|
48
|
+
self - elements.flatten(1)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Alias for #excluding.
|
38
52
|
def without(*elements)
|
39
|
-
|
53
|
+
excluding(*elements)
|
40
54
|
end
|
41
55
|
|
42
56
|
# Equal to <tt>self[1]</tt>.
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/xml_mini"
|
2
4
|
require "active_support/core_ext/hash/keys"
|
3
5
|
require "active_support/core_ext/string/inflections"
|
@@ -72,13 +74,13 @@ class Array
|
|
72
74
|
|
73
75
|
case length
|
74
76
|
when 0
|
75
|
-
""
|
77
|
+
+""
|
76
78
|
when 1
|
77
|
-
"#{self[0]}"
|
79
|
+
+"#{self[0]}"
|
78
80
|
when 2
|
79
|
-
"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
|
81
|
+
+"#{self[0]}#{options[:two_words_connector]}#{self[1]}"
|
80
82
|
else
|
81
|
-
"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
|
83
|
+
+"#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
|
82
84
|
end
|
83
85
|
end
|
84
86
|
|
@@ -179,7 +181,7 @@ class Array
|
|
179
181
|
# </messages>
|
180
182
|
#
|
181
183
|
def to_xml(options = {})
|
182
|
-
require "active_support/builder" unless defined?(Builder)
|
184
|
+
require "active_support/builder" unless defined?(Builder::XmlMarkup)
|
183
185
|
|
184
186
|
options = options.dup
|
185
187
|
options[:indent] ||= 2
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Array
|
4
|
+
# Removes and returns the elements for which the block returns a true value.
|
5
|
+
# If no block is given, an Enumerator is returned instead.
|
6
|
+
#
|
7
|
+
# numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
8
|
+
# odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9]
|
9
|
+
# numbers # => [0, 2, 4, 6, 8]
|
10
|
+
def extract!
|
11
|
+
return to_enum(:extract!) { size } unless block_given?
|
12
|
+
|
13
|
+
extracted_elements = []
|
14
|
+
|
15
|
+
reject! do |element|
|
16
|
+
extracted_elements << element if yield(element)
|
17
|
+
end
|
18
|
+
|
19
|
+
extracted_elements
|
20
|
+
end
|
21
|
+
end
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/array/wrap"
|
2
4
|
require "active_support/core_ext/array/access"
|
3
5
|
require "active_support/core_ext/array/conversions"
|
6
|
+
require "active_support/core_ext/array/extract"
|
4
7
|
require "active_support/core_ext/array/extract_options"
|
5
8
|
require "active_support/core_ext/array/grouping"
|
6
|
-
require "active_support/core_ext/array/prepend_and_append"
|
7
9
|
require "active_support/core_ext/array/inquiry"
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "benchmark"
|
2
4
|
|
3
5
|
class << Benchmark
|
@@ -8,7 +10,7 @@ class << Benchmark
|
|
8
10
|
#
|
9
11
|
# Benchmark.ms { User.all }
|
10
12
|
# # => 0.074
|
11
|
-
def ms
|
12
|
-
1000 * realtime
|
13
|
+
def ms(&block)
|
14
|
+
1000 * realtime(&block)
|
13
15
|
end
|
14
16
|
end
|
@@ -1,11 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require "active_support/core_ext/
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/redefine_method"
|
4
4
|
|
5
5
|
class Class
|
6
6
|
# Declare a class-level attribute whose value is inheritable by subclasses.
|
7
7
|
# Subclasses can change their own value and it will not impact parent class.
|
8
8
|
#
|
9
|
+
# ==== Options
|
10
|
+
#
|
11
|
+
# * <tt>:instance_reader</tt> - Sets the instance reader method (defaults to true).
|
12
|
+
# * <tt>:instance_writer</tt> - Sets the instance writer method (defaults to true).
|
13
|
+
# * <tt>:instance_accessor</tt> - Sets both instance methods (defaults to true).
|
14
|
+
# * <tt>:instance_predicate</tt> - Sets a predicate method (defaults to true).
|
15
|
+
# * <tt>:default</tt> - Sets a default value for the attribute (defaults to nil).
|
16
|
+
#
|
17
|
+
# ==== Examples
|
18
|
+
#
|
9
19
|
# class Base
|
10
20
|
# class_attribute :setting
|
11
21
|
# end
|
@@ -68,61 +78,54 @@ class Class
|
|
68
78
|
# object.setting = false # => NoMethodError
|
69
79
|
#
|
70
80
|
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
81
|
+
#
|
82
|
+
# To set a default value for the attribute, pass <tt>default:</tt>, like so:
|
83
|
+
#
|
84
|
+
# class_attribute :settings, default: {}
|
85
|
+
def class_attribute(*attrs, instance_accessor: true,
|
86
|
+
instance_reader: instance_accessor, instance_writer: instance_accessor, instance_predicate: true, default: nil)
|
76
87
|
|
88
|
+
class_methods, methods = [], []
|
77
89
|
attrs.each do |name|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
remove_possible_singleton_method("#{name}?")
|
82
|
-
define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate
|
83
|
-
|
84
|
-
ivar = "@#{name}"
|
90
|
+
unless name.is_a?(Symbol) || name.is_a?(String)
|
91
|
+
raise TypeError, "#{name.inspect} is not a symbol nor a string"
|
92
|
+
end
|
85
93
|
|
86
|
-
|
87
|
-
|
88
|
-
singleton_class.class_eval do
|
89
|
-
remove_possible_method(name)
|
90
|
-
define_method(name) { val }
|
94
|
+
class_methods << <<~RUBY # In case the method exists and is not public
|
95
|
+
silence_redefinition_of_method def #{name}
|
91
96
|
end
|
97
|
+
RUBY
|
92
98
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
define_method(name) do
|
97
|
-
if instance_variable_defined? ivar
|
98
|
-
instance_variable_get ivar
|
99
|
-
else
|
100
|
-
singleton_class.send name
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
99
|
+
methods << <<~RUBY if instance_reader
|
100
|
+
silence_redefinition_of_method def #{name}
|
101
|
+
defined?(@#{name}) ? @#{name} : self.class.#{name}
|
104
102
|
end
|
105
|
-
|
106
|
-
end
|
103
|
+
RUBY
|
107
104
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
else
|
114
|
-
self.class.public_send name
|
115
|
-
end
|
105
|
+
class_methods << <<~RUBY
|
106
|
+
silence_redefinition_of_method def #{name}=(value)
|
107
|
+
redefine_method(:#{name}) { value } if singleton_class?
|
108
|
+
redefine_singleton_method(:#{name}) { value }
|
109
|
+
value
|
116
110
|
end
|
111
|
+
RUBY
|
117
112
|
|
118
|
-
|
119
|
-
|
120
|
-
|
113
|
+
methods << <<~RUBY if instance_writer
|
114
|
+
silence_redefinition_of_method(:#{name}=)
|
115
|
+
attr_writer :#{name}
|
116
|
+
RUBY
|
121
117
|
|
122
|
-
if
|
123
|
-
|
124
|
-
|
118
|
+
if instance_predicate
|
119
|
+
class_methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
120
|
+
if instance_reader
|
121
|
+
methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
122
|
+
end
|
125
123
|
end
|
126
124
|
end
|
125
|
+
|
126
|
+
location = caller_locations(1, 1).first
|
127
|
+
class_eval(["class << self", *class_methods, "end", *methods].join(";").tr("\n", ";"), location.path, location.lineno)
|
128
|
+
|
129
|
+
attrs.each { |name| public_send("#{name}=", default) }
|
127
130
|
end
|
128
131
|
end
|
@@ -1,40 +1,22 @@
|
|
1
|
-
|
2
|
-
require "active_support/core_ext/module/reachable"
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
3
|
class Class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
# C.descendants # => [B, A, D]
|
22
|
-
def descendants
|
23
|
-
descendants = []
|
24
|
-
ObjectSpace.each_object(singleton_class) do |k|
|
25
|
-
next if k.singleton_class?
|
26
|
-
descendants.unshift k unless k == self
|
27
|
-
end
|
28
|
-
descendants
|
29
|
-
end
|
30
|
-
rescue StandardError # JRuby 9.0.4.0 and earlier
|
31
|
-
def descendants
|
32
|
-
descendants = []
|
33
|
-
ObjectSpace.each_object(Class) do |k|
|
34
|
-
descendants.unshift k if k < self
|
35
|
-
end
|
36
|
-
descendants.uniq!
|
37
|
-
descendants
|
4
|
+
# Returns an array with all classes that are < than its receiver.
|
5
|
+
#
|
6
|
+
# class C; end
|
7
|
+
# C.descendants # => []
|
8
|
+
#
|
9
|
+
# class B < C; end
|
10
|
+
# C.descendants # => [B]
|
11
|
+
#
|
12
|
+
# class A < B; end
|
13
|
+
# C.descendants # => [B, A]
|
14
|
+
#
|
15
|
+
# class D < C; end
|
16
|
+
# C.descendants # => [B, A, D]
|
17
|
+
def descendants
|
18
|
+
ObjectSpace.each_object(singleton_class).reject do |k|
|
19
|
+
k.singleton_class? || k == self
|
38
20
|
end
|
39
21
|
end
|
40
22
|
|
@@ -46,10 +28,6 @@ class Class
|
|
46
28
|
#
|
47
29
|
# Foo.subclasses # => [Bar]
|
48
30
|
def subclasses
|
49
|
-
|
50
|
-
chain.each do |k|
|
51
|
-
subclasses << k unless chain.any? { |c| c > k }
|
52
|
-
end
|
53
|
-
subclasses
|
31
|
+
descendants.select { |descendant| descendant.superclass == self }
|
54
32
|
end
|
55
33
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "date"
|
2
4
|
require "active_support/duration"
|
3
5
|
require "active_support/core_ext/object/acts_like"
|
@@ -108,12 +110,13 @@ class Date
|
|
108
110
|
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
109
111
|
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
|
110
112
|
def advance(options)
|
111
|
-
options = options.dup
|
112
113
|
d = self
|
113
|
-
|
114
|
-
d = d >> options
|
115
|
-
d = d
|
116
|
-
d = d +
|
114
|
+
|
115
|
+
d = d >> options[:years] * 12 if options[:years]
|
116
|
+
d = d >> options[:months] if options[:months]
|
117
|
+
d = d + options[:weeks] * 7 if options[:weeks]
|
118
|
+
d = d + options[:days] if options[:days]
|
119
|
+
|
117
120
|
d
|
118
121
|
end
|
119
122
|
|
@@ -1,13 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "date"
|
2
4
|
require "active_support/inflector/methods"
|
3
5
|
require "active_support/core_ext/date/zones"
|
4
|
-
require "active_support/core_ext/module/
|
6
|
+
require "active_support/core_ext/module/redefine_method"
|
5
7
|
|
6
8
|
class Date
|
7
9
|
DATE_FORMATS = {
|
8
10
|
short: "%d %b",
|
9
11
|
long: "%B %d, %Y",
|
10
12
|
db: "%Y-%m-%d",
|
13
|
+
inspect: "%Y-%m-%d",
|
11
14
|
number: "%Y%m%d",
|
12
15
|
long_ordinal: lambda { |date|
|
13
16
|
day_format = ActiveSupport::Inflector.ordinalize(date.day)
|
@@ -17,14 +20,6 @@ class Date
|
|
17
20
|
iso8601: lambda { |date| date.iso8601 }
|
18
21
|
}
|
19
22
|
|
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
23
|
# Convert to a formatted string. See DATE_FORMATS for predefined formats.
|
29
24
|
#
|
30
25
|
# This method is aliased to <tt>to_s</tt>.
|
@@ -70,6 +65,8 @@ class Date
|
|
70
65
|
alias_method :default_inspect, :inspect
|
71
66
|
alias_method :inspect, :readable_inspect
|
72
67
|
|
68
|
+
silence_redefinition_of_method :to_time
|
69
|
+
|
73
70
|
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
74
71
|
# The timezone can be either :local or :utc (default :local).
|
75
72
|
#
|
@@ -79,11 +76,16 @@ class Date
|
|
79
76
|
# date.to_time(:local) # => 2007-11-10 00:00:00 0800
|
80
77
|
#
|
81
78
|
# date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
|
79
|
+
#
|
80
|
+
# NOTE: The :local timezone is Ruby's *process* timezone, i.e. ENV['TZ'].
|
81
|
+
# If the *application's* timezone is needed, then use +in_time_zone+ instead.
|
82
82
|
def to_time(form = :local)
|
83
83
|
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
|
84
|
-
::Time.
|
84
|
+
::Time.public_send(form, year, month, day)
|
85
85
|
end
|
86
86
|
|
87
|
+
silence_redefinition_of_method :xmlschema
|
88
|
+
|
87
89
|
# Returns a string which represents the time in used time zone as DateTime
|
88
90
|
# defined by XML Schema:
|
89
91
|
#
|