activesupport 5.1.7 → 7.0.4.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (279) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +259 -585
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +6 -5
  5. data/lib/active_support/actionable_error.rb +48 -0
  6. data/lib/active_support/all.rb +2 -0
  7. data/lib/active_support/array_inquirer.rb +4 -2
  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 +2 -0
  11. data/lib/active_support/cache/file_store.rb +50 -43
  12. data/lib/active_support/cache/mem_cache_store.rb +194 -67
  13. data/lib/active_support/cache/memory_store.rb +70 -34
  14. data/lib/active_support/cache/null_store.rb +18 -3
  15. data/lib/active_support/cache/redis_cache_store.rb +474 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +73 -50
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +2 -0
  18. data/lib/active_support/cache.rb +556 -220
  19. data/lib/active_support/callbacks.rb +264 -159
  20. data/lib/active_support/code_generator.rb +65 -0
  21. data/lib/active_support/concern.rb +81 -8
  22. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +16 -0
  23. data/lib/active_support/concurrency/share_lock.rb +4 -3
  24. data/lib/active_support/configurable.rb +17 -16
  25. data/lib/active_support/configuration_file.rb +51 -0
  26. data/lib/active_support/core_ext/array/access.rb +18 -8
  27. data/lib/active_support/core_ext/array/conversions.rb +20 -17
  28. data/lib/active_support/core_ext/array/deprecated_conversions.rb +25 -0
  29. data/lib/active_support/core_ext/array/extract.rb +21 -0
  30. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  31. data/lib/active_support/core_ext/array/grouping.rb +8 -6
  32. data/lib/active_support/core_ext/array/inquiry.rb +4 -2
  33. data/lib/active_support/core_ext/array/wrap.rb +2 -0
  34. data/lib/active_support/core_ext/array.rb +4 -1
  35. data/lib/active_support/core_ext/benchmark.rb +4 -2
  36. data/lib/active_support/core_ext/big_decimal/conversions.rb +3 -1
  37. data/lib/active_support/core_ext/big_decimal.rb +2 -0
  38. data/lib/active_support/core_ext/class/attribute.rb +50 -47
  39. data/lib/active_support/core_ext/class/attribute_accessors.rb +2 -0
  40. data/lib/active_support/core_ext/class/subclasses.rb +10 -24
  41. data/lib/active_support/core_ext/class.rb +2 -0
  42. data/lib/active_support/core_ext/date/acts_like.rb +2 -0
  43. data/lib/active_support/core_ext/date/blank.rb +3 -1
  44. data/lib/active_support/core_ext/date/calculations.rb +17 -14
  45. data/lib/active_support/core_ext/date/conversions.rb +24 -22
  46. data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
  47. data/lib/active_support/core_ext/date/zones.rb +2 -0
  48. data/lib/active_support/core_ext/date.rb +3 -0
  49. data/lib/active_support/core_ext/date_and_time/calculations.rb +65 -41
  50. data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -1
  51. data/lib/active_support/core_ext/date_and_time/zones.rb +2 -1
  52. data/lib/active_support/core_ext/date_time/acts_like.rb +2 -0
  53. data/lib/active_support/core_ext/date_time/blank.rb +3 -1
  54. data/lib/active_support/core_ext/date_time/calculations.rb +3 -1
  55. data/lib/active_support/core_ext/date_time/compatibility.rb +7 -5
  56. data/lib/active_support/core_ext/date_time/conversions.rb +15 -14
  57. data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
  58. data/lib/active_support/core_ext/date_time.rb +3 -0
  59. data/lib/active_support/core_ext/digest/uuid.rb +42 -14
  60. data/lib/active_support/core_ext/digest.rb +3 -0
  61. data/lib/active_support/core_ext/enumerable.rb +244 -72
  62. data/lib/active_support/core_ext/file/atomic.rb +6 -2
  63. data/lib/active_support/core_ext/file.rb +2 -0
  64. data/lib/active_support/core_ext/hash/conversions.rb +7 -6
  65. data/lib/active_support/core_ext/hash/deep_merge.rb +8 -12
  66. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  67. data/lib/active_support/core_ext/hash/except.rb +4 -2
  68. data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -3
  69. data/lib/active_support/core_ext/hash/keys.rb +4 -31
  70. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  71. data/lib/active_support/core_ext/hash/slice.rb +8 -29
  72. data/lib/active_support/core_ext/hash.rb +3 -2
  73. data/lib/active_support/core_ext/integer/inflections.rb +2 -0
  74. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  75. data/lib/active_support/core_ext/integer/time.rb +7 -14
  76. data/lib/active_support/core_ext/integer.rb +2 -0
  77. data/lib/active_support/core_ext/kernel/concern.rb +2 -0
  78. data/lib/active_support/core_ext/kernel/reporting.rb +6 -4
  79. data/lib/active_support/core_ext/kernel/singleton_class.rb +3 -1
  80. data/lib/active_support/core_ext/kernel.rb +2 -1
  81. data/lib/active_support/core_ext/load_error.rb +3 -8
  82. data/lib/active_support/core_ext/module/aliasing.rb +2 -0
  83. data/lib/active_support/core_ext/module/anonymous.rb +2 -0
  84. data/lib/active_support/core_ext/module/attr_internal.rb +4 -2
  85. data/lib/active_support/core_ext/module/attribute_accessors.rb +46 -56
  86. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +36 -27
  87. data/lib/active_support/core_ext/module/concerning.rb +15 -10
  88. data/lib/active_support/core_ext/module/delegation.rb +97 -58
  89. data/lib/active_support/core_ext/module/deprecation.rb +2 -0
  90. data/lib/active_support/core_ext/module/introspection.rb +18 -15
  91. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +5 -23
  93. data/lib/active_support/core_ext/module.rb +3 -1
  94. data/lib/active_support/core_ext/name_error.rb +30 -2
  95. data/lib/active_support/core_ext/numeric/bytes.rb +2 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +134 -129
  97. data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +7 -15
  99. data/lib/active_support/core_ext/numeric.rb +3 -1
  100. data/lib/active_support/core_ext/object/acts_like.rb +41 -6
  101. data/lib/active_support/core_ext/object/blank.rb +15 -5
  102. data/lib/active_support/core_ext/object/conversions.rb +2 -0
  103. data/lib/active_support/core_ext/object/deep_dup.rb +3 -1
  104. data/lib/active_support/core_ext/object/duplicable.rb +16 -110
  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 +51 -26
  108. data/lib/active_support/core_ext/object/to_param.rb +2 -0
  109. data/lib/active_support/core_ext/object/to_query.rb +4 -2
  110. data/lib/active_support/core_ext/object/try.rb +26 -14
  111. data/lib/active_support/core_ext/object/with_options.rb +24 -3
  112. data/lib/active_support/core_ext/object.rb +2 -0
  113. data/lib/active_support/core_ext/pathname/existence.rb +21 -0
  114. data/lib/active_support/core_ext/pathname.rb +3 -0
  115. data/lib/active_support/core_ext/range/compare_range.rb +57 -0
  116. data/lib/active_support/core_ext/range/conversions.rb +35 -25
  117. data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
  118. data/lib/active_support/core_ext/range/each.rb +6 -3
  119. data/lib/active_support/core_ext/range/include_time_with_zone.rb +7 -0
  120. data/lib/active_support/core_ext/range/overlaps.rb +3 -1
  121. data/lib/active_support/core_ext/range.rb +4 -1
  122. data/lib/active_support/core_ext/regexp.rb +10 -5
  123. data/lib/active_support/core_ext/securerandom.rb +25 -3
  124. data/lib/active_support/core_ext/string/access.rb +7 -16
  125. data/lib/active_support/core_ext/string/behavior.rb +2 -0
  126. data/lib/active_support/core_ext/string/conversions.rb +5 -2
  127. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  128. data/lib/active_support/core_ext/string/filters.rb +44 -1
  129. data/lib/active_support/core_ext/string/indent.rb +2 -0
  130. data/lib/active_support/core_ext/string/inflections.rb +69 -16
  131. data/lib/active_support/core_ext/string/inquiry.rb +4 -1
  132. data/lib/active_support/core_ext/string/multibyte.rb +9 -4
  133. data/lib/active_support/core_ext/string/output_safety.rb +135 -27
  134. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
  135. data/lib/active_support/core_ext/string/strip.rb +5 -1
  136. data/lib/active_support/core_ext/string/zones.rb +2 -0
  137. data/lib/active_support/core_ext/string.rb +2 -0
  138. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  139. data/lib/active_support/core_ext/symbol.rb +3 -0
  140. data/lib/active_support/core_ext/time/acts_like.rb +2 -0
  141. data/lib/active_support/core_ext/time/calculations.rb +81 -24
  142. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  143. data/lib/active_support/core_ext/time/conversions.rb +17 -12
  144. data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
  145. data/lib/active_support/core_ext/time/zones.rb +12 -25
  146. data/lib/active_support/core_ext/time.rb +3 -0
  147. data/lib/active_support/core_ext/uri.rb +4 -23
  148. data/lib/active_support/core_ext.rb +4 -1
  149. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  150. data/lib/active_support/current_attributes.rb +226 -0
  151. data/lib/active_support/dependencies/autoload.rb +2 -0
  152. data/lib/active_support/dependencies/interlock.rb +12 -18
  153. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  154. data/lib/active_support/dependencies.rb +59 -715
  155. data/lib/active_support/deprecation/behaviors.rb +48 -13
  156. data/lib/active_support/deprecation/constant_accessor.rb +4 -2
  157. data/lib/active_support/deprecation/disallowed.rb +56 -0
  158. data/lib/active_support/deprecation/instance_delegator.rb +2 -1
  159. data/lib/active_support/deprecation/method_wrappers.rb +29 -21
  160. data/lib/active_support/deprecation/proxy_wrappers.rb +34 -8
  161. data/lib/active_support/deprecation/reporting.rb +54 -9
  162. data/lib/active_support/deprecation.rb +10 -3
  163. data/lib/active_support/descendants_tracker.rb +192 -34
  164. data/lib/active_support/digest.rb +22 -0
  165. data/lib/active_support/duration/iso8601_parser.rb +9 -9
  166. data/lib/active_support/duration/iso8601_serializer.rb +29 -15
  167. data/lib/active_support/duration.rb +158 -72
  168. data/lib/active_support/encrypted_configuration.rb +56 -0
  169. data/lib/active_support/encrypted_file.rb +129 -0
  170. data/lib/active_support/environment_inquirer.rb +20 -0
  171. data/lib/active_support/error_reporter.rb +117 -0
  172. data/lib/active_support/evented_file_update_checker.rb +87 -122
  173. data/lib/active_support/execution_context/test_helper.rb +13 -0
  174. data/lib/active_support/execution_context.rb +53 -0
  175. data/lib/active_support/execution_wrapper.rb +46 -21
  176. data/lib/active_support/executor/test_helper.rb +7 -0
  177. data/lib/active_support/executor.rb +2 -0
  178. data/lib/active_support/file_update_checker.rb +2 -1
  179. data/lib/active_support/fork_tracker.rb +71 -0
  180. data/lib/active_support/gem_version.rb +7 -5
  181. data/lib/active_support/gzip.rb +2 -0
  182. data/lib/active_support/hash_with_indifferent_access.rb +126 -42
  183. data/lib/active_support/html_safe_translation.rb +43 -0
  184. data/lib/active_support/i18n.rb +5 -1
  185. data/lib/active_support/i18n_railtie.rb +19 -14
  186. data/lib/active_support/inflections.rb +2 -0
  187. data/lib/active_support/inflector/inflections.rb +41 -14
  188. data/lib/active_support/inflector/methods.rb +73 -87
  189. data/lib/active_support/inflector/transliterate.rb +56 -18
  190. data/lib/active_support/inflector.rb +2 -0
  191. data/lib/active_support/isolated_execution_state.rb +72 -0
  192. data/lib/active_support/json/decoding.rb +27 -26
  193. data/lib/active_support/json/encoding.rb +16 -6
  194. data/lib/active_support/json.rb +2 -0
  195. data/lib/active_support/key_generator.rb +25 -38
  196. data/lib/active_support/lazy_load_hooks.rb +35 -6
  197. data/lib/active_support/locale/en.rb +33 -0
  198. data/lib/active_support/locale/en.yml +8 -4
  199. data/lib/active_support/log_subscriber/test_helper.rb +4 -2
  200. data/lib/active_support/log_subscriber.rb +54 -13
  201. data/lib/active_support/logger.rb +4 -17
  202. data/lib/active_support/logger_silence.rb +13 -20
  203. data/lib/active_support/logger_thread_safe_level.rb +48 -10
  204. data/lib/active_support/message_encryptor.rb +111 -37
  205. data/lib/active_support/message_verifier.rb +124 -21
  206. data/lib/active_support/messages/metadata.rb +80 -0
  207. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  208. data/lib/active_support/messages/rotator.rb +57 -0
  209. data/lib/active_support/multibyte/chars.rb +19 -76
  210. data/lib/active_support/multibyte/unicode.rb +9 -331
  211. data/lib/active_support/multibyte.rb +3 -1
  212. data/lib/active_support/notifications/fanout.rb +165 -37
  213. data/lib/active_support/notifications/instrumenter.rb +92 -11
  214. data/lib/active_support/notifications.rb +96 -30
  215. data/lib/active_support/number_helper/number_converter.rb +8 -9
  216. data/lib/active_support/number_helper/number_to_currency_converter.rb +14 -12
  217. data/lib/active_support/number_helper/number_to_delimited_converter.rb +6 -3
  218. data/lib/active_support/number_helper/number_to_human_converter.rb +6 -3
  219. data/lib/active_support/number_helper/number_to_human_size_converter.rb +7 -4
  220. data/lib/active_support/number_helper/number_to_percentage_converter.rb +5 -1
  221. data/lib/active_support/number_helper/number_to_phone_converter.rb +6 -3
  222. data/lib/active_support/number_helper/number_to_rounded_converter.rb +14 -27
  223. data/lib/active_support/number_helper/rounding_helper.rb +16 -34
  224. data/lib/active_support/number_helper.rb +38 -12
  225. data/lib/active_support/option_merger.rb +19 -6
  226. data/lib/active_support/ordered_hash.rb +4 -2
  227. data/lib/active_support/ordered_options.rb +18 -6
  228. data/lib/active_support/parameter_filter.rb +138 -0
  229. data/lib/active_support/per_thread_registry.rb +8 -1
  230. data/lib/active_support/proxy_object.rb +2 -0
  231. data/lib/active_support/rails.rb +3 -10
  232. data/lib/active_support/railtie.rb +112 -11
  233. data/lib/active_support/reloader.rb +12 -11
  234. data/lib/active_support/rescuable.rb +19 -18
  235. data/lib/active_support/ruby_features.rb +7 -0
  236. data/lib/active_support/secure_compare_rotator.rb +51 -0
  237. data/lib/active_support/security_utils.rb +26 -15
  238. data/lib/active_support/string_inquirer.rb +4 -3
  239. data/lib/active_support/subscriber.rb +81 -42
  240. data/lib/active_support/tagged_logging.rb +45 -9
  241. data/lib/active_support/test_case.rb +86 -2
  242. data/lib/active_support/testing/assertions.rb +89 -21
  243. data/lib/active_support/testing/autorun.rb +2 -0
  244. data/lib/active_support/testing/constant_lookup.rb +2 -0
  245. data/lib/active_support/testing/declarative.rb +2 -0
  246. data/lib/active_support/testing/deprecation.rb +54 -2
  247. data/lib/active_support/testing/file_fixtures.rb +4 -0
  248. data/lib/active_support/testing/isolation.rb +6 -4
  249. data/lib/active_support/testing/method_call_assertions.rb +34 -5
  250. data/lib/active_support/testing/parallelization/server.rb +82 -0
  251. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  252. data/lib/active_support/testing/parallelization.rb +55 -0
  253. data/lib/active_support/testing/parallelize_executor.rb +76 -0
  254. data/lib/active_support/testing/setup_and_teardown.rb +12 -7
  255. data/lib/active_support/testing/stream.rb +6 -7
  256. data/lib/active_support/testing/tagged_logging.rb +3 -1
  257. data/lib/active_support/testing/time_helpers.rb +91 -15
  258. data/lib/active_support/time.rb +2 -0
  259. data/lib/active_support/time_with_zone.rb +168 -56
  260. data/lib/active_support/values/time_zone.rb +85 -37
  261. data/lib/active_support/version.rb +3 -1
  262. data/lib/active_support/xml_mini/jdom.rb +6 -5
  263. data/lib/active_support/xml_mini/libxml.rb +9 -7
  264. data/lib/active_support/xml_mini/libxmlsax.rb +7 -5
  265. data/lib/active_support/xml_mini/nokogiri.rb +8 -6
  266. data/lib/active_support/xml_mini/nokogirisax.rb +6 -4
  267. data/lib/active_support/xml_mini/rexml.rb +13 -4
  268. data/lib/active_support/xml_mini.rb +10 -15
  269. data/lib/active_support.rb +30 -9
  270. metadata +76 -35
  271. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
  272. data/lib/active_support/core_ext/hash/compact.rb +0 -27
  273. data/lib/active_support/core_ext/hash/transform_values.rb +0 -30
  274. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  275. data/lib/active_support/core_ext/marshal.rb +0 -22
  276. data/lib/active_support/core_ext/module/reachable.rb +0 -8
  277. data/lib/active_support/core_ext/numeric/inquiry.rb +0 -26
  278. data/lib/active_support/core_ext/range/include_range.rb +0 -23
  279. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -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
  #
@@ -26,93 +28,6 @@ class Object
26
28
  end
27
29
  end
28
30
 
29
- class NilClass
30
- begin
31
- nil.dup
32
- rescue TypeError
33
-
34
- # +nil+ is not duplicable:
35
- #
36
- # nil.duplicable? # => false
37
- # nil.dup # => TypeError: can't dup NilClass
38
- def duplicable?
39
- false
40
- end
41
- end
42
- end
43
-
44
- class FalseClass
45
- begin
46
- false.dup
47
- rescue TypeError
48
-
49
- # +false+ is not duplicable:
50
- #
51
- # false.duplicable? # => false
52
- # false.dup # => TypeError: can't dup FalseClass
53
- def duplicable?
54
- false
55
- end
56
- end
57
- end
58
-
59
- class TrueClass
60
- begin
61
- true.dup
62
- rescue TypeError
63
-
64
- # +true+ is not duplicable:
65
- #
66
- # true.duplicable? # => false
67
- # true.dup # => TypeError: can't dup TrueClass
68
- def duplicable?
69
- false
70
- end
71
- end
72
- end
73
-
74
- class Symbol
75
- begin
76
- :symbol.dup # Ruby 2.4.x.
77
- "symbol_from_string".to_sym.dup # Some symbols can't `dup` in Ruby 2.4.0.
78
- rescue TypeError
79
-
80
- # Symbols are not duplicable:
81
- #
82
- # :my_symbol.duplicable? # => false
83
- # :my_symbol.dup # => TypeError: can't dup Symbol
84
- def duplicable?
85
- false
86
- end
87
- end
88
- end
89
-
90
- class Numeric
91
- begin
92
- 1.dup
93
- rescue TypeError
94
-
95
- # Numbers are not duplicable:
96
- #
97
- # 3.duplicable? # => false
98
- # 3.dup # => TypeError: can't dup Integer
99
- def duplicable?
100
- false
101
- end
102
- end
103
- end
104
-
105
- require "bigdecimal"
106
- class BigDecimal
107
- # BigDecimals are duplicable:
108
- #
109
- # BigDecimal.new("1.2").duplicable? # => true
110
- # BigDecimal.new("1.2").dup # => #<BigDecimal:...,'0.12E1',18(18)>
111
- def duplicable?
112
- true
113
- end
114
- end
115
-
116
31
  class Method
117
32
  # Methods are not duplicable:
118
33
  #
@@ -123,32 +38,23 @@ class Method
123
38
  end
124
39
  end
125
40
 
126
- class Complex
127
- begin
128
- Complex(1).dup
129
- rescue TypeError
130
-
131
- # Complexes are not duplicable for RUBY_VERSION < 2.5.0:
132
- #
133
- # Complex(1).duplicable? # => false
134
- # Complex(1).dup # => TypeError: can't copy Complex
135
- def duplicable?
136
- false
137
- end
41
+ class UnboundMethod
42
+ # Unbound methods are not duplicable:
43
+ #
44
+ # method(:puts).unbind.duplicable? # => false
45
+ # method(:puts).unbind.dup # => TypeError: allocator undefined for UnboundMethod
46
+ def duplicable?
47
+ false
138
48
  end
139
49
  end
140
50
 
141
- class Rational
142
- begin
143
- Rational(1).dup
144
- rescue TypeError
51
+ require "singleton"
145
52
 
146
- # Rationals are not duplicable for RUBY_VERSION < 2.5.0:
147
- #
148
- # Rational(1).duplicable? # => false
149
- # Rational(1).dup # => TypeError: can't copy Rational
150
- def duplicable?
151
- false
152
- end
53
+ module Singleton
54
+ # Singleton instances are not duplicable:
55
+ #
56
+ # Class.new.include(Singleton).instance.dup # TypeError (can't dup instance of singleton
57
+ def duplicable?
58
+ false
153
59
  end
154
60
  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,6 +1,9 @@
1
- # Hack to load json gem first so we can overwrite its to_json.
1
+ # frozen_string_literal: true
2
+
3
+ # Hack to load json gem first so we can override its to_json.
2
4
  require "json"
3
5
  require "bigdecimal"
6
+ require "ipaddr"
4
7
  require "uri/generic"
5
8
  require "pathname"
6
9
  require "active_support/core_ext/big_decimal/conversions" # for #to_s
@@ -12,11 +15,11 @@ require "active_support/core_ext/time/conversions"
12
15
  require "active_support/core_ext/date_time/conversions"
13
16
  require "active_support/core_ext/date/conversions"
14
17
 
18
+ #--
15
19
  # The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
16
20
  # their default behavior. That said, we need to define the basic to_json method in all of them,
17
21
  # otherwise they will always use to_json gem implementation, which is backwards incompatible in
18
- # several cases (for instance, the JSON implementation for Hash does not work) with inheritance
19
- # and consequently classes as ActiveSupport::OrderedHash cannot be serialized to json.
22
+ # several cases (for instance, the JSON implementation for Hash does not work) with inheritance.
20
23
  #
21
24
  # On the other hand, we should avoid conflict with ::JSON.{generate,dump}(obj). Unfortunately, the
22
25
  # JSON gem's encoder relies on its own to_json implementation to encode objects. Since it always
@@ -42,12 +45,18 @@ module ActiveSupport
42
45
  end
43
46
  end
44
47
 
45
- [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].reverse_each do |klass|
48
+ [Enumerable, Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].reverse_each do |klass|
46
49
  klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
47
50
  end
48
51
 
52
+ class Module
53
+ def as_json(options = nil) # :nodoc:
54
+ name
55
+ end
56
+ end
57
+
49
58
  class Object
50
- def as_json(options = nil) #:nodoc:
59
+ def as_json(options = nil) # :nodoc:
51
60
  if respond_to?(:to_hash)
52
61
  to_hash.as_json(options)
53
62
  else
@@ -56,44 +65,44 @@ class Object
56
65
  end
57
66
  end
58
67
 
59
- class Struct #:nodoc:
68
+ class Struct # :nodoc:
60
69
  def as_json(options = nil)
61
70
  Hash[members.zip(values)].as_json(options)
62
71
  end
63
72
  end
64
73
 
65
74
  class TrueClass
66
- def as_json(options = nil) #:nodoc:
75
+ def as_json(options = nil) # :nodoc:
67
76
  self
68
77
  end
69
78
  end
70
79
 
71
80
  class FalseClass
72
- def as_json(options = nil) #:nodoc:
81
+ def as_json(options = nil) # :nodoc:
73
82
  self
74
83
  end
75
84
  end
76
85
 
77
86
  class NilClass
78
- def as_json(options = nil) #:nodoc:
87
+ def as_json(options = nil) # :nodoc:
79
88
  self
80
89
  end
81
90
  end
82
91
 
83
92
  class String
84
- def as_json(options = nil) #:nodoc:
93
+ def as_json(options = nil) # :nodoc:
85
94
  self
86
95
  end
87
96
  end
88
97
 
89
98
  class Symbol
90
- def as_json(options = nil) #:nodoc:
99
+ def as_json(options = nil) # :nodoc:
91
100
  to_s
92
101
  end
93
102
  end
94
103
 
95
104
  class Numeric
96
- def as_json(options = nil) #:nodoc:
105
+ def as_json(options = nil) # :nodoc:
97
106
  self
98
107
  end
99
108
  end
@@ -101,7 +110,7 @@ end
101
110
  class Float
102
111
  # Encoding Infinity or NaN to JSON should return "null". The default returns
103
112
  # "Infinity" or "NaN" which are not valid JSON.
104
- def as_json(options = nil) #:nodoc:
113
+ def as_json(options = nil) # :nodoc:
105
114
  finite? ? self : nil
106
115
  end
107
116
  end
@@ -116,37 +125,43 @@ class BigDecimal
116
125
  # if the other end knows by contract that the data is supposed to be a
117
126
  # BigDecimal, it still has the chance to post-process the string and get the
118
127
  # real value.
119
- def as_json(options = nil) #:nodoc:
128
+ def as_json(options = nil) # :nodoc:
120
129
  finite? ? to_s : nil
121
130
  end
122
131
  end
123
132
 
124
133
  class Regexp
125
- def as_json(options = nil) #:nodoc:
134
+ def as_json(options = nil) # :nodoc:
126
135
  to_s
127
136
  end
128
137
  end
129
138
 
130
139
  module Enumerable
131
- def as_json(options = nil) #:nodoc:
140
+ def as_json(options = nil) # :nodoc:
132
141
  to_a.as_json(options)
133
142
  end
134
143
  end
135
144
 
145
+ class IO
146
+ def as_json(options = nil) # :nodoc:
147
+ to_s
148
+ end
149
+ end
150
+
136
151
  class Range
137
- def as_json(options = nil) #:nodoc:
152
+ def as_json(options = nil) # :nodoc:
138
153
  to_s
139
154
  end
140
155
  end
141
156
 
142
157
  class Array
143
- def as_json(options = nil) #:nodoc:
158
+ def as_json(options = nil) # :nodoc:
144
159
  map { |v| options ? v.as_json(options.dup) : v.as_json }
145
160
  end
146
161
  end
147
162
 
148
163
  class Hash
149
- def as_json(options = nil) #:nodoc:
164
+ def as_json(options = nil) # :nodoc:
150
165
  # create a subset of the hash by applying :only or :except
151
166
  subset = if options
152
167
  if attrs = options[:only]
@@ -160,12 +175,16 @@ class Hash
160
175
  self
161
176
  end
162
177
 
163
- Hash[subset.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }]
178
+ result = {}
179
+ subset.each do |k, v|
180
+ result[k.to_s] = options ? v.as_json(options.dup) : v.as_json
181
+ end
182
+ result
164
183
  end
165
184
  end
166
185
 
167
186
  class Time
168
- def as_json(options = nil) #:nodoc:
187
+ def as_json(options = nil) # :nodoc:
169
188
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
170
189
  xmlschema(ActiveSupport::JSON::Encoding.time_precision)
171
190
  else
@@ -175,7 +194,7 @@ class Time
175
194
  end
176
195
 
177
196
  class Date
178
- def as_json(options = nil) #:nodoc:
197
+ def as_json(options = nil) # :nodoc:
179
198
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
180
199
  strftime("%Y-%m-%d")
181
200
  else
@@ -185,7 +204,7 @@ class Date
185
204
  end
186
205
 
187
206
  class DateTime
188
- def as_json(options = nil) #:nodoc:
207
+ def as_json(options = nil) # :nodoc:
189
208
  if ActiveSupport::JSON::Encoding.use_standard_json_time_format
190
209
  xmlschema(ActiveSupport::JSON::Encoding.time_precision)
191
210
  else
@@ -194,19 +213,25 @@ class DateTime
194
213
  end
195
214
  end
196
215
 
197
- class URI::Generic #:nodoc:
216
+ class URI::Generic # :nodoc:
217
+ def as_json(options = nil)
218
+ to_s
219
+ end
220
+ end
221
+
222
+ class Pathname # :nodoc:
198
223
  def as_json(options = nil)
199
224
  to_s
200
225
  end
201
226
  end
202
227
 
203
- class Pathname #:nodoc:
228
+ class IPAddr # :nodoc:
204
229
  def as_json(options = nil)
205
230
  to_s
206
231
  end
207
232
  end
208
233
 
209
- class Process::Status #:nodoc:
234
+ class Process::Status # :nodoc:
210
235
  def as_json(options = nil)
211
236
  { exitstatus: exitstatus, pid: pid }
212
237
  end
@@ -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
@@ -73,11 +75,11 @@ class Hash
73
75
  #
74
76
  # This method is also aliased as +to_param+.
75
77
  def to_query(namespace = nil)
76
- query = collect do |key, value|
78
+ query = filter_map 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
82
+ end
81
83
 
82
84
  query.sort! unless namespace.to_s.include?("[]")
83
85
  query.join("&")
@@ -1,22 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "delegate"
2
4
 
3
5
  module ActiveSupport
4
- module Tryable #:nodoc:
5
- def try(*a, &b)
6
- try!(*a, &b) if a.empty? || respond_to?(a.first)
6
+ module Tryable # :nodoc:
7
+ def try(*args, &block)
8
+ if args.empty? && block_given?
9
+ if block.arity == 0
10
+ instance_eval(&block)
11
+ else
12
+ yield self
13
+ end
14
+ elsif respond_to?(args.first)
15
+ public_send(*args, &block)
16
+ end
7
17
  end
18
+ ruby2_keywords(:try)
8
19
 
9
- def try!(*a, &b)
10
- if a.empty? && block_given?
11
- if b.arity == 0
12
- instance_eval(&b)
20
+ def try!(*args, &block)
21
+ if args.empty? && block_given?
22
+ if block.arity == 0
23
+ instance_eval(&block)
13
24
  else
14
25
  yield self
15
26
  end
16
27
  else
17
- public_send(*a, &b)
28
+ public_send(*args, &block)
18
29
  end
19
30
  end
31
+ ruby2_keywords(:try!)
20
32
  end
21
33
  end
22
34
 
@@ -27,7 +39,7 @@ class Object
27
39
  # :method: try
28
40
  #
29
41
  # :call-seq:
30
- # try(*a, &b)
42
+ # try(*args, &block)
31
43
  #
32
44
  # Invokes the public method whose name goes as first argument just like
33
45
  # +public_send+ does, except that if the receiver does not respond to it the
@@ -92,7 +104,7 @@ class Object
92
104
  # :method: try!
93
105
  #
94
106
  # :call-seq:
95
- # try!(*a, &b)
107
+ # try!(*args, &block)
96
108
  #
97
109
  # Same as #try, but raises a +NoMethodError+ exception if the receiver is
98
110
  # not +nil+ and does not implement the tried method.
@@ -109,7 +121,7 @@ class Delegator
109
121
  # :method: try
110
122
  #
111
123
  # :call-seq:
112
- # try(a*, &b)
124
+ # try(*args, &block)
113
125
  #
114
126
  # See Object#try
115
127
 
@@ -117,7 +129,7 @@ class Delegator
117
129
  # :method: try!
118
130
  #
119
131
  # :call-seq:
120
- # try!(a*, &b)
132
+ # try!(*args, &block)
121
133
  #
122
134
  # See Object#try!
123
135
  end
@@ -133,14 +145,14 @@ class NilClass
133
145
  #
134
146
  # With +try+
135
147
  # @person.try(:children).try(:first).try(:name)
136
- def try(*args)
148
+ def try(*)
137
149
  nil
138
150
  end
139
151
 
140
152
  # Calling +try!+ on +nil+ always returns +nil+.
141
153
  #
142
154
  # nil.try!(:name) # => nil
143
- def try!(*args)
155
+ def try!(*)
144
156
  nil
145
157
  end
146
158
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/option_merger"
2
4
 
3
5
  class Object
@@ -60,21 +62,40 @@ 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:
67
69
  #
68
70
  # class Phone < ActiveRecord::Base
69
- # enum phone_number_type: [home: 0, office: 1, mobile: 2]
71
+ # enum phone_number_type: { home: 0, office: 1, mobile: 2 }
70
72
  #
71
73
  # with_options presence: true do
72
74
  # validates :phone_number_type, inclusion: { in: Phone.phone_number_types.keys }
73
75
  # end
74
76
  # end
75
77
  #
78
+ # When the block argument is omitted, the decorated Object instance is returned:
79
+ #
80
+ # module MyStyledHelpers
81
+ # def styled
82
+ # with_options style: "color: red;"
83
+ # end
84
+ # end
85
+ #
86
+ # # styled.link_to "I'm red", "/"
87
+ # # #=> <a href="/" style="color: red;">I'm red</a>
88
+ #
89
+ # # styled.button_tag "I'm red too!"
90
+ # # #=> <button style="color: red;">I'm red too!</button>
91
+ #
76
92
  def with_options(options, &block)
77
93
  option_merger = ActiveSupport::OptionMerger.new(self, options)
78
- block.arity.zero? ? option_merger.instance_eval(&block) : block.call(option_merger)
94
+
95
+ if block
96
+ block.arity.zero? ? option_merger.instance_eval(&block) : block.call(option_merger)
97
+ else
98
+ option_merger
99
+ end
79
100
  end
80
101
  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"
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pathname
4
+ # Returns the receiver if the named file exists otherwise returns +nil+.
5
+ # <tt>pathname.existence</tt> is equivalent to
6
+ #
7
+ # pathname.exist? ? pathname : nil
8
+ #
9
+ # For example, something like
10
+ #
11
+ # content = pathname.read if pathname.exist?
12
+ #
13
+ # becomes
14
+ #
15
+ # content = pathname.existence&.read
16
+ #
17
+ # @return [Pathname]
18
+ def existence
19
+ self if exist?
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/pathname/existence"
@@ -0,0 +1,57 @@
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
+ is_backwards_op = value.exclude_end? ? :>= : :>
19
+ return false if value.begin && value.end && value.begin.public_send(is_backwards_op, value.end)
20
+ # 1...10 includes 1..9 but it does not include 1..10.
21
+ # 1..10 includes 1...11 but it does not include 1...12.
22
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
23
+ value_max = !exclude_end? && value.exclude_end? ? value.max : value.last
24
+ super(value.first) && (self.end.nil? || value_max.public_send(operator, last))
25
+ else
26
+ super
27
+ end
28
+ end
29
+
30
+ # Extends the default Range#include? to support range comparisons.
31
+ # (1..5).include?(1..5) # => true
32
+ # (1..5).include?(2..3) # => true
33
+ # (1..5).include?(1...6) # => true
34
+ # (1..5).include?(2..6) # => false
35
+ #
36
+ # The native Range#include? behavior is untouched.
37
+ # ('a'..'f').include?('c') # => true
38
+ # (5..9).include?(11) # => false
39
+ #
40
+ # The given range must be fully bounded, with both start and end.
41
+ def include?(value)
42
+ if value.is_a?(::Range)
43
+ is_backwards_op = value.exclude_end? ? :>= : :>
44
+ return false if value.begin && value.end && value.begin.public_send(is_backwards_op, value.end)
45
+ # 1...10 includes 1..9 but it does not include 1..10.
46
+ # 1..10 includes 1...11 but it does not include 1...12.
47
+ operator = exclude_end? && !value.exclude_end? ? :< : :<=
48
+ value_max = !exclude_end? && value.exclude_end? ? value.max : value.last
49
+ super(value.first) && (self.end.nil? || value_max.public_send(operator, last))
50
+ else
51
+ super
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ Range.prepend(ActiveSupport::CompareWithRange)