activesupport 5.1.7 → 5.2.0.beta1
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 +303 -617
- data/README.rdoc +1 -1
- data/lib/active_support.rb +3 -12
- data/lib/active_support/all.rb +2 -0
- data/lib/active_support/array_inquirer.rb +2 -0
- data/lib/active_support/backtrace_cleaner.rb +2 -0
- data/lib/active_support/benchmarkable.rb +2 -0
- data/lib/active_support/builder.rb +2 -0
- data/lib/active_support/cache.rb +127 -58
- data/lib/active_support/cache/file_store.rb +4 -3
- data/lib/active_support/cache/mem_cache_store.rb +12 -4
- data/lib/active_support/cache/memory_store.rb +2 -0
- data/lib/active_support/cache/null_store.rb +2 -0
- data/lib/active_support/cache/redis_cache_store.rb +404 -0
- data/lib/active_support/cache/strategy/local_cache.rb +9 -2
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +2 -0
- data/lib/active_support/callbacks.rb +26 -37
- data/lib/active_support/concern.rb +3 -1
- data/lib/active_support/concurrency/share_lock.rb +2 -0
- data/lib/active_support/configurable.rb +2 -0
- data/lib/active_support/core_ext.rb +3 -1
- data/lib/active_support/core_ext/array.rb +2 -0
- data/lib/active_support/core_ext/array/access.rb +4 -2
- data/lib/active_support/core_ext/array/conversions.rb +2 -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/prepend_and_append.rb +4 -2
- data/lib/active_support/core_ext/array/wrap.rb +2 -0
- data/lib/active_support/core_ext/benchmark.rb +2 -0
- data/lib/active_support/core_ext/big_decimal.rb +2 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +2 -0
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +34 -16
- data/lib/active_support/core_ext/class/attribute_accessors.rb +2 -0
- data/lib/active_support/core_ext/class/subclasses.rb +1 -2
- data/lib/active_support/core_ext/date.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 +2 -0
- data/lib/active_support/core_ext/date/conversions.rb +10 -9
- data/lib/active_support/core_ext/date/zones.rb +2 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +42 -16
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +3 -1
- data/lib/active_support/core_ext/date_and_time/zones.rb +2 -0
- data/lib/active_support/core_ext/date_time.rb +2 -0
- 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 +2 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +6 -4
- data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
- data/lib/active_support/core_ext/digest/uuid.rb +3 -1
- data/lib/active_support/core_ext/enumerable.rb +3 -1
- data/lib/active_support/core_ext/file.rb +2 -0
- data/lib/active_support/core_ext/file/atomic.rb +2 -0
- data/lib/active_support/core_ext/hash.rb +2 -0
- data/lib/active_support/core_ext/hash/compact.rb +2 -0
- data/lib/active_support/core_ext/hash/conversions.rb +2 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +8 -12
- data/lib/active_support/core_ext/hash/except.rb +2 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +2 -0
- data/lib/active_support/core_ext/hash/keys.rb +2 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +4 -4
- data/lib/active_support/core_ext/hash/transform_values.rb +2 -0
- data/lib/active_support/core_ext/integer.rb +2 -0
- data/lib/active_support/core_ext/integer/inflections.rb +2 -0
- data/lib/active_support/core_ext/integer/multiple.rb +2 -0
- data/lib/active_support/core_ext/integer/time.rb +7 -14
- data/lib/active_support/core_ext/kernel.rb +2 -0
- data/lib/active_support/core_ext/kernel/agnostics.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/load_error.rb +2 -7
- data/lib/active_support/core_ext/marshal.rb +2 -0
- data/lib/active_support/core_ext/module.rb +3 -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 +2 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +21 -24
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +2 -0
- data/lib/active_support/core_ext/module/concerning.rb +2 -0
- data/lib/active_support/core_ext/module/delegation.rb +29 -24
- data/lib/active_support/core_ext/module/deprecation.rb +2 -0
- data/lib/active_support/core_ext/module/introspection.rb +2 -0
- data/lib/active_support/core_ext/module/reachable.rb +3 -0
- data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
- data/lib/active_support/core_ext/module/remove_method.rb +5 -23
- data/lib/active_support/core_ext/name_error.rb +2 -0
- data/lib/active_support/core_ext/numeric.rb +2 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +2 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +9 -7
- data/lib/active_support/core_ext/numeric/inquiry.rb +2 -0
- data/lib/active_support/core_ext/numeric/time.rb +7 -15
- data/lib/active_support/core_ext/object.rb +2 -0
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +2 -0
- data/lib/active_support/core_ext/object/conversions.rb +2 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +2 -0
- data/lib/active_support/core_ext/object/duplicable.rb +10 -8
- 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 +8 -0
- data/lib/active_support/core_ext/object/to_param.rb +2 -0
- data/lib/active_support/core_ext/object/to_query.rb +4 -5
- data/lib/active_support/core_ext/object/try.rb +2 -0
- data/lib/active_support/core_ext/object/with_options.rb +3 -1
- data/lib/active_support/core_ext/range.rb +3 -0
- data/lib/active_support/core_ext/range/conversions.rb +9 -1
- data/lib/active_support/core_ext/range/each.rb +5 -1
- data/lib/active_support/core_ext/range/include_range.rb +2 -0
- 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 +2 -0
- data/lib/active_support/core_ext/securerandom.rb +2 -0
- data/lib/active_support/core_ext/string.rb +2 -0
- data/lib/active_support/core_ext/string/access.rb +2 -0
- data/lib/active_support/core_ext/string/behavior.rb +2 -0
- data/lib/active_support/core_ext/string/conversions.rb +2 -0
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +2 -0
- data/lib/active_support/core_ext/string/indent.rb +2 -0
- data/lib/active_support/core_ext/string/inflections.rb +26 -12
- data/lib/active_support/core_ext/string/inquiry.rb +2 -0
- data/lib/active_support/core_ext/string/multibyte.rb +2 -0
- data/lib/active_support/core_ext/string/output_safety.rb +6 -7
- data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
- data/lib/active_support/core_ext/string/strip.rb +2 -0
- data/lib/active_support/core_ext/string/zones.rb +2 -0
- data/lib/active_support/core_ext/time.rb +2 -0
- data/lib/active_support/core_ext/time/acts_like.rb +2 -0
- data/lib/active_support/core_ext/time/calculations.rb +23 -15
- data/lib/active_support/core_ext/time/compatibility.rb +4 -2
- data/lib/active_support/core_ext/time/conversions.rb +2 -0
- data/lib/active_support/core_ext/time/zones.rb +6 -4
- data/lib/active_support/core_ext/uri.rb +4 -1
- data/lib/active_support/current_attributes.rb +195 -0
- data/lib/active_support/dependencies.rb +15 -25
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +2 -0
- data/lib/active_support/deprecation.rb +4 -2
- data/lib/active_support/deprecation/behaviors.rb +23 -8
- data/lib/active_support/deprecation/constant_accessor.rb +3 -1
- data/lib/active_support/deprecation/instance_delegator.rb +2 -0
- data/lib/active_support/deprecation/method_wrappers.rb +2 -7
- data/lib/active_support/deprecation/proxy_wrappers.rb +4 -1
- data/lib/active_support/deprecation/reporting.rb +4 -2
- data/lib/active_support/descendants_tracker.rb +2 -0
- data/lib/active_support/duration.rb +8 -14
- data/lib/active_support/duration/iso8601_parser.rb +4 -2
- data/lib/active_support/duration/iso8601_serializer.rb +4 -2
- data/lib/active_support/encrypted_configuration.rb +48 -0
- data/lib/active_support/encrypted_file.rb +99 -0
- data/lib/active_support/evented_file_update_checker.rb +2 -0
- data/lib/active_support/execution_wrapper.rb +2 -0
- data/lib/active_support/executor.rb +2 -0
- data/lib/active_support/file_update_checker.rb +2 -0
- data/lib/active_support/gem_version.rb +5 -3
- data/lib/active_support/gzip.rb +2 -0
- data/lib/active_support/hash_with_indifferent_access.rb +33 -1
- data/lib/active_support/i18n.rb +3 -1
- data/lib/active_support/i18n_railtie.rb +5 -11
- data/lib/active_support/inflections.rb +2 -0
- data/lib/active_support/inflector.rb +2 -0
- data/lib/active_support/inflector/inflections.rb +19 -3
- data/lib/active_support/inflector/methods.rb +40 -23
- data/lib/active_support/inflector/transliterate.rb +17 -8
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/decoding.rb +2 -0
- data/lib/active_support/json/encoding.rb +2 -0
- data/lib/active_support/key_generator.rb +3 -1
- data/lib/active_support/lazy_load_hooks.rb +2 -0
- data/lib/active_support/log_subscriber.rb +3 -2
- data/lib/active_support/log_subscriber/test_helper.rb +2 -0
- data/lib/active_support/logger.rb +2 -0
- data/lib/active_support/logger_silence.rb +3 -2
- data/lib/active_support/logger_thread_safe_level.rb +2 -0
- data/lib/active_support/message_encryptor.rb +94 -22
- data/lib/active_support/message_verifier.rb +78 -7
- 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 +2 -0
- data/lib/active_support/multibyte/chars.rb +2 -0
- data/lib/active_support/multibyte/unicode.rb +3 -1
- data/lib/active_support/notifications.rb +2 -0
- data/lib/active_support/notifications/fanout.rb +2 -0
- data/lib/active_support/notifications/instrumenter.rb +2 -0
- data/lib/active_support/number_helper.rb +2 -0
- data/lib/active_support/number_helper/number_converter.rb +2 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +2 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +2 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +2 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +2 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +2 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +3 -1
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +2 -20
- data/lib/active_support/number_helper/rounding_helper.rb +5 -3
- data/lib/active_support/option_merger.rb +2 -0
- data/lib/active_support/ordered_hash.rb +2 -0
- data/lib/active_support/ordered_options.rb +4 -2
- data/lib/active_support/per_thread_registry.rb +2 -0
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +2 -0
- data/lib/active_support/railtie.rb +27 -8
- data/lib/active_support/reloader.rb +7 -5
- data/lib/active_support/rescuable.rb +3 -2
- data/lib/active_support/security_utils.rb +15 -11
- data/lib/active_support/string_inquirer.rb +2 -0
- data/lib/active_support/subscriber.rb +2 -0
- data/lib/active_support/tagged_logging.rb +2 -0
- data/lib/active_support/test_case.rb +2 -1
- data/lib/active_support/testing/assertions.rb +6 -4
- data/lib/active_support/testing/autorun.rb +2 -0
- 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 -0
- data/lib/active_support/testing/file_fixtures.rb +2 -0
- data/lib/active_support/testing/isolation.rb +5 -5
- data/lib/active_support/testing/method_call_assertions.rb +2 -0
- data/lib/active_support/testing/setup_and_teardown.rb +2 -0
- data/lib/active_support/testing/stream.rb +2 -0
- data/lib/active_support/testing/tagged_logging.rb +2 -0
- data/lib/active_support/testing/time_helpers.rb +31 -2
- data/lib/active_support/time.rb +2 -0
- data/lib/active_support/time_with_zone.rb +38 -0
- data/lib/active_support/values/time_zone.rb +20 -12
- data/lib/active_support/version.rb +2 -0
- data/lib/active_support/xml_mini.rb +2 -0
- data/lib/active_support/xml_mini/jdom.rb +4 -2
- data/lib/active_support/xml_mini/libxml.rb +3 -1
- data/lib/active_support/xml_mini/libxmlsax.rb +4 -2
- data/lib/active_support/xml_mini/nokogiri.rb +3 -1
- data/lib/active_support/xml_mini/nokogirisax.rb +3 -1
- data/lib/active_support/xml_mini/rexml.rb +3 -1
- metadata +19 -15
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/concern"
|
2
4
|
require "active_support/descendants_tracker"
|
3
5
|
require "active_support/core_ext/array/extract_options"
|
@@ -62,8 +64,7 @@ module ActiveSupport
|
|
62
64
|
|
63
65
|
included do
|
64
66
|
extend ActiveSupport::DescendantsTracker
|
65
|
-
class_attribute :__callbacks, instance_writer: false
|
66
|
-
self.__callbacks ||= {}
|
67
|
+
class_attribute :__callbacks, instance_writer: false, default: {}
|
67
68
|
end
|
68
69
|
|
69
70
|
CALLBACK_FILTER_TYPES = [:before, :after, :around]
|
@@ -297,8 +298,8 @@ module ActiveSupport
|
|
297
298
|
@kind = kind
|
298
299
|
@filter = filter
|
299
300
|
@key = compute_identifier filter
|
300
|
-
@if = Array(options[:if])
|
301
|
-
@unless = Array(options[:unless])
|
301
|
+
@if = check_conditionals(Array(options[:if]))
|
302
|
+
@unless = check_conditionals(Array(options[:unless]))
|
302
303
|
end
|
303
304
|
|
304
305
|
def filter; @key; end
|
@@ -322,7 +323,7 @@ module ActiveSupport
|
|
322
323
|
|
323
324
|
def duplicates?(other)
|
324
325
|
case @filter
|
325
|
-
when Symbol
|
326
|
+
when Symbol
|
326
327
|
matches?(other.kind, other.filter)
|
327
328
|
else
|
328
329
|
false
|
@@ -349,9 +350,21 @@ module ActiveSupport
|
|
349
350
|
end
|
350
351
|
|
351
352
|
private
|
353
|
+
def check_conditionals(conditionals)
|
354
|
+
if conditionals.any? { |c| c.is_a?(String) }
|
355
|
+
raise ArgumentError, <<-MSG.squish
|
356
|
+
Passing string to be evaluated in :if and :unless conditional
|
357
|
+
options is not supported. Pass a symbol for an instance method,
|
358
|
+
or a lambda, proc or block, instead.
|
359
|
+
MSG
|
360
|
+
end
|
361
|
+
|
362
|
+
conditionals
|
363
|
+
end
|
364
|
+
|
352
365
|
def compute_identifier(filter)
|
353
366
|
case filter
|
354
|
-
when
|
367
|
+
when ::Proc
|
355
368
|
filter.object_id
|
356
369
|
else
|
357
370
|
filter
|
@@ -426,7 +439,6 @@ module ActiveSupport
|
|
426
439
|
# Filters support:
|
427
440
|
#
|
428
441
|
# Symbols:: A method to call.
|
429
|
-
# Strings:: Some content to evaluate.
|
430
442
|
# Procs:: A proc to call with the object.
|
431
443
|
# Objects:: An object with a <tt>before_foo</tt> method on it to call.
|
432
444
|
#
|
@@ -436,8 +448,6 @@ module ActiveSupport
|
|
436
448
|
case filter
|
437
449
|
when Symbol
|
438
450
|
new(nil, filter, [], nil)
|
439
|
-
when String
|
440
|
-
new(nil, :instance_exec, [:value], compile_lambda(filter))
|
441
451
|
when Conditionals::Value
|
442
452
|
new(filter, :call, [:target, :value], nil)
|
443
453
|
when ::Proc
|
@@ -454,10 +464,6 @@ module ActiveSupport
|
|
454
464
|
new(filter, method_to_call, [:target], nil)
|
455
465
|
end
|
456
466
|
end
|
457
|
-
|
458
|
-
def self.compile_lambda(filter)
|
459
|
-
eval("lambda { |value| #{filter} }")
|
460
|
-
end
|
461
467
|
end
|
462
468
|
|
463
469
|
# Execute before and after filters in a sequence instead of
|
@@ -512,7 +518,6 @@ module ActiveSupport
|
|
512
518
|
end
|
513
519
|
end
|
514
520
|
|
515
|
-
# An Array with a compile method.
|
516
521
|
class CallbackChain #:nodoc:#
|
517
522
|
include Enumerable
|
518
523
|
|
@@ -598,7 +603,7 @@ module ActiveSupport
|
|
598
603
|
Proc.new do |target, result_lambda|
|
599
604
|
terminate = true
|
600
605
|
catch(:abort) do
|
601
|
-
result_lambda.call
|
606
|
+
result_lambda.call
|
602
607
|
terminate = false
|
603
608
|
end
|
604
609
|
terminate
|
@@ -651,26 +656,17 @@ module ActiveSupport
|
|
651
656
|
#
|
652
657
|
# ===== Options
|
653
658
|
#
|
654
|
-
# * <tt>:if</tt> - A symbol
|
655
|
-
#
|
656
|
-
#
|
657
|
-
# * <tt>:unless</tt> - A symbol
|
658
|
-
#
|
659
|
-
#
|
659
|
+
# * <tt>:if</tt> - A symbol or an array of symbols, each naming an instance
|
660
|
+
# method or a proc; the callback will be called only when they all return
|
661
|
+
# a true value.
|
662
|
+
# * <tt>:unless</tt> - A symbol or an array of symbols, each naming an
|
663
|
+
# instance method or a proc; the callback will be called only when they
|
664
|
+
# all return a false value.
|
660
665
|
# * <tt>:prepend</tt> - If +true+, the callback will be prepended to the
|
661
666
|
# existing chain rather than appended.
|
662
667
|
def set_callback(name, *filter_list, &block)
|
663
668
|
type, filters, options = normalize_callback_params(filter_list, block)
|
664
669
|
|
665
|
-
if options[:if].is_a?(String) || options[:unless].is_a?(String)
|
666
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
667
|
-
Passing string to be evaluated in :if and :unless conditional
|
668
|
-
options is deprecated and will be removed in Rails 5.2 without
|
669
|
-
replacement. Pass a symbol for an instance method, or a lambda,
|
670
|
-
proc or block, instead.
|
671
|
-
MSG
|
672
|
-
end
|
673
|
-
|
674
670
|
self_chain = get_callbacks name
|
675
671
|
mapped = filters.map do |filter|
|
676
672
|
Callback.build(self_chain, filter, type, options)
|
@@ -695,13 +691,6 @@ module ActiveSupport
|
|
695
691
|
def skip_callback(name, *filter_list, &block)
|
696
692
|
type, filters, options = normalize_callback_params(filter_list, block)
|
697
693
|
|
698
|
-
if options[:if].is_a?(String) || options[:unless].is_a?(String)
|
699
|
-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
700
|
-
Passing string to :if and :unless conditional options is deprecated
|
701
|
-
and will be removed in Rails 5.2 without replacement.
|
702
|
-
MSG
|
703
|
-
end
|
704
|
-
|
705
694
|
options[:raise] = true unless options.key?(:raise)
|
706
695
|
|
707
696
|
__update_callbacks(name) do |target, chain|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveSupport
|
2
4
|
# A typical module looks like this:
|
3
5
|
#
|
@@ -111,7 +113,7 @@ module ActiveSupport
|
|
111
113
|
def append_features(base)
|
112
114
|
if base.instance_variable_defined?(:@_dependencies)
|
113
115
|
base.instance_variable_get(:@_dependencies) << self
|
114
|
-
|
116
|
+
false
|
115
117
|
else
|
116
118
|
return false if base < self
|
117
119
|
@_dependencies.each { |dep| base.include(dep) }
|
@@ -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
|
#
|
@@ -33,8 +35,8 @@ class Array
|
|
33
35
|
# people.without "Aaron", "Todd"
|
34
36
|
# # => ["David", "Rafael"]
|
35
37
|
#
|
36
|
-
# Note: This is an optimization of
|
37
|
-
# instead of
|
38
|
+
# Note: This is an optimization of <tt>Enumerable#without</tt> that uses <tt>Array#-</tt>
|
39
|
+
# instead of <tt>Array#reject</tt> for performance reasons.
|
38
40
|
def without(*elements)
|
39
41
|
self - elements
|
40
42
|
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
|
8
|
+
alias_method :prepend, :unshift unless [].respond_to?(:prepend)
|
7
9
|
end
|
@@ -1,11 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_support/core_ext/kernel/singleton_class"
|
2
|
-
require "active_support/core_ext/module/
|
4
|
+
require "active_support/core_ext/module/redefine_method"
|
3
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
|
@@ -68,32 +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|
|
78
|
-
|
95
|
+
singleton_class.silence_redefinition_of_method(name)
|
79
96
|
define_singleton_method(name) { nil }
|
80
97
|
|
81
|
-
|
98
|
+
singleton_class.silence_redefinition_of_method("#{name}?")
|
82
99
|
define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate
|
83
100
|
|
84
101
|
ivar = "@#{name}"
|
85
102
|
|
86
|
-
|
103
|
+
singleton_class.silence_redefinition_of_method("#{name}=")
|
87
104
|
define_singleton_method("#{name}=") do |val|
|
88
105
|
singleton_class.class_eval do
|
89
|
-
|
90
|
-
define_method(name) { val }
|
106
|
+
redefine_method(name) { val }
|
91
107
|
end
|
92
108
|
|
93
109
|
if singleton_class?
|
94
110
|
class_eval do
|
95
|
-
|
96
|
-
define_method(name) do
|
111
|
+
redefine_method(name) do
|
97
112
|
if instance_variable_defined? ivar
|
98
113
|
instance_variable_get ivar
|
99
114
|
else
|
@@ -106,8 +121,7 @@ class Class
|
|
106
121
|
end
|
107
122
|
|
108
123
|
if instance_reader
|
109
|
-
|
110
|
-
define_method(name) do
|
124
|
+
redefine_method(name) do
|
111
125
|
if instance_variable_defined?(ivar)
|
112
126
|
instance_variable_get ivar
|
113
127
|
else
|
@@ -115,13 +129,17 @@ class Class
|
|
115
129
|
end
|
116
130
|
end
|
117
131
|
|
118
|
-
|
119
|
-
define_method("#{name}?") { !!public_send(name) } if instance_predicate
|
132
|
+
redefine_method("#{name}?") { !!public_send(name) } if instance_predicate
|
120
133
|
end
|
121
134
|
|
122
135
|
if instance_writer
|
123
|
-
|
124
|
-
|
136
|
+
redefine_method("#{name}=") do |val|
|
137
|
+
instance_variable_set ivar, val
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
unless default_value.nil?
|
142
|
+
self.send("#{name}=", default_value)
|
125
143
|
end
|
126
144
|
end
|
127
145
|
end
|
@@ -1,7 +1,9 @@
|
|
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 = {
|
@@ -17,14 +19,6 @@ class Date
|
|
17
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>.
|
@@ -70,6 +64,8 @@ class Date
|
|
70
64
|
alias_method :default_inspect, :inspect
|
71
65
|
alias_method :inspect, :readable_inspect
|
72
66
|
|
67
|
+
silence_redefinition_of_method :to_time
|
68
|
+
|
73
69
|
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
|
74
70
|
# The timezone can be either :local or :utc (default :local).
|
75
71
|
#
|
@@ -79,11 +75,16 @@ class Date
|
|
79
75
|
# date.to_time(:local) # => 2007-11-10 00:00:00 0800
|
80
76
|
#
|
81
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.
|
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.send(form, year, month, day)
|
85
84
|
end
|
86
85
|
|
86
|
+
silence_redefinition_of_method :xmlschema
|
87
|
+
|
87
88
|
# Returns a string which represents the time in used time zone as DateTime
|
88
89
|
# defined by XML Schema:
|
89
90
|
#
|