activesupport 5.0.7.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 +7 -0
- data/CHANGELOG.md +1013 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +39 -0
- data/lib/active_support.rb +99 -0
- data/lib/active_support/all.rb +3 -0
- data/lib/active_support/array_inquirer.rb +44 -0
- data/lib/active_support/backtrace_cleaner.rb +103 -0
- data/lib/active_support/benchmarkable.rb +49 -0
- data/lib/active_support/builder.rb +6 -0
- data/lib/active_support/cache.rb +701 -0
- data/lib/active_support/cache/file_store.rb +204 -0
- data/lib/active_support/cache/mem_cache_store.rb +207 -0
- data/lib/active_support/cache/memory_store.rb +167 -0
- data/lib/active_support/cache/null_store.rb +41 -0
- data/lib/active_support/cache/strategy/local_cache.rb +172 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
- data/lib/active_support/callbacks.rb +791 -0
- data/lib/active_support/concern.rb +142 -0
- data/lib/active_support/concurrency/latch.rb +26 -0
- data/lib/active_support/concurrency/share_lock.rb +226 -0
- data/lib/active_support/configurable.rb +148 -0
- data/lib/active_support/core_ext.rb +4 -0
- data/lib/active_support/core_ext/array.rb +7 -0
- data/lib/active_support/core_ext/array/access.rb +90 -0
- data/lib/active_support/core_ext/array/conversions.rb +211 -0
- data/lib/active_support/core_ext/array/extract_options.rb +29 -0
- data/lib/active_support/core_ext/array/grouping.rb +107 -0
- data/lib/active_support/core_ext/array/inquiry.rb +17 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
- data/lib/active_support/core_ext/array/wrap.rb +46 -0
- data/lib/active_support/core_ext/benchmark.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +1 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/class.rb +2 -0
- data/lib/active_support/core_ext/class/attribute.rb +128 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -0
- data/lib/active_support/core_ext/class/subclasses.rb +41 -0
- data/lib/active_support/core_ext/date.rb +5 -0
- data/lib/active_support/core_ext/date/acts_like.rb +8 -0
- data/lib/active_support/core_ext/date/blank.rb +12 -0
- data/lib/active_support/core_ext/date/calculations.rb +143 -0
- data/lib/active_support/core_ext/date/conversions.rb +95 -0
- data/lib/active_support/core_ext/date/zones.rb +6 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time.rb +5 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +14 -0
- data/lib/active_support/core_ext/date_time/blank.rb +12 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +199 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +105 -0
- data/lib/active_support/core_ext/digest/uuid.rb +51 -0
- data/lib/active_support/core_ext/enumerable.rb +146 -0
- data/lib/active_support/core_ext/file.rb +1 -0
- data/lib/active_support/core_ext/file/atomic.rb +68 -0
- data/lib/active_support/core_ext/hash.rb +9 -0
- data/lib/active_support/core_ext/hash/compact.rb +24 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +38 -0
- data/lib/active_support/core_ext/hash/except.rb +22 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +23 -0
- data/lib/active_support/core_ext/hash/keys.rb +170 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +22 -0
- data/lib/active_support/core_ext/hash/slice.rb +48 -0
- data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
- data/lib/active_support/core_ext/integer.rb +3 -0
- data/lib/active_support/core_ext/integer/inflections.rb +29 -0
- data/lib/active_support/core_ext/integer/multiple.rb +10 -0
- data/lib/active_support/core_ext/integer/time.rb +29 -0
- data/lib/active_support/core_ext/kernel.rb +4 -0
- data/lib/active_support/core_ext/kernel/agnostics.rb +11 -0
- data/lib/active_support/core_ext/kernel/concern.rb +12 -0
- data/lib/active_support/core_ext/kernel/debugger.rb +3 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +43 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +6 -0
- data/lib/active_support/core_ext/load_error.rb +31 -0
- data/lib/active_support/core_ext/marshal.rb +22 -0
- data/lib/active_support/core_ext/module.rb +12 -0
- data/lib/active_support/core_ext/module/aliasing.rb +74 -0
- data/lib/active_support/core_ext/module/anonymous.rb +28 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +36 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +212 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
- data/lib/active_support/core_ext/module/concerning.rb +135 -0
- data/lib/active_support/core_ext/module/delegation.rb +216 -0
- data/lib/active_support/core_ext/module/deprecation.rb +23 -0
- data/lib/active_support/core_ext/module/introspection.rb +68 -0
- data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
- data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
- data/lib/active_support/core_ext/module/reachable.rb +8 -0
- data/lib/active_support/core_ext/module/remove_method.rb +35 -0
- data/lib/active_support/core_ext/name_error.rb +31 -0
- data/lib/active_support/core_ext/numeric.rb +4 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +64 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +144 -0
- data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
- data/lib/active_support/core_ext/numeric/time.rb +74 -0
- data/lib/active_support/core_ext/object.rb +14 -0
- data/lib/active_support/core_ext/object/acts_like.rb +10 -0
- data/lib/active_support/core_ext/object/blank.rb +143 -0
- data/lib/active_support/core_ext/object/conversions.rb +4 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
- data/lib/active_support/core_ext/object/duplicable.rb +124 -0
- data/lib/active_support/core_ext/object/inclusion.rb +27 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +28 -0
- data/lib/active_support/core_ext/object/json.rb +205 -0
- data/lib/active_support/core_ext/object/to_param.rb +1 -0
- data/lib/active_support/core_ext/object/to_query.rb +84 -0
- data/lib/active_support/core_ext/object/try.rb +146 -0
- data/lib/active_support/core_ext/object/with_options.rb +69 -0
- data/lib/active_support/core_ext/range.rb +4 -0
- data/lib/active_support/core_ext/range/conversions.rb +31 -0
- data/lib/active_support/core_ext/range/each.rb +21 -0
- data/lib/active_support/core_ext/range/include_range.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +8 -0
- data/lib/active_support/core_ext/regexp.rb +5 -0
- data/lib/active_support/core_ext/securerandom.rb +23 -0
- data/lib/active_support/core_ext/string.rb +13 -0
- data/lib/active_support/core_ext/string/access.rb +104 -0
- data/lib/active_support/core_ext/string/behavior.rb +6 -0
- data/lib/active_support/core_ext/string/conversions.rb +57 -0
- data/lib/active_support/core_ext/string/exclude.rb +11 -0
- data/lib/active_support/core_ext/string/filters.rb +102 -0
- data/lib/active_support/core_ext/string/indent.rb +43 -0
- data/lib/active_support/core_ext/string/inflections.rb +244 -0
- data/lib/active_support/core_ext/string/inquiry.rb +13 -0
- data/lib/active_support/core_ext/string/multibyte.rb +53 -0
- data/lib/active_support/core_ext/string/output_safety.rb +260 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -0
- data/lib/active_support/core_ext/string/strip.rb +23 -0
- data/lib/active_support/core_ext/string/zones.rb +14 -0
- data/lib/active_support/core_ext/struct.rb +3 -0
- data/lib/active_support/core_ext/time.rb +5 -0
- data/lib/active_support/core_ext/time/acts_like.rb +8 -0
- data/lib/active_support/core_ext/time/calculations.rb +290 -0
- data/lib/active_support/core_ext/time/compatibility.rb +14 -0
- data/lib/active_support/core_ext/time/conversions.rb +67 -0
- data/lib/active_support/core_ext/time/marshal.rb +3 -0
- data/lib/active_support/core_ext/time/zones.rb +111 -0
- data/lib/active_support/core_ext/uri.rb +24 -0
- data/lib/active_support/dependencies.rb +755 -0
- data/lib/active_support/dependencies/autoload.rb +77 -0
- data/lib/active_support/dependencies/interlock.rb +55 -0
- data/lib/active_support/deprecation.rb +43 -0
- data/lib/active_support/deprecation/behaviors.rb +90 -0
- data/lib/active_support/deprecation/instance_delegator.rb +37 -0
- data/lib/active_support/deprecation/method_wrappers.rb +70 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +149 -0
- data/lib/active_support/deprecation/reporting.rb +112 -0
- data/lib/active_support/descendants_tracker.rb +60 -0
- data/lib/active_support/duration.rb +235 -0
- data/lib/active_support/duration/iso8601_parser.rb +122 -0
- data/lib/active_support/duration/iso8601_serializer.rb +51 -0
- data/lib/active_support/evented_file_update_checker.rb +199 -0
- data/lib/active_support/execution_wrapper.rb +126 -0
- data/lib/active_support/executor.rb +6 -0
- data/lib/active_support/file_update_checker.rb +157 -0
- data/lib/active_support/gem_version.rb +15 -0
- data/lib/active_support/gzip.rb +36 -0
- data/lib/active_support/hash_with_indifferent_access.rb +329 -0
- data/lib/active_support/i18n.rb +13 -0
- data/lib/active_support/i18n_railtie.rb +115 -0
- data/lib/active_support/inflections.rb +70 -0
- data/lib/active_support/inflector.rb +7 -0
- data/lib/active_support/inflector/inflections.rb +242 -0
- data/lib/active_support/inflector/methods.rb +390 -0
- data/lib/active_support/inflector/transliterate.rb +112 -0
- data/lib/active_support/json.rb +2 -0
- data/lib/active_support/json/decoding.rb +74 -0
- data/lib/active_support/json/encoding.rb +127 -0
- data/lib/active_support/key_generator.rb +71 -0
- data/lib/active_support/lazy_load_hooks.rb +76 -0
- data/lib/active_support/locale/en.yml +135 -0
- data/lib/active_support/log_subscriber.rb +109 -0
- data/lib/active_support/log_subscriber/test_helper.rb +104 -0
- data/lib/active_support/logger.rb +106 -0
- data/lib/active_support/logger_silence.rb +28 -0
- data/lib/active_support/logger_thread_safe_level.rb +31 -0
- data/lib/active_support/message_encryptor.rb +114 -0
- data/lib/active_support/message_verifier.rb +134 -0
- data/lib/active_support/multibyte.rb +21 -0
- data/lib/active_support/multibyte/chars.rb +231 -0
- data/lib/active_support/multibyte/unicode.rb +413 -0
- data/lib/active_support/notifications.rb +212 -0
- data/lib/active_support/notifications/fanout.rb +157 -0
- data/lib/active_support/notifications/instrumenter.rb +91 -0
- data/lib/active_support/number_helper.rb +368 -0
- data/lib/active_support/number_helper/number_converter.rb +182 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
- data/lib/active_support/option_merger.rb +25 -0
- data/lib/active_support/ordered_hash.rb +48 -0
- data/lib/active_support/ordered_options.rb +81 -0
- data/lib/active_support/per_thread_registry.rb +58 -0
- data/lib/active_support/proxy_object.rb +13 -0
- data/lib/active_support/rails.rb +27 -0
- data/lib/active_support/railtie.rb +51 -0
- data/lib/active_support/reloader.rb +129 -0
- data/lib/active_support/rescuable.rb +173 -0
- data/lib/active_support/security_utils.rb +27 -0
- data/lib/active_support/string_inquirer.rb +26 -0
- data/lib/active_support/subscriber.rb +120 -0
- data/lib/active_support/tagged_logging.rb +77 -0
- data/lib/active_support/test_case.rb +88 -0
- data/lib/active_support/testing/assertions.rb +99 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +50 -0
- data/lib/active_support/testing/declarative.rb +26 -0
- data/lib/active_support/testing/deprecation.rb +36 -0
- data/lib/active_support/testing/file_fixtures.rb +34 -0
- data/lib/active_support/testing/isolation.rb +115 -0
- data/lib/active_support/testing/method_call_assertions.rb +41 -0
- data/lib/active_support/testing/setup_and_teardown.rb +50 -0
- data/lib/active_support/testing/stream.rb +42 -0
- data/lib/active_support/testing/tagged_logging.rb +25 -0
- data/lib/active_support/testing/time_helpers.rb +136 -0
- data/lib/active_support/time.rb +18 -0
- data/lib/active_support/time_with_zone.rb +511 -0
- data/lib/active_support/values/time_zone.rb +484 -0
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +8 -0
- data/lib/active_support/xml_mini.rb +209 -0
- data/lib/active_support/xml_mini/jdom.rb +181 -0
- data/lib/active_support/xml_mini/libxml.rb +77 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +82 -0
- data/lib/active_support/xml_mini/nokogiri.rb +81 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +85 -0
- data/lib/active_support/xml_mini/rexml.rb +128 -0
- metadata +350 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_support/array_inquirer'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
# Wraps the array in an +ArrayInquirer+ object, which gives a friendlier way
|
5
|
+
# to check its string-like contents.
|
6
|
+
#
|
7
|
+
# pets = [:cat, :dog].inquiry
|
8
|
+
#
|
9
|
+
# pets.cat? # => true
|
10
|
+
# pets.ferret? # => false
|
11
|
+
#
|
12
|
+
# pets.any?(:cat, :ferret) # => true
|
13
|
+
# pets.any?(:ferret, :alligator) # => false
|
14
|
+
def inquiry
|
15
|
+
ActiveSupport::ArrayInquirer.new(self)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
class Array
|
2
|
+
# Wraps its argument in an array unless it is already an array (or array-like).
|
3
|
+
#
|
4
|
+
# Specifically:
|
5
|
+
#
|
6
|
+
# * If the argument is +nil+ an empty array is returned.
|
7
|
+
# * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
|
8
|
+
# * Otherwise, returns an array with the argument as its single element.
|
9
|
+
#
|
10
|
+
# Array.wrap(nil) # => []
|
11
|
+
# Array.wrap([1, 2, 3]) # => [1, 2, 3]
|
12
|
+
# Array.wrap(0) # => [0]
|
13
|
+
#
|
14
|
+
# This method is similar in purpose to <tt>Kernel#Array</tt>, but there are some differences:
|
15
|
+
#
|
16
|
+
# * If the argument responds to +to_ary+ the method is invoked. <tt>Kernel#Array</tt>
|
17
|
+
# moves on to try +to_a+ if the returned value is +nil+, but <tt>Array.wrap</tt> returns
|
18
|
+
# an array with the argument as its single element right away.
|
19
|
+
# * If the returned value from +to_ary+ is neither +nil+ nor an +Array+ object, <tt>Kernel#Array</tt>
|
20
|
+
# 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, if the argument does not respond to +to_ary+
|
22
|
+
# it returns an array with the argument as its single element.
|
23
|
+
#
|
24
|
+
# The last point is easily explained with some enumerables:
|
25
|
+
#
|
26
|
+
# Array(foo: :bar) # => [[:foo, :bar]]
|
27
|
+
# Array.wrap(foo: :bar) # => [{:foo=>:bar}]
|
28
|
+
#
|
29
|
+
# There's also a related idiom that uses the splat operator:
|
30
|
+
#
|
31
|
+
# [*object]
|
32
|
+
#
|
33
|
+
# which returns <tt>[]</tt> for +nil+, but calls to <tt>Array(object)</tt> otherwise.
|
34
|
+
#
|
35
|
+
# The differences with <tt>Kernel#Array</tt> explained above
|
36
|
+
# apply to the rest of <tt>object</tt>s.
|
37
|
+
def self.wrap(object)
|
38
|
+
if object.nil?
|
39
|
+
[]
|
40
|
+
elsif object.respond_to?(:to_ary)
|
41
|
+
object.to_ary || [object]
|
42
|
+
else
|
43
|
+
[object]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'active_support/core_ext/big_decimal/conversions'
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'bigdecimal/util'
|
3
|
+
|
4
|
+
module ActiveSupport
|
5
|
+
module BigDecimalWithDefaultFormat #:nodoc:
|
6
|
+
DEFAULT_STRING_FORMAT = 'F'
|
7
|
+
|
8
|
+
def to_s(format = nil)
|
9
|
+
super(format || DEFAULT_STRING_FORMAT)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
BigDecimal.prepend(ActiveSupport::BigDecimalWithDefaultFormat)
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'active_support/core_ext/kernel/singleton_class'
|
2
|
+
require 'active_support/core_ext/module/remove_method'
|
3
|
+
require 'active_support/core_ext/array/extract_options'
|
4
|
+
|
5
|
+
class Class
|
6
|
+
# Declare a class-level attribute whose value is inheritable by subclasses.
|
7
|
+
# Subclasses can change their own value and it will not impact parent class.
|
8
|
+
#
|
9
|
+
# class Base
|
10
|
+
# class_attribute :setting
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# class Subclass < Base
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# Base.setting = true
|
17
|
+
# Subclass.setting # => true
|
18
|
+
# Subclass.setting = false
|
19
|
+
# Subclass.setting # => false
|
20
|
+
# Base.setting # => true
|
21
|
+
#
|
22
|
+
# In the above case as long as Subclass does not assign a value to setting
|
23
|
+
# by performing <tt>Subclass.setting = _something_ </tt>, <tt>Subclass.setting</tt>
|
24
|
+
# would read value assigned to parent class. Once Subclass assigns a value then
|
25
|
+
# the value assigned by Subclass would be returned.
|
26
|
+
#
|
27
|
+
# This matches normal Ruby method inheritance: think of writing an attribute
|
28
|
+
# on a subclass as overriding the reader method. However, you need to be aware
|
29
|
+
# when using +class_attribute+ with mutable structures as +Array+ or +Hash+.
|
30
|
+
# In such cases, you don't want to do changes in places but use setters:
|
31
|
+
#
|
32
|
+
# Base.setting = []
|
33
|
+
# Base.setting # => []
|
34
|
+
# Subclass.setting # => []
|
35
|
+
#
|
36
|
+
# # Appending in child changes both parent and child because it is the same object:
|
37
|
+
# Subclass.setting << :foo
|
38
|
+
# Base.setting # => [:foo]
|
39
|
+
# Subclass.setting # => [:foo]
|
40
|
+
#
|
41
|
+
# # Use setters to not propagate changes:
|
42
|
+
# Base.setting = []
|
43
|
+
# Subclass.setting += [:foo]
|
44
|
+
# Base.setting # => []
|
45
|
+
# Subclass.setting # => [:foo]
|
46
|
+
#
|
47
|
+
# For convenience, an instance predicate method is defined as well.
|
48
|
+
# To skip it, pass <tt>instance_predicate: false</tt>.
|
49
|
+
#
|
50
|
+
# Subclass.setting? # => false
|
51
|
+
#
|
52
|
+
# Instances may overwrite the class value in the same way:
|
53
|
+
#
|
54
|
+
# Base.setting = true
|
55
|
+
# object = Base.new
|
56
|
+
# object.setting # => true
|
57
|
+
# object.setting = false
|
58
|
+
# object.setting # => false
|
59
|
+
# Base.setting # => true
|
60
|
+
#
|
61
|
+
# To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
|
62
|
+
#
|
63
|
+
# object.setting # => NoMethodError
|
64
|
+
# object.setting? # => NoMethodError
|
65
|
+
#
|
66
|
+
# To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
|
67
|
+
#
|
68
|
+
# object.setting = false # => NoMethodError
|
69
|
+
#
|
70
|
+
# To opt out of both instance methods, pass <tt>instance_accessor: false</tt>.
|
71
|
+
def class_attribute(*attrs)
|
72
|
+
options = attrs.extract_options!
|
73
|
+
instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
|
74
|
+
instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
|
75
|
+
instance_predicate = options.fetch(:instance_predicate, true)
|
76
|
+
|
77
|
+
attrs.each do |name|
|
78
|
+
remove_possible_singleton_method(name)
|
79
|
+
define_singleton_method(name) { nil }
|
80
|
+
|
81
|
+
remove_possible_singleton_method("#{name}?")
|
82
|
+
define_singleton_method("#{name}?") { !!public_send(name) } if instance_predicate
|
83
|
+
|
84
|
+
ivar = "@#{name}"
|
85
|
+
|
86
|
+
remove_possible_singleton_method("#{name}=")
|
87
|
+
define_singleton_method("#{name}=") do |val|
|
88
|
+
singleton_class.class_eval do
|
89
|
+
remove_possible_method(name)
|
90
|
+
define_method(name) { val }
|
91
|
+
end
|
92
|
+
|
93
|
+
if singleton_class?
|
94
|
+
class_eval do
|
95
|
+
remove_possible_method(name)
|
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
|
104
|
+
end
|
105
|
+
val
|
106
|
+
end
|
107
|
+
|
108
|
+
if instance_reader
|
109
|
+
remove_possible_method name
|
110
|
+
define_method(name) do
|
111
|
+
if instance_variable_defined?(ivar)
|
112
|
+
instance_variable_get ivar
|
113
|
+
else
|
114
|
+
self.class.public_send name
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
remove_possible_method "#{name}?"
|
119
|
+
define_method("#{name}?") { !!public_send(name) } if instance_predicate
|
120
|
+
end
|
121
|
+
|
122
|
+
if instance_writer
|
123
|
+
remove_possible_method "#{name}="
|
124
|
+
attr_writer name
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'active_support/core_ext/module/anonymous'
|
2
|
+
require 'active_support/core_ext/module/reachable'
|
3
|
+
|
4
|
+
class Class
|
5
|
+
begin
|
6
|
+
# Test if this Ruby supports each_object against singleton_class
|
7
|
+
ObjectSpace.each_object(Numeric.singleton_class) {}
|
8
|
+
|
9
|
+
def descendants # :nodoc:
|
10
|
+
descendants = []
|
11
|
+
ObjectSpace.each_object(singleton_class) do |k|
|
12
|
+
descendants.unshift k unless k == self
|
13
|
+
end
|
14
|
+
descendants
|
15
|
+
end
|
16
|
+
rescue StandardError # JRuby 9.0.4.0 and earlier
|
17
|
+
def descendants # :nodoc:
|
18
|
+
descendants = []
|
19
|
+
ObjectSpace.each_object(Class) do |k|
|
20
|
+
descendants.unshift k if k < self
|
21
|
+
end
|
22
|
+
descendants.uniq!
|
23
|
+
descendants
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns an array with the direct children of +self+.
|
28
|
+
#
|
29
|
+
# class Foo; end
|
30
|
+
# class Bar < Foo; end
|
31
|
+
# class Baz < Bar; end
|
32
|
+
#
|
33
|
+
# Foo.subclasses # => [Bar]
|
34
|
+
def subclasses
|
35
|
+
subclasses, chain = [], descendants
|
36
|
+
chain.each do |k|
|
37
|
+
subclasses << k unless chain.any? { |c| c > k }
|
38
|
+
end
|
39
|
+
subclasses
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'active_support/duration'
|
3
|
+
require 'active_support/core_ext/object/acts_like'
|
4
|
+
require 'active_support/core_ext/date/zones'
|
5
|
+
require 'active_support/core_ext/time/zones'
|
6
|
+
require 'active_support/core_ext/date_and_time/calculations'
|
7
|
+
|
8
|
+
class Date
|
9
|
+
include DateAndTime::Calculations
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :beginning_of_week_default
|
13
|
+
|
14
|
+
# Returns the week start (e.g. :monday) for the current request, if this has been set (via Date.beginning_of_week=).
|
15
|
+
# 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>.
|
16
|
+
# If no config.beginning_of_week was specified, returns :monday.
|
17
|
+
def beginning_of_week
|
18
|
+
Thread.current[:beginning_of_week] || beginning_of_week_default || :monday
|
19
|
+
end
|
20
|
+
|
21
|
+
# Sets <tt>Date.beginning_of_week</tt> to a week start (e.g. :monday) for current request/thread.
|
22
|
+
#
|
23
|
+
# This method accepts any of the following day symbols:
|
24
|
+
# :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
|
25
|
+
def beginning_of_week=(week_start)
|
26
|
+
Thread.current[:beginning_of_week] = find_beginning_of_week!(week_start)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns week start day symbol (e.g. :monday), or raises an +ArgumentError+ for invalid day symbol.
|
30
|
+
def find_beginning_of_week!(week_start)
|
31
|
+
raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
|
32
|
+
week_start
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns a new Date representing the date 1 day ago (i.e. yesterday's date).
|
36
|
+
def yesterday
|
37
|
+
::Date.current.yesterday
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns a new Date representing the date 1 day after today (i.e. tomorrow's date).
|
41
|
+
def tomorrow
|
42
|
+
::Date.current.tomorrow
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns Time.zone.today when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns Date.today.
|
46
|
+
def current
|
47
|
+
::Time.zone ? ::Time.zone.today : ::Date.today
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
52
|
+
# and then subtracts the specified number of seconds.
|
53
|
+
def ago(seconds)
|
54
|
+
in_time_zone.since(-seconds)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
58
|
+
# and then adds the specified number of seconds
|
59
|
+
def since(seconds)
|
60
|
+
in_time_zone.since(seconds)
|
61
|
+
end
|
62
|
+
alias :in :since
|
63
|
+
|
64
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)
|
65
|
+
def beginning_of_day
|
66
|
+
in_time_zone
|
67
|
+
end
|
68
|
+
alias :midnight :beginning_of_day
|
69
|
+
alias :at_midnight :beginning_of_day
|
70
|
+
alias :at_beginning_of_day :beginning_of_day
|
71
|
+
|
72
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the middle of the day (12:00)
|
73
|
+
def middle_of_day
|
74
|
+
in_time_zone.middle_of_day
|
75
|
+
end
|
76
|
+
alias :midday :middle_of_day
|
77
|
+
alias :noon :middle_of_day
|
78
|
+
alias :at_midday :middle_of_day
|
79
|
+
alias :at_noon :middle_of_day
|
80
|
+
alias :at_middle_of_day :middle_of_day
|
81
|
+
|
82
|
+
# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
|
83
|
+
def end_of_day
|
84
|
+
in_time_zone.end_of_day
|
85
|
+
end
|
86
|
+
alias :at_end_of_day :end_of_day
|
87
|
+
|
88
|
+
def plus_with_duration(other) #:nodoc:
|
89
|
+
if ActiveSupport::Duration === other
|
90
|
+
other.since(self)
|
91
|
+
else
|
92
|
+
plus_without_duration(other)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
alias_method :plus_without_duration, :+
|
96
|
+
alias_method :+, :plus_with_duration
|
97
|
+
|
98
|
+
def minus_with_duration(other) #:nodoc:
|
99
|
+
if ActiveSupport::Duration === other
|
100
|
+
plus_with_duration(-other)
|
101
|
+
else
|
102
|
+
minus_without_duration(other)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
alias_method :minus_without_duration, :-
|
106
|
+
alias_method :-, :minus_with_duration
|
107
|
+
|
108
|
+
# Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with
|
109
|
+
# any of these keys: <tt>:years</tt>, <tt>:months</tt>, <tt>:weeks</tt>, <tt>:days</tt>.
|
110
|
+
def advance(options)
|
111
|
+
options = options.dup
|
112
|
+
d = self
|
113
|
+
d = d >> options.delete(:years) * 12 if options[:years]
|
114
|
+
d = d >> options.delete(:months) if options[:months]
|
115
|
+
d = d + options.delete(:weeks) * 7 if options[:weeks]
|
116
|
+
d = d + options.delete(:days) if options[:days]
|
117
|
+
d
|
118
|
+
end
|
119
|
+
|
120
|
+
# Returns a new Date where one or more of the elements have been changed according to the +options+ parameter.
|
121
|
+
# The +options+ parameter is a hash with a combination of these keys: <tt>:year</tt>, <tt>:month</tt>, <tt>:day</tt>.
|
122
|
+
#
|
123
|
+
# Date.new(2007, 5, 12).change(day: 1) # => Date.new(2007, 5, 1)
|
124
|
+
# Date.new(2007, 5, 12).change(year: 2005, month: 1) # => Date.new(2005, 1, 12)
|
125
|
+
def change(options)
|
126
|
+
::Date.new(
|
127
|
+
options.fetch(:year, year),
|
128
|
+
options.fetch(:month, month),
|
129
|
+
options.fetch(:day, day)
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
133
|
+
# Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there.
|
134
|
+
def compare_with_coercion(other)
|
135
|
+
if other.is_a?(Time)
|
136
|
+
self.to_datetime <=> other
|
137
|
+
else
|
138
|
+
compare_without_coercion(other)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
alias_method :compare_without_coercion, :<=>
|
142
|
+
alias_method :<=>, :compare_with_coercion
|
143
|
+
end
|