activesupport 5.1.7 → 5.2.4.3

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.

Files changed (242) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +401 -541
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +3 -3
  5. data/lib/active_support.rb +5 -13
  6. data/lib/active_support/all.rb +2 -0
  7. data/lib/active_support/array_inquirer.rb +2 -0
  8. data/lib/active_support/backtrace_cleaner.rb +2 -0
  9. data/lib/active_support/benchmarkable.rb +2 -0
  10. data/lib/active_support/builder.rb +2 -0
  11. data/lib/active_support/cache.rb +197 -83
  12. data/lib/active_support/cache/file_store.rb +5 -4
  13. data/lib/active_support/cache/mem_cache_store.rb +39 -39
  14. data/lib/active_support/cache/memory_store.rb +2 -0
  15. data/lib/active_support/cache/null_store.rb +2 -0
  16. data/lib/active_support/cache/redis_cache_store.rb +466 -0
  17. data/lib/active_support/cache/strategy/local_cache.rb +33 -2
  18. data/lib/active_support/cache/strategy/local_cache_middleware.rb +2 -0
  19. data/lib/active_support/callbacks.rb +28 -39
  20. data/lib/active_support/concern.rb +10 -4
  21. data/lib/active_support/concurrency/share_lock.rb +2 -0
  22. data/lib/active_support/configurable.rb +2 -0
  23. data/lib/active_support/core_ext.rb +3 -1
  24. data/lib/active_support/core_ext/array.rb +2 -0
  25. data/lib/active_support/core_ext/array/access.rb +4 -2
  26. data/lib/active_support/core_ext/array/conversions.rb +2 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +2 -0
  29. data/lib/active_support/core_ext/array/inquiry.rb +2 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +4 -2
  31. data/lib/active_support/core_ext/array/wrap.rb +2 -0
  32. data/lib/active_support/core_ext/benchmark.rb +2 -0
  33. data/lib/active_support/core_ext/big_decimal.rb +2 -0
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +2 -0
  35. data/lib/active_support/core_ext/class.rb +2 -0
  36. data/lib/active_support/core_ext/class/attribute.rb +34 -16
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +2 -0
  38. data/lib/active_support/core_ext/class/subclasses.rb +1 -2
  39. data/lib/active_support/core_ext/date.rb +2 -0
  40. data/lib/active_support/core_ext/date/acts_like.rb +2 -0
  41. data/lib/active_support/core_ext/date/blank.rb +2 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +2 -0
  43. data/lib/active_support/core_ext/date/conversions.rb +10 -9
  44. data/lib/active_support/core_ext/date/zones.rb +2 -0
  45. data/lib/active_support/core_ext/date_and_time/calculations.rb +50 -16
  46. data/lib/active_support/core_ext/date_and_time/compatibility.rb +3 -1
  47. data/lib/active_support/core_ext/date_and_time/zones.rb +2 -0
  48. data/lib/active_support/core_ext/date_time.rb +2 -0
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +2 -0
  50. data/lib/active_support/core_ext/date_time/blank.rb +2 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +2 -0
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +7 -5
  53. data/lib/active_support/core_ext/date_time/conversions.rb +2 -0
  54. data/lib/active_support/core_ext/digest.rb +3 -0
  55. data/lib/active_support/core_ext/digest/uuid.rb +3 -1
  56. data/lib/active_support/core_ext/enumerable.rb +8 -1
  57. data/lib/active_support/core_ext/file.rb +2 -0
  58. data/lib/active_support/core_ext/file/atomic.rb +3 -1
  59. data/lib/active_support/core_ext/hash.rb +2 -0
  60. data/lib/active_support/core_ext/hash/compact.rb +2 -0
  61. data/lib/active_support/core_ext/hash/conversions.rb +4 -2
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +8 -12
  63. data/lib/active_support/core_ext/hash/except.rb +2 -0
  64. data/lib/active_support/core_ext/hash/indifferent_access.rb +2 -0
  65. data/lib/active_support/core_ext/hash/keys.rb +2 -0
  66. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  67. data/lib/active_support/core_ext/hash/slice.rb +4 -4
  68. data/lib/active_support/core_ext/hash/transform_values.rb +2 -0
  69. data/lib/active_support/core_ext/integer.rb +2 -0
  70. data/lib/active_support/core_ext/integer/inflections.rb +2 -0
  71. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  72. data/lib/active_support/core_ext/integer/time.rb +7 -14
  73. data/lib/active_support/core_ext/kernel.rb +2 -0
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  75. data/lib/active_support/core_ext/kernel/concern.rb +2 -0
  76. data/lib/active_support/core_ext/kernel/reporting.rb +2 -0
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/load_error.rb +2 -7
  79. data/lib/active_support/core_ext/marshal.rb +2 -0
  80. data/lib/active_support/core_ext/module.rb +3 -0
  81. data/lib/active_support/core_ext/module/aliasing.rb +2 -0
  82. data/lib/active_support/core_ext/module/anonymous.rb +2 -0
  83. data/lib/active_support/core_ext/module/attr_internal.rb +2 -0
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +21 -24
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +2 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +7 -8
  87. data/lib/active_support/core_ext/module/delegation.rb +31 -29
  88. data/lib/active_support/core_ext/module/deprecation.rb +2 -0
  89. data/lib/active_support/core_ext/module/introspection.rb +2 -0
  90. data/lib/active_support/core_ext/module/reachable.rb +3 -0
  91. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +5 -23
  93. data/lib/active_support/core_ext/name_error.rb +7 -0
  94. data/lib/active_support/core_ext/numeric.rb +2 -0
  95. data/lib/active_support/core_ext/numeric/bytes.rb +2 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +9 -7
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +2 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +7 -15
  99. data/lib/active_support/core_ext/object.rb +2 -0
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +12 -1
  102. data/lib/active_support/core_ext/object/conversions.rb +2 -0
  103. data/lib/active_support/core_ext/object/deep_dup.rb +2 -0
  104. data/lib/active_support/core_ext/object/duplicable.rb +10 -8
  105. data/lib/active_support/core_ext/object/inclusion.rb +2 -0
  106. data/lib/active_support/core_ext/object/instance_variables.rb +2 -0
  107. data/lib/active_support/core_ext/object/json.rb +8 -0
  108. data/lib/active_support/core_ext/object/to_param.rb +2 -0
  109. data/lib/active_support/core_ext/object/to_query.rb +2 -0
  110. data/lib/active_support/core_ext/object/try.rb +2 -0
  111. data/lib/active_support/core_ext/object/with_options.rb +3 -1
  112. data/lib/active_support/core_ext/range.rb +4 -1
  113. data/lib/active_support/core_ext/range/compare_range.rb +61 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +9 -1
  115. data/lib/active_support/core_ext/range/each.rb +5 -1
  116. data/lib/active_support/core_ext/range/include_range.rb +2 -22
  117. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  118. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  119. data/lib/active_support/core_ext/regexp.rb +2 -0
  120. data/lib/active_support/core_ext/securerandom.rb +2 -0
  121. data/lib/active_support/core_ext/string.rb +2 -0
  122. data/lib/active_support/core_ext/string/access.rb +2 -0
  123. data/lib/active_support/core_ext/string/behavior.rb +2 -0
  124. data/lib/active_support/core_ext/string/conversions.rb +2 -0
  125. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  126. data/lib/active_support/core_ext/string/filters.rb +2 -0
  127. data/lib/active_support/core_ext/string/indent.rb +2 -0
  128. data/lib/active_support/core_ext/string/inflections.rb +26 -12
  129. data/lib/active_support/core_ext/string/inquiry.rb +2 -0
  130. data/lib/active_support/core_ext/string/multibyte.rb +4 -0
  131. data/lib/active_support/core_ext/string/output_safety.rb +6 -7
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  133. data/lib/active_support/core_ext/string/strip.rb +2 -0
  134. data/lib/active_support/core_ext/string/zones.rb +2 -0
  135. data/lib/active_support/core_ext/time.rb +2 -0
  136. data/lib/active_support/core_ext/time/acts_like.rb +2 -0
  137. data/lib/active_support/core_ext/time/calculations.rb +23 -15
  138. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  139. data/lib/active_support/core_ext/time/conversions.rb +2 -0
  140. data/lib/active_support/core_ext/time/zones.rb +6 -4
  141. data/lib/active_support/core_ext/uri.rb +6 -6
  142. data/lib/active_support/current_attributes.rb +195 -0
  143. data/lib/active_support/dependencies.rb +25 -26
  144. data/lib/active_support/dependencies/autoload.rb +2 -0
  145. data/lib/active_support/dependencies/interlock.rb +2 -0
  146. data/lib/active_support/deprecation.rb +4 -2
  147. data/lib/active_support/deprecation/behaviors.rb +28 -9
  148. data/lib/active_support/deprecation/constant_accessor.rb +4 -2
  149. data/lib/active_support/deprecation/instance_delegator.rb +2 -0
  150. data/lib/active_support/deprecation/method_wrappers.rb +30 -17
  151. data/lib/active_support/deprecation/proxy_wrappers.rb +5 -2
  152. data/lib/active_support/deprecation/reporting.rb +5 -3
  153. data/lib/active_support/descendants_tracker.rb +2 -0
  154. data/lib/active_support/digest.rb +20 -0
  155. data/lib/active_support/duration.rb +11 -7
  156. data/lib/active_support/duration/iso8601_parser.rb +4 -2
  157. data/lib/active_support/duration/iso8601_serializer.rb +4 -2
  158. data/lib/active_support/encrypted_configuration.rb +49 -0
  159. data/lib/active_support/encrypted_file.rb +99 -0
  160. data/lib/active_support/evented_file_update_checker.rb +2 -0
  161. data/lib/active_support/execution_wrapper.rb +2 -0
  162. data/lib/active_support/executor.rb +2 -0
  163. data/lib/active_support/file_update_checker.rb +2 -0
  164. data/lib/active_support/gem_version.rb +5 -3
  165. data/lib/active_support/gzip.rb +2 -0
  166. data/lib/active_support/hash_with_indifferent_access.rb +55 -1
  167. data/lib/active_support/i18n.rb +3 -1
  168. data/lib/active_support/i18n_railtie.rb +4 -6
  169. data/lib/active_support/inflections.rb +2 -0
  170. data/lib/active_support/inflector.rb +2 -0
  171. data/lib/active_support/inflector/inflections.rb +20 -4
  172. data/lib/active_support/inflector/methods.rb +43 -24
  173. data/lib/active_support/inflector/transliterate.rb +17 -8
  174. data/lib/active_support/json.rb +2 -0
  175. data/lib/active_support/json/decoding.rb +2 -0
  176. data/lib/active_support/json/encoding.rb +2 -0
  177. data/lib/active_support/key_generator.rb +3 -1
  178. data/lib/active_support/lazy_load_hooks.rb +2 -0
  179. data/lib/active_support/log_subscriber.rb +3 -2
  180. data/lib/active_support/log_subscriber/test_helper.rb +2 -0
  181. data/lib/active_support/logger.rb +2 -0
  182. data/lib/active_support/logger_silence.rb +3 -2
  183. data/lib/active_support/logger_thread_safe_level.rb +4 -1
  184. data/lib/active_support/message_encryptor.rb +95 -22
  185. data/lib/active_support/message_verifier.rb +78 -7
  186. data/lib/active_support/messages/metadata.rb +71 -0
  187. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  188. data/lib/active_support/messages/rotator.rb +56 -0
  189. data/lib/active_support/multibyte.rb +2 -0
  190. data/lib/active_support/multibyte/chars.rb +2 -0
  191. data/lib/active_support/multibyte/unicode.rb +4 -2
  192. data/lib/active_support/notifications.rb +2 -0
  193. data/lib/active_support/notifications/fanout.rb +4 -2
  194. data/lib/active_support/notifications/instrumenter.rb +2 -0
  195. data/lib/active_support/number_helper.rb +2 -0
  196. data/lib/active_support/number_helper/number_converter.rb +2 -0
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +2 -0
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +2 -0
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +2 -0
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +2 -0
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +2 -0
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +3 -1
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +2 -20
  204. data/lib/active_support/number_helper/rounding_helper.rb +6 -4
  205. data/lib/active_support/option_merger.rb +2 -0
  206. data/lib/active_support/ordered_hash.rb +2 -0
  207. data/lib/active_support/ordered_options.rb +5 -3
  208. data/lib/active_support/per_thread_registry.rb +2 -0
  209. data/lib/active_support/proxy_object.rb +2 -0
  210. data/lib/active_support/rails.rb +2 -0
  211. data/lib/active_support/railtie.rb +37 -8
  212. data/lib/active_support/reloader.rb +7 -5
  213. data/lib/active_support/rescuable.rb +3 -2
  214. data/lib/active_support/security_utils.rb +15 -11
  215. data/lib/active_support/string_inquirer.rb +2 -0
  216. data/lib/active_support/subscriber.rb +8 -2
  217. data/lib/active_support/tagged_logging.rb +2 -0
  218. data/lib/active_support/test_case.rb +3 -2
  219. data/lib/active_support/testing/assertions.rb +31 -14
  220. data/lib/active_support/testing/autorun.rb +2 -0
  221. data/lib/active_support/testing/constant_lookup.rb +2 -0
  222. data/lib/active_support/testing/declarative.rb +2 -0
  223. data/lib/active_support/testing/deprecation.rb +2 -0
  224. data/lib/active_support/testing/file_fixtures.rb +2 -0
  225. data/lib/active_support/testing/isolation.rb +3 -1
  226. data/lib/active_support/testing/method_call_assertions.rb +2 -0
  227. data/lib/active_support/testing/setup_and_teardown.rb +12 -7
  228. data/lib/active_support/testing/stream.rb +2 -0
  229. data/lib/active_support/testing/tagged_logging.rb +2 -0
  230. data/lib/active_support/testing/time_helpers.rb +33 -3
  231. data/lib/active_support/time.rb +2 -0
  232. data/lib/active_support/time_with_zone.rb +38 -0
  233. data/lib/active_support/values/time_zone.rb +20 -8
  234. data/lib/active_support/version.rb +2 -0
  235. data/lib/active_support/xml_mini.rb +4 -2
  236. data/lib/active_support/xml_mini/jdom.rb +4 -2
  237. data/lib/active_support/xml_mini/libxml.rb +3 -1
  238. data/lib/active_support/xml_mini/libxmlsax.rb +4 -2
  239. data/lib/active_support/xml_mini/nokogiri.rb +3 -1
  240. data/lib/active_support/xml_mini/nokogirisax.rb +3 -1
  241. data/lib/active_support/xml_mini/rexml.rb +3 -1
  242. metadata +17 -5
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Module
2
4
  # deprecate :foo
3
5
  # deprecate bar: 'message'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/inflector"
2
4
 
3
5
  class Module
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/module/anonymous"
2
4
  require "active_support/core_ext/string/inflections"
3
5
 
@@ -5,4 +7,5 @@ class Module
5
7
  def reachable? #:nodoc:
6
8
  !anonymous? && name.safe_constantize.equal?(self)
7
9
  end
10
+ deprecate :reachable?
8
11
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Module
4
+ if RUBY_VERSION >= "2.3"
5
+ # Marks the named method as intended to be redefined, if it exists.
6
+ # Suppresses the Ruby method redefinition warning. Prefer
7
+ # #redefine_method where possible.
8
+ def silence_redefinition_of_method(method)
9
+ if method_defined?(method) || private_method_defined?(method)
10
+ # This suppresses the "method redefined" warning; the self-alias
11
+ # looks odd, but means we don't need to generate a unique name
12
+ alias_method method, method
13
+ end
14
+ end
15
+ else
16
+ def silence_redefinition_of_method(method)
17
+ if method_defined?(method) || private_method_defined?(method)
18
+ alias_method :__rails_redefine, method
19
+ remove_method :__rails_redefine
20
+ end
21
+ end
22
+ end
23
+
24
+ # Replaces the existing method definition, if there is one, with the passed
25
+ # block as its body.
26
+ def redefine_method(method, &block)
27
+ visibility = method_visibility(method)
28
+ silence_redefinition_of_method(method)
29
+ define_method(method, &block)
30
+ send(visibility, method)
31
+ end
32
+
33
+ # Replaces the existing singleton method definition, if there is one, with
34
+ # the passed block as its body.
35
+ def redefine_singleton_method(method, &block)
36
+ singleton_class.redefine_method(method, &block)
37
+ end
38
+
39
+ def method_visibility(method) # :nodoc:
40
+ case
41
+ when private_method_defined?(method)
42
+ :private
43
+ when protected_method_defined?(method)
44
+ :protected
45
+ else
46
+ :public
47
+ end
48
+ end
49
+ end
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/redefine_method"
4
+
1
5
  class Module
2
6
  # Removes the named method, if it exists.
3
7
  def remove_possible_method(method)
@@ -8,28 +12,6 @@ class Module
8
12
 
9
13
  # Removes the named singleton method, if it exists.
10
14
  def remove_possible_singleton_method(method)
11
- singleton_class.instance_eval do
12
- remove_possible_method(method)
13
- end
14
- end
15
-
16
- # Replaces the existing method definition, if there is one, with the passed
17
- # block as its body.
18
- def redefine_method(method, &block)
19
- visibility = method_visibility(method)
20
- remove_possible_method(method)
21
- define_method(method, &block)
22
- send(visibility, method)
23
- end
24
-
25
- def method_visibility(method) # :nodoc:
26
- case
27
- when private_method_defined?(method)
28
- :private
29
- when protected_method_defined?(method)
30
- :protected
31
- else
32
- :public
33
- end
15
+ singleton_class.remove_possible_method(method)
34
16
  end
35
17
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class NameError
2
4
  # Extract the name of the missing constant from the exception message.
3
5
  #
@@ -8,6 +10,11 @@ class NameError
8
10
  # end
9
11
  # # => "HelloWorld"
10
12
  def missing_name
13
+ # Since ruby v2.3.0 `did_you_mean` gem is loaded by default.
14
+ # It extends NameError#message with spell corrections which are SLOW.
15
+ # We should use original_message message instead.
16
+ message = respond_to?(:original_message) ? original_message : self.message
17
+
11
18
  if /undefined local variable or method/ !~ message
12
19
  $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message
13
20
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/numeric/bytes"
2
4
  require "active_support/core_ext/numeric/time"
3
5
  require "active_support/core_ext/numeric/inquiry"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Numeric
2
4
  KILOBYTE = 1024
3
5
  MEGABYTE = KILOBYTE * 1024
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/big_decimal/conversions"
2
4
  require "active_support/number_helper"
3
5
  require "active_support/core_ext/module/deprecation"
@@ -106,19 +108,19 @@ module ActiveSupport::NumericWithFormat
106
108
  when Integer, String
107
109
  super(format)
108
110
  when :phone
109
- return ActiveSupport::NumberHelper.number_to_phone(self, options || {})
111
+ ActiveSupport::NumberHelper.number_to_phone(self, options || {})
110
112
  when :currency
111
- return ActiveSupport::NumberHelper.number_to_currency(self, options || {})
113
+ ActiveSupport::NumberHelper.number_to_currency(self, options || {})
112
114
  when :percentage
113
- return ActiveSupport::NumberHelper.number_to_percentage(self, options || {})
115
+ ActiveSupport::NumberHelper.number_to_percentage(self, options || {})
114
116
  when :delimited
115
- return ActiveSupport::NumberHelper.number_to_delimited(self, options || {})
117
+ ActiveSupport::NumberHelper.number_to_delimited(self, options || {})
116
118
  when :rounded
117
- return ActiveSupport::NumberHelper.number_to_rounded(self, options || {})
119
+ ActiveSupport::NumberHelper.number_to_rounded(self, options || {})
118
120
  when :human
119
- return ActiveSupport::NumberHelper.number_to_human(self, options || {})
121
+ ActiveSupport::NumberHelper.number_to_human(self, options || {})
120
122
  when :human_size
121
- return ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
123
+ ActiveSupport::NumberHelper.number_to_human_size(self, options || {})
122
124
  when Symbol
123
125
  super()
124
126
  else
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  unless 1.respond_to?(:positive?) # TODO: Remove this file when we drop support to ruby < 2.3
2
4
  class Numeric
3
5
  # Returns true if the number is positive.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/duration"
2
4
  require "active_support/core_ext/time/calculations"
3
5
  require "active_support/core_ext/time/acts_like"
@@ -5,19 +7,9 @@ require "active_support/core_ext/date/calculations"
5
7
  require "active_support/core_ext/date/acts_like"
6
8
 
7
9
  class Numeric
8
- # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
9
- #
10
- # These methods use Time#advance for precise date calculations when using from_now, ago, etc.
11
- # as well as adding or subtracting their results from a Time object. For example:
12
- #
13
- # # equivalent to Time.current.advance(months: 1)
14
- # 1.month.from_now
15
- #
16
- # # equivalent to Time.current.advance(years: 2)
17
- # 2.years.from_now
10
+ # Returns a Duration instance matching the number of seconds provided.
18
11
  #
19
- # # equivalent to Time.current.advance(months: 4, years: 5)
20
- # (4.months + 5.years).from_now
12
+ # 2.seconds # => 2 seconds
21
13
  def seconds
22
14
  ActiveSupport::Duration.seconds(self)
23
15
  end
@@ -64,10 +56,10 @@ class Numeric
64
56
  alias :fortnight :fortnights
65
57
 
66
58
  # Returns the number of milliseconds equivalent to the seconds provided.
67
- # Used with the standard time durations, like 1.hour.in_milliseconds --
68
- # so we can feed them to JavaScript functions like getTime().
59
+ # Used with the standard time durations.
69
60
  #
70
- # 2.in_milliseconds # => 2_000
61
+ # 2.in_milliseconds # => 2000
62
+ # 1.hour.in_milliseconds # => 3600000
71
63
  def in_milliseconds
72
64
  self * 1000
73
65
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/object/acts_like"
2
4
  require "active_support/core_ext/object/blank"
3
5
  require "active_support/core_ext/object/duplicable"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Object
2
4
  # A duck-type assistant method. For example, Active Support extends Date
3
5
  # to define an <tt>acts_like_date?</tt> method, and extends Time to define
@@ -5,6 +7,15 @@ class Object
5
7
  # <tt>x.acts_like?(:date)</tt> to do duck-type-safe comparisons, since classes that
6
8
  # we want to act like Time simply need to define an <tt>acts_like_time?</tt> method.
7
9
  def acts_like?(duck)
8
- respond_to? :"acts_like_#{duck}?"
10
+ case duck
11
+ when :time
12
+ respond_to? :acts_like_time?
13
+ when :date
14
+ respond_to? :acts_like_date?
15
+ when :string
16
+ respond_to? :acts_like_string?
17
+ else
18
+ respond_to? :"acts_like_#{duck}?"
19
+ end
9
20
  end
10
21
  end
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/regexp"
4
+ require "concurrent/map"
2
5
 
3
6
  class Object
4
7
  # An object is blank if it's false, empty, or a whitespace string.
@@ -100,6 +103,9 @@ end
100
103
 
101
104
  class String
102
105
  BLANK_RE = /\A[[:space:]]*\z/
106
+ ENCODED_BLANKS = Concurrent::Map.new do |h, enc|
107
+ h[enc] = Regexp.new(BLANK_RE.source.encode(enc), BLANK_RE.options | Regexp::FIXEDENCODING)
108
+ end
103
109
 
104
110
  # A string is blank if it's empty or contains whitespaces only:
105
111
  #
@@ -117,7 +123,12 @@ class String
117
123
  # The regexp that matches blank strings is expensive. For the case of empty
118
124
  # strings we can speed up this method (~3.5x) with an empty? call. The
119
125
  # penalty for the rest of strings is marginal.
120
- empty? || BLANK_RE.match?(self)
126
+ empty? ||
127
+ begin
128
+ BLANK_RE.match?(self)
129
+ rescue Encoding::CompatibilityError
130
+ ENCODED_BLANKS[self.encoding].match?(self)
131
+ end
121
132
  end
122
133
  end
123
134
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/object/to_param"
2
4
  require "active_support/core_ext/object/to_query"
3
5
  require "active_support/core_ext/array/conversions"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/object/duplicable"
2
4
 
3
5
  class Object
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
4
  # Most objects are cloneable, but not all. For example you can't dup methods:
3
5
  #
@@ -106,8 +108,8 @@ require "bigdecimal"
106
108
  class BigDecimal
107
109
  # BigDecimals are duplicable:
108
110
  #
109
- # BigDecimal.new("1.2").duplicable? # => true
110
- # BigDecimal.new("1.2").dup # => #<BigDecimal:...,'0.12E1',18(18)>
111
+ # BigDecimal("1.2").duplicable? # => true
112
+ # BigDecimal("1.2").dup # => #<BigDecimal:...,'0.12E1',18(18)>
111
113
  def duplicable?
112
114
  true
113
115
  end
@@ -128,10 +130,10 @@ class Complex
128
130
  Complex(1).dup
129
131
  rescue TypeError
130
132
 
131
- # Complexes are not duplicable for RUBY_VERSION < 2.5.0:
133
+ # Complexes are not duplicable:
132
134
  #
133
- # Complex(1).duplicable? # => false
134
- # Complex(1).dup # => TypeError: can't copy Complex
135
+ # Complex(1).duplicable? # => false
136
+ # Complex(1).dup # => TypeError: can't copy Complex
135
137
  def duplicable?
136
138
  false
137
139
  end
@@ -143,10 +145,10 @@ class Rational
143
145
  Rational(1).dup
144
146
  rescue TypeError
145
147
 
146
- # Rationals are not duplicable for RUBY_VERSION < 2.5.0:
148
+ # Rationals are not duplicable:
147
149
  #
148
- # Rational(1).duplicable? # => false
149
- # Rational(1).dup # => TypeError: can't copy Rational
150
+ # Rational(1).duplicable? # => false
151
+ # Rational(1).dup # => TypeError: can't copy Rational
150
152
  def duplicable?
151
153
  false
152
154
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Object
2
4
  # Returns true if this object is included in the argument. Argument must be
3
5
  # any object which responds to +#include?+. Usage:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Object
2
4
  # Returns a hash with string keys that maps instance variable names without "@" to their
3
5
  # corresponding values.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Hack to load json gem first so we can overwrite its to_json.
2
4
  require "json"
3
5
  require "bigdecimal"
@@ -133,6 +135,12 @@ module Enumerable
133
135
  end
134
136
  end
135
137
 
138
+ class IO
139
+ def as_json(options = nil) #:nodoc:
140
+ to_s
141
+ end
142
+ end
143
+
136
144
  class Range
137
145
  def as_json(options = nil) #:nodoc:
138
146
  to_s
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/object/to_query"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "cgi"
2
4
 
3
5
  class Object
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "delegate"
2
4
 
3
5
  module ActiveSupport
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/option_merger"
2
4
 
3
5
  class Object
@@ -60,7 +62,7 @@ class Object
60
62
  #
61
63
  # validates :content, length: { minimum: 50 }, if: -> { content.present? }
62
64
  #
63
- # Hence the inherited default for `if` key is ignored.
65
+ # Hence the inherited default for +if+ key is ignored.
64
66
  #
65
67
  # NOTE: You cannot call class methods implicitly inside of with_options.
66
68
  # You can access these methods using the class name instead:
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/range/conversions"
2
- require "active_support/core_ext/range/include_range"
4
+ require "active_support/core_ext/range/compare_range"
5
+ require "active_support/core_ext/range/include_time_with_zone"
3
6
  require "active_support/core_ext/range/overlaps"
4
7
  require "active_support/core_ext/range/each"
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ module CompareWithRange #:nodoc:
5
+ # Extends the default Range#=== to support range comparisons.
6
+ # (1..5) === (1..5) # => true
7
+ # (1..5) === (2..3) # => true
8
+ # (1..5) === (2..6) # => false
9
+ #
10
+ # The native Range#=== behavior is untouched.
11
+ # ('a'..'f') === ('c') # => true
12
+ # (5..9) === (11) # => false
13
+ def ===(value)
14
+ if value.is_a?(::Range)
15
+ # 1...10 includes 1..9 but it does not include 1..10.
16
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
17
+ super(value.first) && value.last.send(operator, last)
18
+ else
19
+ super
20
+ end
21
+ end
22
+
23
+ # Extends the default Range#include? to support range comparisons.
24
+ # (1..5).include?(1..5) # => true
25
+ # (1..5).include?(2..3) # => true
26
+ # (1..5).include?(2..6) # => false
27
+ #
28
+ # The native Range#include? behavior is untouched.
29
+ # ('a'..'f').include?('c') # => true
30
+ # (5..9).include?(11) # => false
31
+ def include?(value)
32
+ if value.is_a?(::Range)
33
+ # 1...10 includes 1..9 but it does not include 1..10.
34
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
35
+ super(value.first) && value.last.send(operator, last)
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ # Extends the default Range#cover? to support range comparisons.
42
+ # (1..5).cover?(1..5) # => true
43
+ # (1..5).cover?(2..3) # => true
44
+ # (1..5).cover?(2..6) # => false
45
+ #
46
+ # The native Range#cover? behavior is untouched.
47
+ # ('a'..'f').cover?('c') # => true
48
+ # (5..9).cover?(11) # => false
49
+ def cover?(value)
50
+ if value.is_a?(::Range)
51
+ # 1...10 covers 1..9 but it does not cover 1..10.
52
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
53
+ super(value.first) && value.last.send(operator, last)
54
+ else
55
+ super
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ Range.prepend(ActiveSupport::CompareWithRange)