activesupport 5.0.0 → 6.1.0

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 (268) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +343 -590
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -4
  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 +11 -5
  8. data/lib/active_support/backtrace_cleaner.rb +33 -5
  9. data/lib/active_support/benchmarkable.rb +5 -3
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache/file_store.rb +45 -53
  12. data/lib/active_support/cache/mem_cache_store.rb +81 -79
  13. data/lib/active_support/cache/memory_store.rb +69 -41
  14. data/lib/active_support/cache/null_store.rb +11 -4
  15. data/lib/active_support/cache/redis_cache_store.rb +493 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +74 -37
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  18. data/lib/active_support/cache.rb +332 -161
  19. data/lib/active_support/callbacks.rb +657 -586
  20. data/lib/active_support/concern.rb +79 -6
  21. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +35 -0
  22. data/lib/active_support/concurrency/share_lock.rb +59 -19
  23. data/lib/active_support/configurable.rb +15 -17
  24. data/lib/active_support/configuration_file.rb +46 -0
  25. data/lib/active_support/core_ext/array/access.rb +21 -7
  26. data/lib/active_support/core_ext/array/conversions.rb +20 -18
  27. data/lib/active_support/core_ext/array/extract.rb +21 -0
  28. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  29. data/lib/active_support/core_ext/array/grouping.rb +3 -1
  30. data/lib/active_support/core_ext/array/inquiry.rb +3 -1
  31. data/lib/active_support/core_ext/array/wrap.rb +2 -0
  32. data/lib/active_support/core_ext/array.rb +9 -7
  33. data/lib/active_support/core_ext/benchmark.rb +5 -3
  34. data/lib/active_support/core_ext/big_decimal/conversions.rb +6 -6
  35. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  36. data/lib/active_support/core_ext/class/attribute.rb +52 -49
  37. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  38. data/lib/active_support/core_ext/class/subclasses.rb +18 -26
  39. data/lib/active_support/core_ext/class.rb +4 -2
  40. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  41. data/lib/active_support/core_ext/date/blank.rb +3 -1
  42. data/lib/active_support/core_ext/date/calculations.rb +16 -13
  43. data/lib/active_support/core_ext/date/conversions.rb +23 -21
  44. data/lib/active_support/core_ext/date/zones.rb +4 -2
  45. data/lib/active_support/core_ext/date.rb +7 -5
  46. data/lib/active_support/core_ext/date_and_time/calculations.rb +82 -53
  47. data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -5
  48. data/lib/active_support/core_ext/date_and_time/zones.rb +9 -9
  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 +3 -1
  51. data/lib/active_support/core_ext/date_time/calculations.rb +23 -11
  52. data/lib/active_support/core_ext/date_time/compatibility.rb +15 -2
  53. data/lib/active_support/core_ext/date_time/conversions.rb +14 -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 +165 -29
  58. data/lib/active_support/core_ext/file/atomic.rb +7 -5
  59. data/lib/active_support/core_ext/file.rb +3 -1
  60. data/lib/active_support/core_ext/hash/conversions.rb +40 -39
  61. data/lib/active_support/core_ext/hash/deep_merge.rb +8 -12
  62. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  63. data/lib/active_support/core_ext/hash/except.rb +4 -2
  64. data/lib/active_support/core_ext/hash/indifferent_access.rb +3 -2
  65. data/lib/active_support/core_ext/hash/keys.rb +9 -36
  66. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  67. data/lib/active_support/core_ext/hash/slice.rb +8 -29
  68. data/lib/active_support/core_ext/hash.rb +10 -9
  69. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  70. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  71. data/lib/active_support/core_ext/integer/time.rb +11 -18
  72. data/lib/active_support/core_ext/integer.rb +5 -3
  73. data/lib/active_support/core_ext/kernel/concern.rb +3 -1
  74. data/lib/active_support/core_ext/kernel/reporting.rb +3 -1
  75. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  76. data/lib/active_support/core_ext/kernel.rb +5 -4
  77. data/lib/active_support/core_ext/load_error.rb +2 -23
  78. data/lib/active_support/core_ext/marshal.rb +6 -2
  79. data/lib/active_support/core_ext/module/aliasing.rb +5 -48
  80. data/lib/active_support/core_ext/module/anonymous.rb +2 -0
  81. data/lib/active_support/core_ext/module/attr_internal.rb +7 -5
  82. data/lib/active_support/core_ext/module/attribute_accessors.rb +53 -59
  83. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +31 -24
  84. data/lib/active_support/core_ext/module/concerning.rb +16 -11
  85. data/lib/active_support/core_ext/module/delegation.rb +159 -44
  86. data/lib/active_support/core_ext/module/deprecation.rb +2 -0
  87. data/lib/active_support/core_ext/module/introspection.rb +23 -26
  88. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  89. data/lib/active_support/core_ext/module/remove_method.rb +5 -23
  90. data/lib/active_support/core_ext/module.rb +13 -12
  91. data/lib/active_support/core_ext/name_error.rb +36 -2
  92. data/lib/active_support/core_ext/numeric/bytes.rb +2 -0
  93. data/lib/active_support/core_ext/numeric/conversions.rb +129 -134
  94. data/lib/active_support/core_ext/numeric/time.rb +18 -26
  95. data/lib/active_support/core_ext/numeric.rb +5 -4
  96. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  97. data/lib/active_support/core_ext/object/blank.rb +14 -2
  98. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  99. data/lib/active_support/core_ext/object/deep_dup.rb +4 -2
  100. data/lib/active_support/core_ext/object/duplicable.rb +13 -62
  101. data/lib/active_support/core_ext/object/inclusion.rb +3 -1
  102. data/lib/active_support/core_ext/object/instance_variables.rb +2 -0
  103. data/lib/active_support/core_ext/object/json.rb +42 -15
  104. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  105. data/lib/active_support/core_ext/object/to_query.rb +10 -5
  106. data/lib/active_support/core_ext/object/try.rb +20 -8
  107. data/lib/active_support/core_ext/object/with_options.rb +15 -2
  108. data/lib/active_support/core_ext/object.rb +14 -12
  109. data/lib/active_support/core_ext/range/compare_range.rb +82 -0
  110. data/lib/active_support/core_ext/range/conversions.rb +35 -25
  111. data/lib/active_support/core_ext/range/each.rb +5 -2
  112. data/lib/active_support/core_ext/range/include_time_with_zone.rb +28 -0
  113. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  114. data/lib/active_support/core_ext/range.rb +7 -4
  115. data/lib/active_support/core_ext/regexp.rb +10 -1
  116. data/lib/active_support/core_ext/securerandom.rb +28 -6
  117. data/lib/active_support/core_ext/string/access.rb +9 -18
  118. data/lib/active_support/core_ext/string/behavior.rb +2 -0
  119. data/lib/active_support/core_ext/string/conversions.rb +5 -2
  120. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  121. data/lib/active_support/core_ext/string/filters.rb +47 -4
  122. data/lib/active_support/core_ext/string/indent.rb +6 -4
  123. data/lib/active_support/core_ext/string/inflections.rb +78 -29
  124. data/lib/active_support/core_ext/string/inquiry.rb +4 -1
  125. data/lib/active_support/core_ext/string/multibyte.rb +10 -5
  126. data/lib/active_support/core_ext/string/output_safety.rb +86 -31
  127. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
  128. data/lib/active_support/core_ext/string/strip.rb +5 -1
  129. data/lib/active_support/core_ext/string/zones.rb +4 -2
  130. data/lib/active_support/core_ext/string.rb +15 -13
  131. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +14 -0
  132. data/lib/active_support/core_ext/symbol.rb +3 -0
  133. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  134. data/lib/active_support/core_ext/time/calculations.rb +117 -45
  135. data/lib/active_support/core_ext/time/compatibility.rb +13 -2
  136. data/lib/active_support/core_ext/time/conversions.rb +18 -12
  137. data/lib/active_support/core_ext/time/zones.rb +9 -7
  138. data/lib/active_support/core_ext/time.rb +7 -5
  139. data/lib/active_support/core_ext/uri.rb +12 -7
  140. data/lib/active_support/core_ext.rb +3 -2
  141. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  142. data/lib/active_support/current_attributes.rb +208 -0
  143. data/lib/active_support/dependencies/autoload.rb +2 -0
  144. data/lib/active_support/dependencies/interlock.rb +7 -1
  145. data/lib/active_support/dependencies/zeitwerk_integration.rb +117 -0
  146. data/lib/active_support/dependencies.rb +172 -98
  147. data/lib/active_support/deprecation/behaviors.rb +45 -13
  148. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  149. data/lib/active_support/deprecation/disallowed.rb +56 -0
  150. data/lib/active_support/deprecation/instance_delegator.rb +16 -2
  151. data/lib/active_support/deprecation/method_wrappers.rb +32 -17
  152. data/lib/active_support/deprecation/proxy_wrappers.rb +35 -7
  153. data/lib/active_support/deprecation/reporting.rb +61 -16
  154. data/lib/active_support/deprecation.rb +17 -9
  155. data/lib/active_support/descendants_tracker.rb +61 -9
  156. data/lib/active_support/digest.rb +20 -0
  157. data/lib/active_support/duration/iso8601_parser.rb +67 -66
  158. data/lib/active_support/duration/iso8601_serializer.rb +25 -17
  159. data/lib/active_support/duration.rb +349 -46
  160. data/lib/active_support/encrypted_configuration.rb +45 -0
  161. data/lib/active_support/encrypted_file.rb +117 -0
  162. data/lib/active_support/environment_inquirer.rb +20 -0
  163. data/lib/active_support/evented_file_update_checker.rb +88 -112
  164. data/lib/active_support/execution_wrapper.rb +25 -13
  165. data/lib/active_support/executor.rb +3 -1
  166. data/lib/active_support/file_update_checker.rb +56 -51
  167. data/lib/active_support/fork_tracker.rb +62 -0
  168. data/lib/active_support/gem_version.rb +4 -2
  169. data/lib/active_support/gzip.rb +7 -5
  170. data/lib/active_support/hash_with_indifferent_access.rb +153 -49
  171. data/lib/active_support/i18n.rb +9 -6
  172. data/lib/active_support/i18n_railtie.rb +30 -20
  173. data/lib/active_support/inflections.rb +13 -11
  174. data/lib/active_support/inflector/inflections.rb +28 -15
  175. data/lib/active_support/inflector/methods.rb +120 -109
  176. data/lib/active_support/inflector/transliterate.rb +60 -25
  177. data/lib/active_support/inflector.rb +7 -5
  178. data/lib/active_support/json/decoding.rb +30 -29
  179. data/lib/active_support/json/encoding.rb +22 -11
  180. data/lib/active_support/json.rb +4 -2
  181. data/lib/active_support/key_generator.rb +6 -36
  182. data/lib/active_support/lazy_load_hooks.rb +53 -20
  183. data/lib/active_support/locale/en.rb +33 -0
  184. data/lib/active_support/locale/en.yml +7 -3
  185. data/lib/active_support/log_subscriber/test_helper.rb +11 -9
  186. data/lib/active_support/log_subscriber.rb +51 -18
  187. data/lib/active_support/logger.rb +9 -22
  188. data/lib/active_support/logger_silence.rb +14 -21
  189. data/lib/active_support/logger_thread_safe_level.rb +55 -8
  190. data/lib/active_support/message_encryptor.rb +170 -53
  191. data/lib/active_support/message_verifier.rb +91 -20
  192. data/lib/active_support/messages/metadata.rb +80 -0
  193. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  194. data/lib/active_support/messages/rotator.rb +57 -0
  195. data/lib/active_support/multibyte/chars.rb +24 -78
  196. data/lib/active_support/multibyte/unicode.rb +21 -352
  197. data/lib/active_support/multibyte.rb +4 -2
  198. data/lib/active_support/notifications/fanout.rb +121 -19
  199. data/lib/active_support/notifications/instrumenter.rb +78 -14
  200. data/lib/active_support/notifications.rb +80 -12
  201. data/lib/active_support/number_helper/number_converter.rb +17 -16
  202. data/lib/active_support/number_helper/number_to_currency_converter.rb +6 -9
  203. data/lib/active_support/number_helper/number_to_delimited_converter.rb +5 -3
  204. data/lib/active_support/number_helper/number_to_human_converter.rb +13 -12
  205. data/lib/active_support/number_helper/number_to_human_size_converter.rb +11 -13
  206. data/lib/active_support/number_helper/number_to_percentage_converter.rb +5 -1
  207. data/lib/active_support/number_helper/number_to_phone_converter.rb +5 -4
  208. data/lib/active_support/number_helper/number_to_rounded_converter.rb +18 -55
  209. data/lib/active_support/number_helper/rounding_helper.rb +50 -0
  210. data/lib/active_support/number_helper.rb +45 -16
  211. data/lib/active_support/option_merger.rb +25 -4
  212. data/lib/active_support/ordered_hash.rb +6 -4
  213. data/lib/active_support/ordered_options.rb +23 -9
  214. data/lib/active_support/parameter_filter.rb +133 -0
  215. data/lib/active_support/per_thread_registry.rb +7 -5
  216. data/lib/active_support/proxy_object.rb +2 -0
  217. data/lib/active_support/rails.rb +8 -9
  218. data/lib/active_support/railtie.rb +62 -11
  219. data/lib/active_support/reloader.rb +12 -11
  220. data/lib/active_support/rescuable.rb +20 -11
  221. data/lib/active_support/secure_compare_rotator.rb +51 -0
  222. data/lib/active_support/security_utils.rb +26 -15
  223. data/lib/active_support/string_inquirer.rb +12 -3
  224. data/lib/active_support/subscriber.rb +77 -23
  225. data/lib/active_support/tagged_logging.rb +52 -17
  226. data/lib/active_support/test_case.rb +106 -29
  227. data/lib/active_support/testing/assertions.rb +144 -8
  228. data/lib/active_support/testing/autorun.rb +5 -10
  229. data/lib/active_support/testing/constant_lookup.rb +2 -1
  230. data/lib/active_support/testing/declarative.rb +3 -1
  231. data/lib/active_support/testing/deprecation.rb +4 -2
  232. data/lib/active_support/testing/file_fixtures.rb +4 -0
  233. data/lib/active_support/testing/isolation.rb +19 -24
  234. data/lib/active_support/testing/method_call_assertions.rb +31 -2
  235. data/lib/active_support/testing/parallelization/server.rb +78 -0
  236. data/lib/active_support/testing/parallelization/worker.rb +100 -0
  237. data/lib/active_support/testing/parallelization.rb +51 -0
  238. data/lib/active_support/testing/setup_and_teardown.rb +13 -8
  239. data/lib/active_support/testing/stream.rb +30 -29
  240. data/lib/active_support/testing/tagged_logging.rb +3 -1
  241. data/lib/active_support/testing/time_helpers.rb +125 -24
  242. data/lib/active_support/time.rb +14 -12
  243. data/lib/active_support/time_with_zone.rb +142 -55
  244. data/lib/active_support/values/time_zone.rb +160 -53
  245. data/lib/active_support/version.rb +3 -1
  246. data/lib/active_support/xml_mini/jdom.rb +115 -114
  247. data/lib/active_support/xml_mini/libxml.rb +15 -14
  248. data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
  249. data/lib/active_support/xml_mini/nokogiri.rb +13 -13
  250. data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
  251. data/lib/active_support/xml_mini/rexml.rb +18 -9
  252. data/lib/active_support/xml_mini.rb +44 -42
  253. data/lib/active_support.rb +19 -10
  254. metadata +79 -37
  255. data/lib/active_support/concurrency/latch.rb +0 -19
  256. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
  257. data/lib/active_support/core_ext/hash/compact.rb +0 -20
  258. data/lib/active_support/core_ext/hash/transform_values.rb +0 -29
  259. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  260. data/lib/active_support/core_ext/kernel/debugger.rb +0 -3
  261. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -3
  262. data/lib/active_support/core_ext/module/qualified_const.rb +0 -70
  263. data/lib/active_support/core_ext/module/reachable.rb +0 -8
  264. data/lib/active_support/core_ext/numeric/inquiry.rb +0 -26
  265. data/lib/active_support/core_ext/range/include_range.rb +0 -23
  266. data/lib/active_support/core_ext/struct.rb +0 -3
  267. data/lib/active_support/core_ext/time/marshal.rb +0 -3
  268. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Hash
2
4
  # Merges the caller into +other_hash+. For example,
3
5
  #
@@ -12,11 +14,12 @@ class Hash
12
14
  def reverse_merge(other_hash)
13
15
  other_hash.merge(self)
14
16
  end
17
+ alias_method :with_defaults, :reverse_merge
15
18
 
16
19
  # Destructive +reverse_merge+.
17
20
  def reverse_merge!(other_hash)
18
- # right wins if there is no left
19
- merge!( other_hash ){|key,left,right| left }
21
+ replace(reverse_merge(other_hash))
20
22
  end
21
23
  alias_method :reverse_update, :reverse_merge!
24
+ alias_method :with_defaults!, :reverse_merge!
22
25
  end
@@ -1,35 +1,13 @@
1
- class Hash
2
- # Slices a hash to include only the given keys. Returns a hash containing
3
- # the given keys.
4
- #
5
- # { a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b)
6
- # # => {:a=>1, :b=>2}
7
- #
8
- # This is useful for limiting an options hash to valid keys before
9
- # passing to a method:
10
- #
11
- # def search(criteria = {})
12
- # criteria.assert_valid_keys(:mass, :velocity, :time)
13
- # end
14
- #
15
- # search(options.slice(:mass, :velocity, :time))
16
- #
17
- # If you have an array of keys you want to limit to, you should splat them:
18
- #
19
- # valid_keys = [:mass, :velocity, :time]
20
- # search(options.slice(*valid_keys))
21
- def slice(*keys)
22
- keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
23
- keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
24
- end
1
+ # frozen_string_literal: true
25
2
 
3
+ class Hash
26
4
  # Replaces the hash with only the given keys.
27
5
  # Returns a hash containing the removed key/value pairs.
28
6
  #
29
- # { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
30
- # # => {:c=>3, :d=>4}
7
+ # hash = { a: 1, b: 2, c: 3, d: 4 }
8
+ # hash.slice!(:a, :b) # => {:c=>3, :d=>4}
9
+ # hash # => {:a=>1, :b=>2}
31
10
  def slice!(*keys)
32
- keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
33
11
  omit = slice(*self.keys - keys)
34
12
  hash = slice(*keys)
35
13
  hash.default = default
@@ -40,8 +18,9 @@ class Hash
40
18
 
41
19
  # Removes and returns the key/value pairs matching the given keys.
42
20
  #
43
- # { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
44
- # { a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
21
+ # hash = { a: 1, b: 2, c: 3, d: 4 }
22
+ # hash.extract!(:a, :b) # => {:a=>1, :b=>2}
23
+ # hash # => {:c=>3, :d=>4}
45
24
  def extract!(*keys)
46
25
  keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
47
26
  end
@@ -1,9 +1,10 @@
1
- require 'active_support/core_ext/hash/compact'
2
- require 'active_support/core_ext/hash/conversions'
3
- require 'active_support/core_ext/hash/deep_merge'
4
- require 'active_support/core_ext/hash/except'
5
- require 'active_support/core_ext/hash/indifferent_access'
6
- require 'active_support/core_ext/hash/keys'
7
- require 'active_support/core_ext/hash/reverse_merge'
8
- require 'active_support/core_ext/hash/slice'
9
- require 'active_support/core_ext/hash/transform_values'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/hash/conversions"
4
+ require "active_support/core_ext/hash/deep_merge"
5
+ require "active_support/core_ext/hash/deep_transform_values"
6
+ require "active_support/core_ext/hash/except"
7
+ require "active_support/core_ext/hash/indifferent_access"
8
+ require "active_support/core_ext/hash/keys"
9
+ require "active_support/core_ext/hash/reverse_merge"
10
+ require "active_support/core_ext/hash/slice"
@@ -1,4 +1,6 @@
1
- require 'active_support/inflector'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/inflector"
2
4
 
3
5
  class Integer
4
6
  # Ordinalize turns a number into an ordinal string used to denote the
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Integer
2
4
  # Check whether the integer is evenly divisible by the argument.
3
5
  #
@@ -5,6 +7,6 @@ class Integer
5
7
  # 6.multiple_of?(5) # => false
6
8
  # 10.multiple_of?(2) # => true
7
9
  def multiple_of?(number)
8
- number != 0 ? self % number == 0 : zero?
10
+ number == 0 ? self == 0 : self % number == 0
9
11
  end
10
12
  end
@@ -1,29 +1,22 @@
1
- require 'active_support/duration'
2
- require 'active_support/core_ext/numeric/time'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/duration"
4
+ require "active_support/core_ext/numeric/time"
3
5
 
4
6
  class Integer
5
- # Enables the use of time calculations and declarations, like <tt>45.minutes +
6
- # 2.hours + 4.years</tt>.
7
- #
8
- # These methods use Time#advance for precise date calculations when using
9
- # <tt>from_now</tt>, +ago+, etc. as well as adding or subtracting their
10
- # results from a Time object.
11
- #
12
- # # equivalent to Time.now.advance(months: 1)
13
- # 1.month.from_now
7
+ # Returns a Duration instance matching the number of months provided.
14
8
  #
15
- # # equivalent to Time.now.advance(years: 2)
16
- # 2.years.from_now
17
- #
18
- # # equivalent to Time.now.advance(months: 4, years: 5)
19
- # (4.months + 5.years).from_now
9
+ # 2.months # => 2 months
20
10
  def months
21
- ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
11
+ ActiveSupport::Duration.months(self)
22
12
  end
23
13
  alias :month :months
24
14
 
15
+ # Returns a Duration instance matching the number of years provided.
16
+ #
17
+ # 2.years # => 2 years
25
18
  def years
26
- ActiveSupport::Duration.new(self * 365.25.days.to_i, [[:years, self]])
19
+ ActiveSupport::Duration.years(self)
27
20
  end
28
21
  alias :year :years
29
22
  end
@@ -1,3 +1,5 @@
1
- require 'active_support/core_ext/integer/multiple'
2
- require 'active_support/core_ext/integer/inflections'
3
- require 'active_support/core_ext/integer/time'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/integer/multiple"
4
+ require "active_support/core_ext/integer/inflections"
5
+ require "active_support/core_ext/integer/time"
@@ -1,4 +1,6 @@
1
- require 'active_support/core_ext/module/concerning'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/concerning"
2
4
 
3
5
  module Kernel
4
6
  module_function
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kernel
2
4
  module_function
3
5
 
4
- # Sets $VERBOSE to nil for the duration of the block and back to its original
6
+ # Sets $VERBOSE to +nil+ for the duration of the block and back to its original
5
7
  # value afterwards.
6
8
  #
7
9
  # silence_warnings do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kernel
2
4
  # class_eval on an object acts like singleton_class.class_eval.
3
5
  def class_eval(*args, &block)
@@ -1,4 +1,5 @@
1
- require 'active_support/core_ext/kernel/agnostics'
2
- require 'active_support/core_ext/kernel/concern'
3
- require 'active_support/core_ext/kernel/reporting'
4
- require 'active_support/core_ext/kernel/singleton_class'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/kernel/concern"
4
+ require "active_support/core_ext/kernel/reporting"
5
+ require "active_support/core_ext/kernel/singleton_class"
@@ -1,30 +1,9 @@
1
- require 'active_support/deprecation/proxy_wrappers'
1
+ # frozen_string_literal: true
2
2
 
3
3
  class LoadError
4
- REGEXPS = [
5
- /^no such file to load -- (.+)$/i,
6
- /^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
7
- /^Missing API definition file in (.+)$/i,
8
- /^cannot load such file -- (.+)$/i,
9
- ]
10
-
11
- unless method_defined?(:path)
12
- # Returns the path which was unable to be loaded.
13
- def path
14
- @path ||= begin
15
- REGEXPS.find do |regex|
16
- message =~ regex
17
- end
18
- $1
19
- end
20
- end
21
- end
22
-
23
4
  # Returns true if the given path name (except perhaps for the ".rb"
24
5
  # extension) is the missing file which caused the exception to be raised.
25
6
  def is_missing?(location)
26
- location.sub(/\.rb$/, ''.freeze) == path.sub(/\.rb$/, ''.freeze)
7
+ location.delete_suffix(".rb") == path.to_s.delete_suffix(".rb")
27
8
  end
28
9
  end
29
-
30
- MissingSourceFile = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('MissingSourceFile', 'LoadError')
@@ -1,7 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/string/inflections"
4
+
1
5
  module ActiveSupport
2
6
  module MarshalWithAutoloading # :nodoc:
3
- def load(source)
4
- super(source)
7
+ def load(source, proc = nil)
8
+ super(source, proc)
5
9
  rescue ArgumentError, NameError => exc
6
10
  if exc.message.match(%r|undefined class/module (.+?)(?:::)?\z|)
7
11
  # try loading the class/module
@@ -1,52 +1,6 @@
1
- class Module
2
- # NOTE: This method is deprecated. Please use <tt>Module#prepend</tt> that
3
- # comes with Ruby 2.0 or newer instead.
4
- #
5
- # Encapsulates the common pattern of:
6
- #
7
- # alias_method :foo_without_feature, :foo
8
- # alias_method :foo, :foo_with_feature
9
- #
10
- # With this, you simply do:
11
- #
12
- # alias_method_chain :foo, :feature
13
- #
14
- # And both aliases are set up for you.
15
- #
16
- # Query and bang methods (foo?, foo!) keep the same punctuation:
17
- #
18
- # alias_method_chain :foo?, :feature
19
- #
20
- # is equivalent to
21
- #
22
- # alias_method :foo_without_feature?, :foo?
23
- # alias_method :foo?, :foo_with_feature?
24
- #
25
- # so you can safely chain foo, foo?, foo! and/or foo= with the same feature.
26
- def alias_method_chain(target, feature)
27
- ActiveSupport::Deprecation.warn("alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super.")
28
-
29
- # Strip out punctuation on predicates, bang or writer methods since
30
- # e.g. target?_without_feature is not a valid method name.
31
- aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
32
- yield(aliased_target, punctuation) if block_given?
33
-
34
- with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
35
- without_method = "#{aliased_target}_without_#{feature}#{punctuation}"
36
-
37
- alias_method without_method, target
38
- alias_method target, with_method
39
-
40
- case
41
- when public_method_defined?(without_method)
42
- public target
43
- when protected_method_defined?(without_method)
44
- protected target
45
- when private_method_defined?(without_method)
46
- private target
47
- end
48
- end
1
+ # frozen_string_literal: true
49
2
 
3
+ class Module
50
4
  # Allows you to make aliases for attributes, which includes
51
5
  # getter, setter, and a predicate.
52
6
  #
@@ -65,6 +19,9 @@ class Module
65
19
  # e.subject = "Megastars"
66
20
  # e.title # => "Megastars"
67
21
  def alias_attribute(new_name, old_name)
22
+ # The following reader methods use an explicit `self` receiver in order to
23
+ # support aliases that start with an uppercase letter. Otherwise, they would
24
+ # be resolved as constants instead.
68
25
  module_eval <<-STR, __FILE__, __LINE__ + 1
69
26
  def #{new_name}; self.#{old_name}; end # def subject; self.title; end
70
27
  def #{new_name}?; self.#{old_name}?; end # def subject?; self.title?; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Module
2
4
  # A module may or may not have a name.
3
5
  #
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Module
2
4
  # Declares an attribute reader backed by an internally-named instance variable.
3
5
  def attr_internal_reader(*attrs)
4
- attrs.each {|attr_name| attr_internal_define(attr_name, :reader)}
6
+ attrs.each { |attr_name| attr_internal_define(attr_name, :reader) }
5
7
  end
6
8
 
7
9
  # Declares an attribute writer backed by an internally-named instance variable.
8
10
  def attr_internal_writer(*attrs)
9
- attrs.each {|attr_name| attr_internal_define(attr_name, :writer)}
11
+ attrs.each { |attr_name| attr_internal_define(attr_name, :writer) }
10
12
  end
11
13
 
12
14
  # Declares an attribute reader and writer backed by an internally-named instance
@@ -18,7 +20,7 @@ class Module
18
20
  alias_method :attr_internal, :attr_internal_accessor
19
21
 
20
22
  class << self; attr_accessor :attr_internal_naming_format end
21
- self.attr_internal_naming_format = '@_%s'
23
+ self.attr_internal_naming_format = "@_%s"
22
24
 
23
25
  private
24
26
  def attr_internal_ivar_name(attr)
@@ -26,9 +28,9 @@ class Module
26
28
  end
27
29
 
28
30
  def attr_internal_define(attr_name, type)
29
- internal_name = attr_internal_ivar_name(attr_name).sub(/\A@/, '')
31
+ internal_name = attr_internal_ivar_name(attr_name).delete_prefix("@")
30
32
  # use native attr_* methods as they are faster on some Ruby implementations
31
- send("attr_#{type}", internal_name)
33
+ public_send("attr_#{type}", internal_name)
32
34
  attr_name, internal_name = "#{attr_name}=", "#{internal_name}=" if type == :writer
33
35
  alias_method attr_name, internal_name
34
36
  remove_method internal_name
@@ -1,4 +1,4 @@
1
- require 'active_support/core_ext/array/extract_options'
1
+ # frozen_string_literal: true
2
2
 
3
3
  # Extends the module object with class/module and instance accessors for
4
4
  # class/module attributes, just like the native attr* accessors for instance
@@ -6,7 +6,8 @@ require 'active_support/core_ext/array/extract_options'
6
6
  class Module
7
7
  # Defines a class attribute and creates a class and instance reader methods.
8
8
  # The underlying class variable is set to +nil+, if it is not previously
9
- # defined.
9
+ # defined. All class and instance methods created will be public, even if
10
+ # this method is called with a private or protected access modifier.
10
11
  #
11
12
  # module HairColors
12
13
  # mattr_reader :hair_colors
@@ -23,7 +24,7 @@ class Module
23
24
  # end
24
25
  # # => NameError: invalid attribute name: 1_Badname
25
26
  #
26
- # If you want to opt out the creation on the instance reader method, pass
27
+ # To omit the instance reader method, pass
27
28
  # <tt>instance_reader: false</tt> or <tt>instance_accessor: false</tt>.
28
29
  #
29
30
  # module HairColors
@@ -36,13 +37,10 @@ class Module
36
37
  #
37
38
  # Person.new.hair_colors # => NoMethodError
38
39
  #
39
- #
40
- # Also, you can pass a block to set up the attribute with a default value.
40
+ # You can set a default value for the attribute.
41
41
  #
42
42
  # module HairColors
43
- # mattr_reader :hair_colors do
44
- # [:brown, :black, :blonde, :red]
45
- # end
43
+ # mattr_reader :hair_colors, default: [:brown, :black, :blonde, :red]
46
44
  # end
47
45
  #
48
46
  # class Person
@@ -50,32 +48,32 @@ class Module
50
48
  # end
51
49
  #
52
50
  # Person.new.hair_colors # => [:brown, :black, :blonde, :red]
53
- def mattr_reader(*syms)
54
- options = syms.extract_options!
51
+ def mattr_reader(*syms, instance_reader: true, instance_accessor: true, default: nil, location: nil)
52
+ raise TypeError, "module attributes should be defined directly on class, not singleton" if singleton_class?
53
+ location ||= caller_locations(1, 1).first
54
+
55
+ definition = []
55
56
  syms.each do |sym|
56
- raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /\A[_A-Za-z]\w*\z/
57
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
58
- @@#{sym} = nil unless defined? @@#{sym}
57
+ raise NameError.new("invalid attribute name: #{sym}") unless /\A[_A-Za-z]\w*\z/.match?(sym)
59
58
 
60
- def self.#{sym}
61
- @@#{sym}
62
- end
63
- EOS
59
+ definition << "def self.#{sym}; @@#{sym}; end"
64
60
 
65
- unless options[:instance_reader] == false || options[:instance_accessor] == false
66
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
67
- def #{sym}
68
- @@#{sym}
69
- end
70
- EOS
61
+ if instance_reader && instance_accessor
62
+ definition << "def #{sym}; @@#{sym}; end"
71
63
  end
72
- class_variable_set("@@#{sym}", yield) if block_given?
64
+
65
+ sym_default_value = (block_given? && default.nil?) ? yield : default
66
+ class_variable_set("@@#{sym}", sym_default_value) unless sym_default_value.nil? && class_variable_defined?("@@#{sym}")
73
67
  end
68
+
69
+ module_eval(definition.join(";"), location.path, location.lineno)
74
70
  end
75
71
  alias :cattr_reader :mattr_reader
76
72
 
77
73
  # Defines a class attribute and creates a class and instance writer methods to
78
- # allow assignment to the attribute.
74
+ # allow assignment to the attribute. All class and instance methods created
75
+ # will be public, even if this method is called with a private or protected
76
+ # access modifier.
79
77
  #
80
78
  # module HairColors
81
79
  # mattr_writer :hair_colors
@@ -90,7 +88,7 @@ class Module
90
88
  # Person.new.hair_colors = [:blonde, :red]
91
89
  # HairColors.class_variable_get("@@hair_colors") # => [:blonde, :red]
92
90
  #
93
- # If you want to opt out the instance writer method, pass
91
+ # To omit the instance writer method, pass
94
92
  # <tt>instance_writer: false</tt> or <tt>instance_accessor: false</tt>.
95
93
  #
96
94
  # module HairColors
@@ -103,12 +101,10 @@ class Module
103
101
  #
104
102
  # Person.new.hair_colors = [:blonde, :red] # => NoMethodError
105
103
  #
106
- # Also, you can pass a block to set up the attribute with a default value.
104
+ # You can set a default value for the attribute.
107
105
  #
108
106
  # module HairColors
109
- # mattr_writer :hair_colors do
110
- # [:brown, :black, :blonde, :red]
111
- # end
107
+ # mattr_writer :hair_colors, default: [:brown, :black, :blonde, :red]
112
108
  # end
113
109
  #
114
110
  # class Person
@@ -116,31 +112,30 @@ class Module
116
112
  # end
117
113
  #
118
114
  # Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
119
- def mattr_writer(*syms)
120
- options = syms.extract_options!
121
- syms.each do |sym|
122
- raise NameError.new("invalid attribute name: #{sym}") unless sym =~ /\A[_A-Za-z]\w*\z/
123
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
124
- @@#{sym} = nil unless defined? @@#{sym}
115
+ def mattr_writer(*syms, instance_writer: true, instance_accessor: true, default: nil, location: nil)
116
+ raise TypeError, "module attributes should be defined directly on class, not singleton" if singleton_class?
117
+ location ||= caller_locations(1, 1).first
125
118
 
126
- def self.#{sym}=(obj)
127
- @@#{sym} = obj
128
- end
129
- EOS
119
+ definition = []
120
+ syms.each do |sym|
121
+ raise NameError.new("invalid attribute name: #{sym}") unless /\A[_A-Za-z]\w*\z/.match?(sym)
122
+ definition << "def self.#{sym}=(val); @@#{sym} = val; end"
130
123
 
131
- unless options[:instance_writer] == false || options[:instance_accessor] == false
132
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
133
- def #{sym}=(obj)
134
- @@#{sym} = obj
135
- end
136
- EOS
124
+ if instance_writer && instance_accessor
125
+ definition << "def #{sym}=(val); @@#{sym} = val; end"
137
126
  end
138
- send("#{sym}=", yield) if block_given?
127
+
128
+ sym_default_value = (block_given? && default.nil?) ? yield : default
129
+ class_variable_set("@@#{sym}", sym_default_value) unless sym_default_value.nil? && class_variable_defined?("@@#{sym}")
139
130
  end
131
+
132
+ module_eval(definition.join(";"), location.path, location.lineno)
140
133
  end
141
134
  alias :cattr_writer :mattr_writer
142
135
 
143
136
  # Defines both class and instance accessors for class attributes.
137
+ # All class and instance methods created will be public, even if
138
+ # this method is called with a private or protected access modifier.
144
139
  #
145
140
  # module HairColors
146
141
  # mattr_accessor :hair_colors
@@ -158,14 +153,14 @@ class Module
158
153
  # parent class. Similarly if parent class changes the value then that would
159
154
  # change the value of subclasses too.
160
155
  #
161
- # class Male < Person
156
+ # class Citizen < Person
162
157
  # end
163
158
  #
164
- # Male.new.hair_colors << :blue
159
+ # Citizen.new.hair_colors << :blue
165
160
  # Person.new.hair_colors # => [:brown, :black, :blonde, :red, :blue]
166
161
  #
167
- # To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
168
- # To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
162
+ # To omit the instance writer method, pass <tt>instance_writer: false</tt>.
163
+ # To omit the instance reader method, pass <tt>instance_reader: false</tt>.
169
164
  #
170
165
  # module HairColors
171
166
  # mattr_accessor :hair_colors, instance_writer: false, instance_reader: false
@@ -178,7 +173,7 @@ class Module
178
173
  # Person.new.hair_colors = [:brown] # => NoMethodError
179
174
  # Person.new.hair_colors # => NoMethodError
180
175
  #
181
- # Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods.
176
+ # Or pass <tt>instance_accessor: false</tt>, to omit both instance methods.
182
177
  #
183
178
  # module HairColors
184
179
  # mattr_accessor :hair_colors, instance_accessor: false
@@ -191,12 +186,10 @@ class Module
191
186
  # Person.new.hair_colors = [:brown] # => NoMethodError
192
187
  # Person.new.hair_colors # => NoMethodError
193
188
  #
194
- # Also you can pass a block to set up the attribute with a default value.
189
+ # You can set a default value for the attribute.
195
190
  #
196
191
  # module HairColors
197
- # mattr_accessor :hair_colors do
198
- # [:brown, :black, :blonde, :red]
199
- # end
192
+ # mattr_accessor :hair_colors, default: [:brown, :black, :blonde, :red]
200
193
  # end
201
194
  #
202
195
  # class Person
@@ -204,9 +197,10 @@ class Module
204
197
  # end
205
198
  #
206
199
  # Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red]
207
- def mattr_accessor(*syms, &blk)
208
- mattr_reader(*syms, &blk)
209
- mattr_writer(*syms)
200
+ def mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)
201
+ location = caller_locations(1, 1).first
202
+ mattr_reader(*syms, instance_reader: instance_reader, instance_accessor: instance_accessor, default: default, location: location, &blk)
203
+ mattr_writer(*syms, instance_writer: instance_writer, instance_accessor: instance_accessor, default: default, location: location)
210
204
  end
211
205
  alias :cattr_accessor :mattr_accessor
212
206
  end