activesupport 4.2.11.1 → 6.0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of activesupport might be problematic. Click here for more details.

Files changed (263) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +399 -411
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +7 -7
  5. data/lib/active_support/actionable_error.rb +48 -0
  6. data/lib/active_support/all.rb +5 -3
  7. data/lib/active_support/array_inquirer.rb +48 -0
  8. data/lib/active_support/backtrace_cleaner.rb +34 -6
  9. data/lib/active_support/benchmarkable.rb +6 -4
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache/file_store.rb +58 -53
  12. data/lib/active_support/cache/mem_cache_store.rb +95 -91
  13. data/lib/active_support/cache/memory_store.rb +39 -36
  14. data/lib/active_support/cache/null_store.rb +11 -7
  15. data/lib/active_support/cache/redis_cache_store.rb +493 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +75 -42
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  18. data/lib/active_support/cache.rb +331 -217
  19. data/lib/active_support/callbacks.rb +650 -592
  20. data/lib/active_support/concern.rb +35 -6
  21. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +35 -0
  22. data/lib/active_support/concurrency/share_lock.rb +226 -0
  23. data/lib/active_support/configurable.rb +13 -14
  24. data/lib/active_support/core_ext/array/access.rb +41 -1
  25. data/lib/active_support/core_ext/array/conversions.rb +24 -20
  26. data/lib/active_support/core_ext/array/extract.rb +21 -0
  27. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  28. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  29. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  30. data/lib/active_support/core_ext/array/prepend_and_append.rb +4 -6
  31. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  32. data/lib/active_support/core_ext/array.rb +9 -6
  33. data/lib/active_support/core_ext/benchmark.rb +3 -1
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  35. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  36. data/lib/active_support/core_ext/class/attribute.rb +45 -31
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  38. data/lib/active_support/core_ext/class/subclasses.rb +20 -6
  39. data/lib/active_support/core_ext/class.rb +4 -3
  40. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  41. data/lib/active_support/core_ext/date/blank.rb +14 -0
  42. data/lib/active_support/core_ext/date/calculations.rb +17 -14
  43. data/lib/active_support/core_ext/date/conversions.rb +25 -23
  44. data/lib/active_support/core_ext/date/zones.rb +4 -2
  45. data/lib/active_support/core_ext/date.rb +6 -4
  46. data/lib/active_support/core_ext/date_and_time/calculations.rb +154 -65
  47. data/lib/active_support/core_ext/date_and_time/compatibility.rb +4 -3
  48. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -13
  49. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  50. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  51. data/lib/active_support/core_ext/date_time/calculations.rb +37 -19
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +8 -6
  53. data/lib/active_support/core_ext/date_time/conversions.rb +16 -13
  54. data/lib/active_support/core_ext/date_time.rb +7 -5
  55. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  56. data/lib/active_support/core_ext/digest.rb +3 -0
  57. data/lib/active_support/core_ext/enumerable.rb +114 -22
  58. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  59. data/lib/active_support/core_ext/file.rb +3 -1
  60. data/lib/active_support/core_ext/hash/compact.rb +4 -23
  61. data/lib/active_support/core_ext/hash/conversions.rb +62 -41
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  63. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  64. data/lib/active_support/core_ext/hash/except.rb +12 -9
  65. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  66. data/lib/active_support/core_ext/hash/keys.rb +19 -42
  67. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  68. data/lib/active_support/core_ext/hash/slice.rb +5 -27
  69. data/lib/active_support/core_ext/hash/transform_values.rb +4 -22
  70. data/lib/active_support/core_ext/hash.rb +10 -9
  71. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  72. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  73. data/lib/active_support/core_ext/integer/time.rb +11 -18
  74. data/lib/active_support/core_ext/integer.rb +5 -3
  75. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  76. data/lib/active_support/core_ext/kernel/reporting.rb +4 -84
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/kernel.rb +5 -5
  79. data/lib/active_support/core_ext/load_error.rb +3 -22
  80. data/lib/active_support/core_ext/marshal.rb +8 -8
  81. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  82. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  83. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +46 -46
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +144 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  87. data/lib/active_support/core_ext/module/delegation.rb +133 -30
  88. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  89. data/lib/active_support/core_ext/module/introspection.rb +44 -19
  90. data/lib/active_support/core_ext/module/reachable.rb +5 -7
  91. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  93. data/lib/active_support/core_ext/module.rb +13 -11
  94. data/lib/active_support/core_ext/name_error.rb +22 -2
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +129 -136
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +5 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +35 -23
  99. data/lib/active_support/core_ext/numeric.rb +5 -3
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +27 -3
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  104. data/lib/active_support/core_ext/object/duplicable.rb +13 -93
  105. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +51 -20
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +10 -5
  110. data/lib/active_support/core_ext/object/try.rb +81 -23
  111. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  112. data/lib/active_support/core_ext/object.rb +14 -13
  113. data/lib/active_support/core_ext/range/compare_range.rb +76 -0
  114. data/lib/active_support/core_ext/range/conversions.rb +37 -15
  115. data/lib/active_support/core_ext/range/each.rb +18 -17
  116. data/lib/active_support/core_ext/range/include_range.rb +7 -21
  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/range.rb +7 -4
  120. data/lib/active_support/core_ext/regexp.rb +2 -0
  121. data/lib/active_support/core_ext/securerandom.rb +45 -0
  122. data/lib/active_support/core_ext/string/access.rb +16 -6
  123. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  124. data/lib/active_support/core_ext/string/conversions.rb +7 -4
  125. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  126. data/lib/active_support/core_ext/string/filters.rb +48 -6
  127. data/lib/active_support/core_ext/string/indent.rb +6 -4
  128. data/lib/active_support/core_ext/string/inflections.rb +66 -24
  129. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  130. data/lib/active_support/core_ext/string/multibyte.rb +16 -7
  131. data/lib/active_support/core_ext/string/output_safety.rb +93 -40
  132. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  133. data/lib/active_support/core_ext/string/strip.rb +6 -5
  134. data/lib/active_support/core_ext/string/zones.rb +4 -2
  135. data/lib/active_support/core_ext/string.rb +15 -13
  136. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  137. data/lib/active_support/core_ext/time/calculations.rb +115 -52
  138. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  139. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  140. data/lib/active_support/core_ext/time/zones.rb +41 -7
  141. data/lib/active_support/core_ext/time.rb +7 -6
  142. data/lib/active_support/core_ext/uri.rb +6 -7
  143. data/lib/active_support/core_ext.rb +3 -1
  144. data/lib/active_support/current_attributes.rb +203 -0
  145. data/lib/active_support/dependencies/autoload.rb +2 -0
  146. data/lib/active_support/dependencies/interlock.rb +57 -0
  147. data/lib/active_support/dependencies/zeitwerk_integration.rb +117 -0
  148. data/lib/active_support/dependencies.rb +208 -166
  149. data/lib/active_support/deprecation/behaviors.rb +44 -11
  150. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  151. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  152. data/lib/active_support/deprecation/method_wrappers.rb +61 -21
  153. data/lib/active_support/deprecation/proxy_wrappers.rb +81 -30
  154. data/lib/active_support/deprecation/reporting.rb +32 -12
  155. data/lib/active_support/deprecation.rb +12 -9
  156. data/lib/active_support/descendants_tracker.rb +57 -9
  157. data/lib/active_support/digest.rb +20 -0
  158. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  159. data/lib/active_support/duration/iso8601_serializer.rb +53 -0
  160. data/lib/active_support/duration.rb +315 -40
  161. data/lib/active_support/encrypted_configuration.rb +45 -0
  162. data/lib/active_support/encrypted_file.rb +100 -0
  163. data/lib/active_support/evented_file_update_checker.rb +234 -0
  164. data/lib/active_support/execution_wrapper.rb +129 -0
  165. data/lib/active_support/executor.rb +8 -0
  166. data/lib/active_support/file_update_checker.rb +62 -37
  167. data/lib/active_support/gem_version.rb +6 -4
  168. data/lib/active_support/gzip.rb +7 -5
  169. data/lib/active_support/hash_with_indifferent_access.rb +129 -30
  170. data/lib/active_support/i18n.rb +9 -6
  171. data/lib/active_support/i18n_railtie.rb +50 -14
  172. data/lib/active_support/inflections.rb +13 -11
  173. data/lib/active_support/inflector/inflections.rb +58 -13
  174. data/lib/active_support/inflector/methods.rb +159 -145
  175. data/lib/active_support/inflector/transliterate.rb +84 -34
  176. data/lib/active_support/inflector.rb +7 -5
  177. data/lib/active_support/json/decoding.rb +32 -30
  178. data/lib/active_support/json/encoding.rb +17 -60
  179. data/lib/active_support/json.rb +4 -2
  180. data/lib/active_support/key_generator.rb +11 -43
  181. data/lib/active_support/lazy_load_hooks.rb +53 -20
  182. data/lib/active_support/locale/en.rb +33 -0
  183. data/lib/active_support/locale/en.yml +2 -0
  184. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  185. data/lib/active_support/log_subscriber.rb +44 -19
  186. data/lib/active_support/logger.rb +9 -23
  187. data/lib/active_support/logger_silence.rb +32 -14
  188. data/lib/active_support/logger_thread_safe_level.rb +32 -8
  189. data/lib/active_support/message_encryptor.rb +166 -53
  190. data/lib/active_support/message_verifier.rb +149 -16
  191. data/lib/active_support/messages/metadata.rb +72 -0
  192. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  193. data/lib/active_support/messages/rotator.rb +56 -0
  194. data/lib/active_support/multibyte/chars.rb +56 -63
  195. data/lib/active_support/multibyte/unicode.rb +56 -290
  196. data/lib/active_support/multibyte.rb +4 -2
  197. data/lib/active_support/notifications/fanout.rb +109 -22
  198. data/lib/active_support/notifications/instrumenter.rb +107 -16
  199. data/lib/active_support/notifications.rb +51 -10
  200. data/lib/active_support/number_helper/number_converter.rb +16 -15
  201. data/lib/active_support/number_helper/number_to_currency_converter.rb +14 -15
  202. data/lib/active_support/number_helper/number_to_delimited_converter.rb +11 -4
  203. data/lib/active_support/number_helper/number_to_human_converter.rb +13 -10
  204. data/lib/active_support/number_helper/number_to_human_size_converter.rb +11 -9
  205. data/lib/active_support/number_helper/number_to_percentage_converter.rb +5 -1
  206. data/lib/active_support/number_helper/number_to_phone_converter.rb +15 -5
  207. data/lib/active_support/number_helper/number_to_rounded_converter.rb +25 -57
  208. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  209. data/lib/active_support/number_helper.rb +105 -68
  210. data/lib/active_support/option_merger.rb +24 -4
  211. data/lib/active_support/ordered_hash.rb +7 -5
  212. data/lib/active_support/ordered_options.rb +27 -5
  213. data/lib/active_support/parameter_filter.rb +128 -0
  214. data/lib/active_support/per_thread_registry.rb +9 -4
  215. data/lib/active_support/proxy_object.rb +2 -0
  216. data/lib/active_support/rails.rb +10 -8
  217. data/lib/active_support/railtie.rb +43 -9
  218. data/lib/active_support/reloader.rb +130 -0
  219. data/lib/active_support/rescuable.rb +108 -53
  220. data/lib/active_support/security_utils.rb +15 -11
  221. data/lib/active_support/string_inquirer.rb +11 -4
  222. data/lib/active_support/subscriber.rb +74 -30
  223. data/lib/active_support/tagged_logging.rb +25 -13
  224. data/lib/active_support/test_case.rb +107 -44
  225. data/lib/active_support/testing/assertions.rb +151 -20
  226. data/lib/active_support/testing/autorun.rb +4 -2
  227. data/lib/active_support/testing/constant_lookup.rb +2 -1
  228. data/lib/active_support/testing/declarative.rb +3 -1
  229. data/lib/active_support/testing/deprecation.rb +13 -10
  230. data/lib/active_support/testing/file_fixtures.rb +38 -0
  231. data/lib/active_support/testing/isolation.rb +35 -26
  232. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  233. data/lib/active_support/testing/parallelization.rb +134 -0
  234. data/lib/active_support/testing/setup_and_teardown.rb +13 -8
  235. data/lib/active_support/testing/stream.rb +43 -0
  236. data/lib/active_support/testing/tagged_logging.rb +3 -1
  237. data/lib/active_support/testing/time_helpers.rb +84 -20
  238. data/lib/active_support/time.rb +14 -12
  239. data/lib/active_support/time_with_zone.rb +179 -39
  240. data/lib/active_support/values/time_zone.rb +203 -63
  241. data/lib/active_support/version.rb +3 -1
  242. data/lib/active_support/xml_mini/jdom.rb +116 -115
  243. data/lib/active_support/xml_mini/libxml.rb +16 -13
  244. data/lib/active_support/xml_mini/libxmlsax.rb +15 -14
  245. data/lib/active_support/xml_mini/nokogiri.rb +14 -12
  246. data/lib/active_support/xml_mini/nokogirisax.rb +14 -13
  247. data/lib/active_support/xml_mini/rexml.rb +11 -9
  248. data/lib/active_support/xml_mini.rb +38 -46
  249. data/lib/active_support.rb +13 -11
  250. metadata +84 -26
  251. data/lib/active_support/concurrency/latch.rb +0 -27
  252. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
  253. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  254. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  255. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  256. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  257. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -13
  258. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  259. data/lib/active_support/core_ext/object/itself.rb +0 -15
  260. data/lib/active_support/core_ext/struct.rb +0 -6
  261. data/lib/active_support/core_ext/thread.rb +0 -86
  262. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  263. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -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:
@@ -5,7 +7,7 @@ class Object
5
7
  # characters = ["Konata", "Kagami", "Tsukasa"]
6
8
  # "Konata".in?(characters) # => true
7
9
  #
8
- # This will throw an ArgumentError if the argument doesn't respond
10
+ # This will throw an +ArgumentError+ if the argument doesn't respond
9
11
  # to +#include?+.
10
12
  def in?(another_object)
11
13
  another_object.include?(self)
@@ -18,10 +20,10 @@ class Object
18
20
  #
19
21
  # params[:bucket_type].presence_in %w( project calendar )
20
22
  #
21
- # This will throw an ArgumentError if the argument doesn't respond to +#include?+.
23
+ # This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
22
24
  #
23
25
  # @return [Object]
24
26
  def presence_in(another_object)
25
- self.in?(another_object) ? self : nil
27
+ in?(another_object) ? self : nil
26
28
  end
27
29
  end
@@ -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.
@@ -23,6 +25,6 @@ class Object
23
25
  #
24
26
  # C.new(0, 1).instance_variable_names # => ["@y", "@x"]
25
27
  def instance_variable_names
26
- instance_variables.map { |var| var.to_s }
28
+ instance_variables.map(&:to_s)
27
29
  end
28
30
  end
@@ -1,16 +1,20 @@
1
- # Hack to load json gem first so we can overwrite its to_json.
2
- require 'json'
3
- require 'bigdecimal'
4
- require 'active_support/core_ext/big_decimal/conversions' # for #to_s
5
- require 'active_support/core_ext/hash/except'
6
- require 'active_support/core_ext/hash/slice'
7
- require 'active_support/core_ext/object/instance_variables'
8
- require 'time'
9
- require 'active_support/core_ext/time/conversions'
10
- require 'active_support/core_ext/date_time/conversions'
11
- require 'active_support/core_ext/date/conversions'
12
- require 'active_support/core_ext/module/aliasing'
1
+ # frozen_string_literal: true
13
2
 
3
+ # Hack to load json gem first so we can overwrite its to_json.
4
+ require "json"
5
+ require "bigdecimal"
6
+ require "uri/generic"
7
+ require "pathname"
8
+ require "active_support/core_ext/big_decimal/conversions" # for #to_s
9
+ require "active_support/core_ext/hash/except"
10
+ require "active_support/core_ext/hash/slice"
11
+ require "active_support/core_ext/object/instance_variables"
12
+ require "time"
13
+ require "active_support/core_ext/time/conversions"
14
+ require "active_support/core_ext/date_time/conversions"
15
+ require "active_support/core_ext/date/conversions"
16
+
17
+ #--
14
18
  # The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
15
19
  # their default behavior. That said, we need to define the basic to_json method in all of them,
16
20
  # otherwise they will always use to_json gem implementation, which is backwards incompatible in
@@ -26,22 +30,25 @@ require 'active_support/core_ext/module/aliasing'
26
30
  # bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
27
31
  # ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
28
32
  # should give exactly the same results with or without active support.
29
- [Enumerable, Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
30
- klass.class_eval do
31
- def to_json_with_active_support_encoder(options = nil) # :nodoc:
33
+
34
+ module ActiveSupport
35
+ module ToJsonWithActiveSupportEncoder # :nodoc:
36
+ def to_json(options = nil)
32
37
  if options.is_a?(::JSON::State)
33
38
  # Called from JSON.{generate,dump}, forward it to JSON gem's to_json
34
- self.to_json_without_active_support_encoder(options)
39
+ super(options)
35
40
  else
36
41
  # to_json is being invoked directly, use ActiveSupport's encoder
37
42
  ActiveSupport::JSON.encode(self, options)
38
43
  end
39
44
  end
40
-
41
- alias_method_chain :to_json, :active_support_encoder
42
45
  end
43
46
  end
44
47
 
48
+ [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].reverse_each do |klass|
49
+ klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
50
+ end
51
+
45
52
  class Object
46
53
  def as_json(options = nil) #:nodoc:
47
54
  if respond_to?(:to_hash)
@@ -129,6 +136,12 @@ module Enumerable
129
136
  end
130
137
  end
131
138
 
139
+ class IO
140
+ def as_json(options = nil) #:nodoc:
141
+ to_s
142
+ end
143
+ end
144
+
132
145
  class Range
133
146
  def as_json(options = nil) #:nodoc:
134
147
  to_s
@@ -185,13 +198,31 @@ class DateTime
185
198
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
186
199
  xmlschema(ActiveSupport::JSON::Encoding.time_precision)
187
200
  else
188
- strftime('%Y/%m/%d %H:%M:%S %z')
201
+ strftime("%Y/%m/%d %H:%M:%S %z")
189
202
  end
190
203
  end
191
204
  end
192
205
 
206
+ class URI::Generic #:nodoc:
207
+ def as_json(options = nil)
208
+ to_s
209
+ end
210
+ end
211
+
212
+ class Pathname #:nodoc:
213
+ def as_json(options = nil)
214
+ to_s
215
+ end
216
+ end
217
+
193
218
  class Process::Status #:nodoc:
194
219
  def as_json(options = nil)
195
- { :exitstatus => exitstatus, :pid => pid }
220
+ { exitstatus: exitstatus, pid: pid }
221
+ end
222
+ end
223
+
224
+ class Exception
225
+ def as_json(options = nil)
226
+ to_s
196
227
  end
197
228
  end
@@ -1 +1,3 @@
1
- require 'active_support/core_ext/object/to_query'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/object/to_query"
@@ -1,4 +1,6 @@
1
- require 'cgi'
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
2
4
 
3
5
  class Object
4
6
  # Alias of <tt>to_s</tt>.
@@ -38,7 +40,7 @@ class Array
38
40
  # Calls <tt>to_param</tt> on all its elements and joins the result with
39
41
  # slashes. This is used by <tt>url_for</tt> in Action Pack.
40
42
  def to_param
41
- collect { |e| e.to_param }.join '/'
43
+ collect(&:to_param).join "/"
42
44
  end
43
45
 
44
46
  # Converts an array into a string suitable for use as a URL query string,
@@ -51,7 +53,7 @@ class Array
51
53
  if empty?
52
54
  nil.to_query(prefix)
53
55
  else
54
- collect { |value| value.to_query(prefix) }.join '&'
56
+ collect { |value| value.to_query(prefix) }.join "&"
55
57
  end
56
58
  end
57
59
  end
@@ -73,11 +75,14 @@ class Hash
73
75
  #
74
76
  # This method is also aliased as +to_param+.
75
77
  def to_query(namespace = nil)
76
- collect do |key, value|
78
+ query = collect do |key, value|
77
79
  unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
78
80
  value.to_query(namespace ? "#{namespace}[#{key}]" : key)
79
81
  end
80
- end.compact.sort! * '&'
82
+ end.compact
83
+
84
+ query.sort! unless namespace.to_s.include?("[]")
85
+ query.join("&")
81
86
  end
82
87
 
83
88
  alias_method :to_param, :to_query
@@ -1,4 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+
5
+ module ActiveSupport
6
+ module Tryable #:nodoc:
7
+ def try(method_name = nil, *args, &b)
8
+ if method_name.nil? && block_given?
9
+ if b.arity == 0
10
+ instance_eval(&b)
11
+ else
12
+ yield self
13
+ end
14
+ elsif respond_to?(method_name)
15
+ public_send(method_name, *args, &b)
16
+ end
17
+ end
18
+ ruby2_keywords(:try) if respond_to?(:ruby2_keywords, true)
19
+
20
+ def try!(method_name = nil, *args, &b)
21
+ if method_name.nil? && block_given?
22
+ if b.arity == 0
23
+ instance_eval(&b)
24
+ else
25
+ yield self
26
+ end
27
+ else
28
+ public_send(method_name, *args, &b)
29
+ end
30
+ end
31
+ ruby2_keywords(:try!) if respond_to?(:ruby2_keywords, true)
32
+ end
33
+ end
34
+
1
35
  class Object
36
+ include ActiveSupport::Tryable
37
+
38
+ ##
39
+ # :method: try
40
+ #
41
+ # :call-seq:
42
+ # try(*a, &b)
43
+ #
2
44
  # Invokes the public method whose name goes as first argument just like
3
45
  # +public_send+ does, except that if the receiver does not respond to it the
4
46
  # call returns +nil+ rather than raising an exception.
@@ -21,11 +63,11 @@ class Object
21
63
  #
22
64
  # +try+ will also return +nil+ if the receiver does not respond to the method:
23
65
  #
24
- # @person.try(:non_existing_method) #=> nil
66
+ # @person.try(:non_existing_method) # => nil
25
67
  #
26
68
  # instead of
27
69
  #
28
- # @person.non_existing_method if @person.respond_to?(:non_existing_method) #=> nil
70
+ # @person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil
29
71
  #
30
72
  # +try+ returns +nil+ when called on +nil+ regardless of whether it responds
31
73
  # to the method:
@@ -56,27 +98,40 @@ class Object
56
98
  #
57
99
  # Please also note that +try+ is defined on +Object+. Therefore, it won't work
58
100
  # with instances of classes that do not have +Object+ among their ancestors,
59
- # like direct subclasses of +BasicObject+. For example, using +try+ with
60
- # +SimpleDelegator+ will delegate +try+ to the target instead of calling it on
61
- # the delegator itself.
62
- def try(*a, &b)
63
- try!(*a, &b) if a.empty? || respond_to?(a.first)
64
- end
101
+ # like direct subclasses of +BasicObject+.
65
102
 
66
- # Same as #try, but will raise a NoMethodError exception if the receiver is not +nil+ and
67
- # does not implement the tried method.
103
+ ##
104
+ # :method: try!
105
+ #
106
+ # :call-seq:
107
+ # try!(*a, &b)
108
+ #
109
+ # Same as #try, but raises a +NoMethodError+ exception if the receiver is
110
+ # not +nil+ and does not implement the tried method.
111
+ #
112
+ # "a".try!(:upcase) # => "A"
113
+ # nil.try!(:upcase) # => nil
114
+ # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer
115
+ end
68
116
 
69
- def try!(*a, &b)
70
- if a.empty? && block_given?
71
- if b.arity == 0
72
- instance_eval(&b)
73
- else
74
- yield self
75
- end
76
- else
77
- public_send(*a, &b)
78
- end
79
- end
117
+ class Delegator
118
+ include ActiveSupport::Tryable
119
+
120
+ ##
121
+ # :method: try
122
+ #
123
+ # :call-seq:
124
+ # try(a*, &b)
125
+ #
126
+ # See Object#try
127
+
128
+ ##
129
+ # :method: try!
130
+ #
131
+ # :call-seq:
132
+ # try!(a*, &b)
133
+ #
134
+ # See Object#try!
80
135
  end
81
136
 
82
137
  class NilClass
@@ -90,11 +145,14 @@ class NilClass
90
145
  #
91
146
  # With +try+
92
147
  # @person.try(:children).try(:first).try(:name)
93
- def try(*args)
148
+ def try(method_name = nil, *args)
94
149
  nil
95
150
  end
96
151
 
97
- def try!(*args)
152
+ # Calling +try!+ on +nil+ always returns +nil+.
153
+ #
154
+ # nil.try!(:name) # => nil
155
+ def try!(method_name = nil, *args)
98
156
  nil
99
157
  end
100
158
  end
@@ -1,4 +1,6 @@
1
- require 'active_support/option_merger'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/option_merger"
2
4
 
3
5
  class Object
4
6
  # An elegant way to factor duplication out of options passed to a series of
@@ -7,7 +9,7 @@ class Object
7
9
  # provided. Each method called on the block variable must take an options
8
10
  # hash as its final argument.
9
11
  #
10
- # Without <tt>with_options></tt>, this code contains duplication:
12
+ # Without <tt>with_options</tt>, this code contains duplication:
11
13
  #
12
14
  # class Account < ActiveRecord::Base
13
15
  # has_many :customers, dependent: :destroy
@@ -60,7 +62,18 @@ 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.
66
+ #
67
+ # NOTE: You cannot call class methods implicitly inside of with_options.
68
+ # You can access these methods using the class name instead:
69
+ #
70
+ # class Phone < ActiveRecord::Base
71
+ # enum phone_number_type: { home: 0, office: 1, mobile: 2 }
72
+ #
73
+ # with_options presence: true do
74
+ # validates :phone_number_type, inclusion: { in: Phone.phone_number_types.keys }
75
+ # end
76
+ # end
64
77
  #
65
78
  def with_options(options, &block)
66
79
  option_merger = ActiveSupport::OptionMerger.new(self, options)
@@ -1,15 +1,16 @@
1
- require 'active_support/core_ext/object/acts_like'
2
- require 'active_support/core_ext/object/blank'
3
- require 'active_support/core_ext/object/duplicable'
4
- require 'active_support/core_ext/object/deep_dup'
5
- require 'active_support/core_ext/object/itself'
6
- require 'active_support/core_ext/object/try'
7
- require 'active_support/core_ext/object/inclusion'
1
+ # frozen_string_literal: true
8
2
 
9
- require 'active_support/core_ext/object/conversions'
10
- require 'active_support/core_ext/object/instance_variables'
3
+ require "active_support/core_ext/object/acts_like"
4
+ require "active_support/core_ext/object/blank"
5
+ require "active_support/core_ext/object/duplicable"
6
+ require "active_support/core_ext/object/deep_dup"
7
+ require "active_support/core_ext/object/try"
8
+ require "active_support/core_ext/object/inclusion"
11
9
 
12
- require 'active_support/core_ext/object/json'
13
- require 'active_support/core_ext/object/to_param'
14
- require 'active_support/core_ext/object/to_query'
15
- require 'active_support/core_ext/object/with_options'
10
+ require "active_support/core_ext/object/conversions"
11
+ require "active_support/core_ext/object/instance_variables"
12
+
13
+ require "active_support/core_ext/object/json"
14
+ require "active_support/core_ext/object/to_param"
15
+ require "active_support/core_ext/object/to_query"
16
+ require "active_support/core_ext/object/with_options"
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveSupport
4
+ module CompareWithRange
5
+ # Extends the default Range#=== to support range comparisons.
6
+ # (1..5) === (1..5) # => true
7
+ # (1..5) === (2..3) # => true
8
+ # (1..5) === (1...6) # => true
9
+ # (1..5) === (2..6) # => false
10
+ #
11
+ # The native Range#=== behavior is untouched.
12
+ # ('a'..'f') === ('c') # => true
13
+ # (5..9) === (11) # => false
14
+ #
15
+ # The given range must be fully bounded, with both start and end.
16
+ def ===(value)
17
+ if value.is_a?(::Range)
18
+ # 1...10 includes 1..9 but it does not include 1..10.
19
+ # 1..10 includes 1...11 but it does not include 1...12.
20
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
21
+ value_max = !exclude_end? && value.exclude_end? ? value.max : value.last
22
+ super(value.first) && (self.end.nil? || value_max.send(operator, last))
23
+ else
24
+ super
25
+ end
26
+ end
27
+
28
+ # Extends the default Range#include? to support range comparisons.
29
+ # (1..5).include?(1..5) # => true
30
+ # (1..5).include?(2..3) # => true
31
+ # (1..5).include?(1...6) # => true
32
+ # (1..5).include?(2..6) # => false
33
+ #
34
+ # The native Range#include? behavior is untouched.
35
+ # ('a'..'f').include?('c') # => true
36
+ # (5..9).include?(11) # => false
37
+ #
38
+ # The given range must be fully bounded, with both start and end.
39
+ def include?(value)
40
+ if value.is_a?(::Range)
41
+ # 1...10 includes 1..9 but it does not include 1..10.
42
+ # 1..10 includes 1...11 but it does not include 1...12.
43
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
44
+ value_max = !exclude_end? && value.exclude_end? ? value.max : value.last
45
+ super(value.first) && (self.end.nil? || value_max.send(operator, last))
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ # Extends the default Range#cover? to support range comparisons.
52
+ # (1..5).cover?(1..5) # => true
53
+ # (1..5).cover?(2..3) # => true
54
+ # (1..5).cover?(1...6) # => true
55
+ # (1..5).cover?(2..6) # => false
56
+ #
57
+ # The native Range#cover? behavior is untouched.
58
+ # ('a'..'f').cover?('c') # => true
59
+ # (5..9).cover?(11) # => false
60
+ #
61
+ # The given range must be fully bounded, with both start and end.
62
+ def cover?(value)
63
+ if value.is_a?(::Range)
64
+ # 1...10 covers 1..9 but it does not cover 1..10.
65
+ # 1..10 covers 1...11 but it does not cover 1...12.
66
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
67
+ value_max = !exclude_end? && value.exclude_end? ? value.max : value.last
68
+ super(value.first) && (self.end.nil? || value_max.send(operator, last))
69
+ else
70
+ super
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ Range.prepend(ActiveSupport::CompareWithRange)
@@ -1,19 +1,41 @@
1
- class Range
2
- RANGE_FORMATS = {
3
- :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
4
- }
1
+ # frozen_string_literal: true
5
2
 
6
- # Gives a human readable format of the range.
7
- #
8
- # (1..100).to_formatted_s # => "1..100"
9
- def to_formatted_s(format = :default)
10
- if formatter = RANGE_FORMATS[format]
11
- formatter.call(first, last)
12
- else
13
- to_default_s
3
+ module ActiveSupport
4
+ module RangeWithFormat
5
+ RANGE_FORMATS = {
6
+ db: -> (start, stop) do
7
+ case start
8
+ when String then "BETWEEN '#{start}' AND '#{stop}'"
9
+ else
10
+ "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'"
11
+ end
12
+ end
13
+ }
14
+
15
+ # Convert range to a formatted string. See RANGE_FORMATS for predefined formats.
16
+ #
17
+ # range = (1..100) # => 1..100
18
+ #
19
+ # range.to_s # => "1..100"
20
+ # range.to_s(:db) # => "BETWEEN '1' AND '100'"
21
+ #
22
+ # == Adding your own range formats to to_s
23
+ # You can add your own formats to the Range::RANGE_FORMATS hash.
24
+ # Use the format name as the hash key and a Proc instance.
25
+ #
26
+ # # config/initializers/range_formats.rb
27
+ # Range::RANGE_FORMATS[:short] = ->(start, stop) { "Between #{start.to_s(:db)} and #{stop.to_s(:db)}" }
28
+ def to_s(format = :default)
29
+ if formatter = RANGE_FORMATS[format]
30
+ formatter.call(first, last)
31
+ else
32
+ super()
33
+ end
14
34
  end
15
- end
16
35
 
17
- alias_method :to_default_s, :to_s
18
- alias_method :to_s, :to_formatted_s
36
+ alias_method :to_default_s, :to_s
37
+ alias_method :to_formatted_s, :to_s
38
+ end
19
39
  end
40
+
41
+ Range.prepend(ActiveSupport::RangeWithFormat)
@@ -1,23 +1,24 @@
1
- require 'active_support/core_ext/module/aliasing'
1
+ # frozen_string_literal: true
2
2
 
3
- class Range #:nodoc:
3
+ require "active_support/time_with_zone"
4
4
 
5
- def each_with_time_with_zone(&block)
6
- ensure_iteration_allowed
7
- each_without_time_with_zone(&block)
8
- end
9
- alias_method_chain :each, :time_with_zone
10
-
11
- def step_with_time_with_zone(n = 1, &block)
12
- ensure_iteration_allowed
13
- step_without_time_with_zone(n, &block)
14
- end
15
- alias_method_chain :step, :time_with_zone
5
+ module ActiveSupport
6
+ module EachTimeWithZone #:nodoc:
7
+ def each(&block)
8
+ ensure_iteration_allowed
9
+ super
10
+ end
16
11
 
17
- private
18
- def ensure_iteration_allowed
19
- if first.is_a?(Time)
20
- raise TypeError, "can't iterate from #{first.class}"
12
+ def step(n = 1, &block)
13
+ ensure_iteration_allowed
14
+ super
21
15
  end
16
+
17
+ private
18
+ def ensure_iteration_allowed
19
+ raise TypeError, "can't iterate from #{first.class}" if first.is_a?(TimeWithZone)
20
+ end
22
21
  end
23
22
  end
23
+
24
+ Range.prepend(ActiveSupport::EachTimeWithZone)
@@ -1,23 +1,9 @@
1
- require 'active_support/core_ext/module/aliasing'
1
+ # frozen_string_literal: true
2
2
 
3
- class Range
4
- # Extends the default Range#include? to support range comparisons.
5
- # (1..5).include?(1..5) # => true
6
- # (1..5).include?(2..3) # => true
7
- # (1..5).include?(2..6) # => false
8
- #
9
- # The native Range#include? behavior is untouched.
10
- # ('a'..'f').include?('c') # => true
11
- # (5..9).include?(11) # => false
12
- def include_with_range?(value)
13
- if value.is_a?(::Range)
14
- # 1...10 includes 1..9 but it does not include 1..10.
15
- operator = exclude_end? && !value.exclude_end? ? :< : :<=
16
- include_without_range?(value.first) && value.last.send(operator, last)
17
- else
18
- include_without_range?(value)
19
- end
20
- end
3
+ require "active_support/deprecation"
21
4
 
22
- alias_method_chain :include?, :range
23
- end
5
+ ActiveSupport::Deprecation.warn "You have required `active_support/core_ext/range/include_range`. " \
6
+ "This file will be removed in Rails 6.1. You should require `active_support/core_ext/range/compare_range` " \
7
+ "instead."
8
+
9
+ require "active_support/core_ext/range/compare_range"
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/time_with_zone"
4
+
5
+ module ActiveSupport
6
+ module IncludeTimeWithZone #:nodoc:
7
+ # Extends the default Range#include? to support ActiveSupport::TimeWithZone.
8
+ #
9
+ # (1.hour.ago..1.hour.from_now).include?(Time.current) # => true
10
+ #
11
+ def include?(value)
12
+ if self.begin.is_a?(TimeWithZone)
13
+ cover?(value)
14
+ elsif self.end.is_a?(TimeWithZone)
15
+ cover?(value)
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Range.prepend(ActiveSupport::IncludeTimeWithZone)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Range
2
4
  # Compare two ranges and see if they overlap each other
3
5
  # (1..5).overlaps?(4..6) # => true
@@ -1,4 +1,7 @@
1
- require 'active_support/core_ext/range/conversions'
2
- require 'active_support/core_ext/range/include_range'
3
- require 'active_support/core_ext/range/overlaps'
4
- require 'active_support/core_ext/range/each'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/range/conversions"
4
+ require "active_support/core_ext/range/compare_range"
5
+ require "active_support/core_ext/range/include_time_with_zone"
6
+ require "active_support/core_ext/range/overlaps"
7
+ require "active_support/core_ext/range/each"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Regexp #:nodoc:
2
4
  def multiline?
3
5
  options & MULTILINE == MULTILINE