activesupport 4.0.12 → 7.0.2.4

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 (295) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +249 -501
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +10 -5
  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 +41 -13
  9. data/lib/active_support/benchmarkable.rb +7 -15
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache/file_store.rb +96 -74
  12. data/lib/active_support/cache/mem_cache_store.rb +211 -103
  13. data/lib/active_support/cache/memory_store.rb +90 -58
  14. data/lib/active_support/cache/null_store.rb +19 -7
  15. data/lib/active_support/cache/redis_cache_store.rb +468 -0
  16. data/lib/active_support/cache/strategy/local_cache.rb +86 -83
  17. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  18. data/lib/active_support/cache.rb +580 -241
  19. data/lib/active_support/callbacks.rb +812 -425
  20. data/lib/active_support/code_generator.rb +65 -0
  21. data/lib/active_support/concern.rb +103 -14
  22. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +33 -0
  23. data/lib/active_support/concurrency/share_lock.rb +226 -0
  24. data/lib/active_support/configurable.rb +21 -19
  25. data/lib/active_support/configuration_file.rb +51 -0
  26. data/lib/active_support/core_ext/array/access.rb +47 -1
  27. data/lib/active_support/core_ext/array/conversions.rb +35 -44
  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 +26 -16
  32. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  33. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  34. data/lib/active_support/core_ext/array.rb +10 -7
  35. data/lib/active_support/core_ext/benchmark.rb +5 -3
  36. data/lib/active_support/core_ext/big_decimal/conversions.rb +9 -26
  37. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  38. data/lib/active_support/core_ext/class/attribute.rb +52 -49
  39. data/lib/active_support/core_ext/class/attribute_accessors.rb +5 -169
  40. data/lib/active_support/core_ext/class/subclasses.rb +25 -26
  41. data/lib/active_support/core_ext/class.rb +4 -4
  42. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  43. data/lib/active_support/core_ext/date/blank.rb +14 -0
  44. data/lib/active_support/core_ext/date/calculations.rb +31 -18
  45. data/lib/active_support/core_ext/date/conversions.rb +43 -32
  46. data/lib/active_support/core_ext/date/deprecated_conversions.rb +26 -0
  47. data/lib/active_support/core_ext/date/zones.rb +5 -34
  48. data/lib/active_support/core_ext/date.rb +7 -4
  49. data/lib/active_support/core_ext/date_and_time/calculations.rb +198 -66
  50. data/lib/active_support/core_ext/date_and_time/compatibility.rb +31 -0
  51. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  52. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  53. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  54. data/lib/active_support/core_ext/date_time/calculations.rb +79 -38
  55. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  56. data/lib/active_support/core_ext/date_time/conversions.rb +31 -26
  57. data/lib/active_support/core_ext/date_time/deprecated_conversions.rb +22 -0
  58. data/lib/active_support/core_ext/date_time.rb +8 -4
  59. data/lib/active_support/core_ext/digest/uuid.rb +79 -0
  60. data/lib/active_support/core_ext/digest.rb +3 -0
  61. data/lib/active_support/core_ext/enumerable.rb +249 -17
  62. data/lib/active_support/core_ext/file/atomic.rb +41 -32
  63. data/lib/active_support/core_ext/file.rb +3 -1
  64. data/lib/active_support/core_ext/hash/conversions.rb +71 -49
  65. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  66. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  67. data/lib/active_support/core_ext/hash/except.rb +14 -5
  68. data/lib/active_support/core_ext/hash/indifferent_access.rb +5 -3
  69. data/lib/active_support/core_ext/hash/keys.rb +39 -56
  70. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  71. data/lib/active_support/core_ext/hash/slice.rb +8 -23
  72. data/lib/active_support/core_ext/hash.rb +10 -8
  73. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  74. data/lib/active_support/core_ext/integer/multiple.rb +3 -1
  75. data/lib/active_support/core_ext/integer/time.rb +11 -33
  76. data/lib/active_support/core_ext/integer.rb +5 -3
  77. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  78. data/lib/active_support/core_ext/kernel/reporting.rb +9 -78
  79. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  80. data/lib/active_support/core_ext/kernel.rb +5 -4
  81. data/lib/active_support/core_ext/load_error.rb +5 -21
  82. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  83. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  84. data/lib/active_support/core_ext/module/attr_internal.rb +8 -8
  85. data/lib/active_support/core_ext/module/attribute_accessors.rb +186 -44
  86. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +157 -0
  87. data/lib/active_support/core_ext/module/concerning.rb +140 -0
  88. data/lib/active_support/core_ext/module/delegation.rb +172 -45
  89. data/lib/active_support/core_ext/module/deprecation.rb +3 -3
  90. data/lib/active_support/core_ext/module/introspection.rb +23 -38
  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 -10
  94. data/lib/active_support/core_ext/name_error.rb +45 -4
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +135 -127
  97. data/lib/active_support/core_ext/numeric/deprecated_conversions.rb +60 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +37 -50
  99. data/lib/active_support/core_ext/numeric.rb +6 -3
  100. data/lib/active_support/core_ext/object/acts_like.rb +41 -6
  101. data/lib/active_support/core_ext/object/blank.rb +70 -20
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +19 -10
  104. data/lib/active_support/core_ext/object/duplicable.rb +17 -47
  105. data/lib/active_support/core_ext/object/inclusion.rb +18 -15
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +244 -0
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +21 -8
  110. data/lib/active_support/core_ext/object/try.rb +106 -26
  111. data/lib/active_support/core_ext/object/with_options.rb +64 -5
  112. data/lib/active_support/core_ext/object.rb +14 -12
  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 +37 -15
  117. data/lib/active_support/core_ext/range/deprecated_conversions.rb +26 -0
  118. data/lib/active_support/core_ext/range/each.rb +18 -17
  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 +2 -0
  121. data/lib/active_support/core_ext/range.rb +7 -4
  122. data/lib/active_support/core_ext/regexp.rb +10 -1
  123. data/lib/active_support/core_ext/securerandom.rb +45 -0
  124. data/lib/active_support/core_ext/string/access.rb +42 -51
  125. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  126. data/lib/active_support/core_ext/string/conversions.rb +18 -13
  127. data/lib/active_support/core_ext/string/exclude.rb +5 -3
  128. data/lib/active_support/core_ext/string/filters.rb +97 -7
  129. data/lib/active_support/core_ext/string/indent.rb +6 -4
  130. data/lib/active_support/core_ext/string/inflections.rb +106 -25
  131. data/lib/active_support/core_ext/string/inquiry.rb +4 -1
  132. data/lib/active_support/core_ext/string/multibyte.rb +18 -9
  133. data/lib/active_support/core_ext/string/output_safety.rb +227 -54
  134. data/lib/active_support/core_ext/string/starts_ends_with.rb +4 -2
  135. data/lib/active_support/core_ext/string/strip.rb +6 -5
  136. data/lib/active_support/core_ext/string/zones.rb +4 -1
  137. data/lib/active_support/core_ext/string.rb +15 -13
  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 +3 -1
  141. data/lib/active_support/core_ext/time/calculations.rb +178 -116
  142. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  143. data/lib/active_support/core_ext/time/conversions.rb +37 -25
  144. data/lib/active_support/core_ext/time/deprecated_conversions.rb +22 -0
  145. data/lib/active_support/core_ext/time/zones.rb +44 -42
  146. data/lib/active_support/core_ext/time.rb +8 -5
  147. data/lib/active_support/core_ext/uri.rb +4 -25
  148. data/lib/active_support/core_ext.rb +4 -2
  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 +3 -1
  152. data/lib/active_support/dependencies/interlock.rb +49 -0
  153. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  154. data/lib/active_support/dependencies.rb +71 -696
  155. data/lib/active_support/deprecation/behaviors.rb +65 -16
  156. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  157. data/lib/active_support/deprecation/disallowed.rb +56 -0
  158. data/lib/active_support/deprecation/instance_delegator.rb +16 -2
  159. data/lib/active_support/deprecation/method_wrappers.rb +62 -21
  160. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -31
  161. data/lib/active_support/deprecation/reporting.rb +81 -18
  162. data/lib/active_support/deprecation.rb +19 -11
  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 +123 -0
  166. data/lib/active_support/duration/iso8601_serializer.rb +67 -0
  167. data/lib/active_support/duration.rb +437 -39
  168. data/lib/active_support/encrypted_configuration.rb +56 -0
  169. data/lib/active_support/encrypted_file.rb +117 -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 +170 -0
  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 +151 -0
  176. data/lib/active_support/executor/test_helper.rb +7 -0
  177. data/lib/active_support/executor.rb +8 -0
  178. data/lib/active_support/file_update_checker.rb +62 -37
  179. data/lib/active_support/fork_tracker.rb +71 -0
  180. data/lib/active_support/gem_version.rb +17 -0
  181. data/lib/active_support/gzip.rb +7 -5
  182. data/lib/active_support/hash_with_indifferent_access.rb +207 -54
  183. data/lib/active_support/html_safe_translation.rb +43 -0
  184. data/lib/active_support/i18n.rb +10 -6
  185. data/lib/active_support/i18n_railtie.rb +48 -19
  186. data/lib/active_support/inflections.rb +19 -12
  187. data/lib/active_support/inflector/inflections.rb +97 -37
  188. data/lib/active_support/inflector/methods.rb +192 -157
  189. data/lib/active_support/inflector/transliterate.rb +83 -33
  190. data/lib/active_support/inflector.rb +7 -5
  191. data/lib/active_support/isolated_execution_state.rb +64 -0
  192. data/lib/active_support/json/decoding.rb +37 -42
  193. data/lib/active_support/json/encoding.rb +93 -293
  194. data/lib/active_support/json.rb +4 -2
  195. data/lib/active_support/key_generator.rb +30 -47
  196. data/lib/active_support/lazy_load_hooks.rb +54 -21
  197. data/lib/active_support/locale/en.rb +33 -0
  198. data/lib/active_support/locale/en.yml +10 -4
  199. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  200. data/lib/active_support/log_subscriber.rb +61 -18
  201. data/lib/active_support/logger.rb +40 -4
  202. data/lib/active_support/logger_silence.rb +17 -20
  203. data/lib/active_support/logger_thread_safe_level.rb +69 -0
  204. data/lib/active_support/message_encryptor.rb +178 -55
  205. data/lib/active_support/message_verifier.rb +195 -26
  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 +45 -92
  210. data/lib/active_support/multibyte/unicode.rb +44 -377
  211. data/lib/active_support/multibyte.rb +5 -3
  212. data/lib/active_support/notifications/fanout.rb +177 -44
  213. data/lib/active_support/notifications/instrumenter.rb +117 -17
  214. data/lib/active_support/notifications.rb +106 -39
  215. data/lib/active_support/number_helper/number_converter.rb +181 -0
  216. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  217. data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
  218. data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
  219. data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
  220. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  221. data/lib/active_support/number_helper/number_to_phone_converter.rb +59 -0
  222. data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
  223. data/lib/active_support/number_helper/rounding_helper.rb +46 -0
  224. data/lib/active_support/number_helper.rb +152 -394
  225. data/lib/active_support/option_merger.rb +18 -5
  226. data/lib/active_support/ordered_hash.rb +8 -6
  227. data/lib/active_support/ordered_options.rb +43 -7
  228. data/lib/active_support/parameter_filter.rb +138 -0
  229. data/lib/active_support/per_thread_registry.rb +24 -11
  230. data/lib/active_support/proxy_object.rb +2 -0
  231. data/lib/active_support/rails.rb +10 -11
  232. data/lib/active_support/railtie.rb +118 -12
  233. data/lib/active_support/reloader.rb +130 -0
  234. data/lib/active_support/rescuable.rb +112 -57
  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 +38 -0
  238. data/lib/active_support/string_inquirer.rb +11 -4
  239. data/lib/active_support/subscriber.rb +109 -39
  240. data/lib/active_support/tagged_logging.rb +54 -17
  241. data/lib/active_support/test_case.rb +121 -37
  242. data/lib/active_support/testing/assertions.rb +177 -39
  243. data/lib/active_support/testing/autorun.rb +5 -3
  244. data/lib/active_support/testing/constant_lookup.rb +3 -6
  245. data/lib/active_support/testing/declarative.rb +10 -22
  246. data/lib/active_support/testing/deprecation.rb +65 -11
  247. data/lib/active_support/testing/file_fixtures.rb +38 -0
  248. data/lib/active_support/testing/isolation.rb +56 -87
  249. data/lib/active_support/testing/method_call_assertions.rb +70 -0
  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 +30 -10
  255. data/lib/active_support/testing/stream.rb +41 -0
  256. data/lib/active_support/testing/tagged_logging.rb +6 -4
  257. data/lib/active_support/testing/time_helpers.rb +246 -0
  258. data/lib/active_support/time.rb +13 -13
  259. data/lib/active_support/time_with_zone.rb +315 -90
  260. data/lib/active_support/values/time_zone.rb +306 -135
  261. data/lib/active_support/version.rb +6 -7
  262. data/lib/active_support/xml_mini/jdom.rb +117 -115
  263. data/lib/active_support/xml_mini/libxml.rb +22 -21
  264. data/lib/active_support/xml_mini/libxmlsax.rb +17 -19
  265. data/lib/active_support/xml_mini/nokogiri.rb +19 -19
  266. data/lib/active_support/xml_mini/nokogirisax.rb +16 -17
  267. data/lib/active_support/xml_mini/rexml.rb +25 -17
  268. data/lib/active_support/xml_mini.rb +67 -56
  269. data/lib/active_support.rb +58 -3
  270. metadata +125 -66
  271. data/lib/active_support/basic_object.rb +0 -11
  272. data/lib/active_support/buffered_logger.rb +0 -21
  273. data/lib/active_support/concurrency/latch.rb +0 -27
  274. data/lib/active_support/core_ext/array/prepend_and_append.rb +0 -7
  275. data/lib/active_support/core_ext/array/uniq_by.rb +0 -19
  276. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -40
  277. data/lib/active_support/core_ext/date_time/zones.rb +0 -24
  278. data/lib/active_support/core_ext/hash/diff.rb +0 -14
  279. data/lib/active_support/core_ext/kernel/agnostics.rb +0 -11
  280. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  281. data/lib/active_support/core_ext/logger.rb +0 -67
  282. data/lib/active_support/core_ext/marshal.rb +0 -21
  283. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  284. data/lib/active_support/core_ext/module/reachable.rb +0 -8
  285. data/lib/active_support/core_ext/object/to_json.rb +0 -27
  286. data/lib/active_support/core_ext/proc.rb +0 -17
  287. data/lib/active_support/core_ext/range/include_range.rb +0 -23
  288. data/lib/active_support/core_ext/string/encoding.rb +0 -8
  289. data/lib/active_support/core_ext/struct.rb +0 -6
  290. data/lib/active_support/core_ext/thread.rb +0 -79
  291. data/lib/active_support/core_ext/time/marshal.rb +0 -30
  292. data/lib/active_support/file_watcher.rb +0 -36
  293. data/lib/active_support/json/variable.rb +0 -18
  294. data/lib/active_support/testing/pending.rb +0 -14
  295. data/lib/active_support/values/unicode_tables.dat +0 -0
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Hash
2
4
  # Returns a new hash with +self+ and +other_hash+ merged recursively.
3
5
  #
4
6
  # h1 = { a: true, b: { c: [1, 2, 3] } }
5
7
  # h2 = { a: false, b: { x: [3, 4, 5] } }
6
8
  #
7
- # h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
9
+ # h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
8
10
  #
9
11
  # Like with Hash#merge in the standard library, a block can be provided
10
12
  # to merge values:
@@ -19,20 +21,14 @@ class Hash
19
21
 
20
22
  # Same as +deep_merge+, but modifies +self+.
21
23
  def deep_merge!(other_hash, &block)
22
- other_hash.each_pair do |current_key, other_value|
23
- this_value = self[current_key]
24
-
25
- self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
26
- this_value.deep_merge(other_value, &block)
24
+ merge!(other_hash) do |key, this_val, other_val|
25
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
26
+ this_val.deep_merge(other_val, &block)
27
+ elsif block_given?
28
+ block.call(key, this_val, other_val)
27
29
  else
28
- if block_given? && key?(current_key)
29
- block.call(current_key, this_value, other_value)
30
- else
31
- other_value
32
- end
30
+ other_val
33
31
  end
34
32
  end
35
-
36
- self
37
33
  end
38
34
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ # Returns a new hash with all values converted by the block operation.
5
+ # This includes the values from the root hash and from all
6
+ # nested hashes and arrays.
7
+ #
8
+ # hash = { person: { name: 'Rob', age: '28' } }
9
+ #
10
+ # hash.deep_transform_values{ |value| value.to_s.upcase }
11
+ # # => {person: {name: "ROB", age: "28"}}
12
+ def deep_transform_values(&block)
13
+ _deep_transform_values_in_object(self, &block)
14
+ end
15
+
16
+ # Destructively converts all values by using the block operation.
17
+ # This includes the values from the root hash and from all
18
+ # nested hashes and arrays.
19
+ def deep_transform_values!(&block)
20
+ _deep_transform_values_in_object!(self, &block)
21
+ end
22
+
23
+ private
24
+ # Support methods for deep transforming nested hashes and arrays.
25
+ def _deep_transform_values_in_object(object, &block)
26
+ case object
27
+ when Hash
28
+ object.transform_values { |value| _deep_transform_values_in_object(value, &block) }
29
+ when Array
30
+ object.map { |e| _deep_transform_values_in_object(e, &block) }
31
+ else
32
+ yield(object)
33
+ end
34
+ end
35
+
36
+ def _deep_transform_values_in_object!(object, &block)
37
+ case object
38
+ when Hash
39
+ object.transform_values! { |value| _deep_transform_values_in_object!(value, &block) }
40
+ when Array
41
+ object.map! { |e| _deep_transform_values_in_object!(e, &block) }
42
+ else
43
+ yield(object)
44
+ end
45
+ end
46
+ end
@@ -1,13 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Hash
2
- # Return a hash that includes everything but the given keys. This is useful for
3
- # limiting a set of parameters to everything but a few known toggles:
4
+ # Returns a hash that includes everything except given keys.
5
+ # hash = { a: true, b: false, c: nil }
6
+ # hash.except(:c) # => { a: true, b: false }
7
+ # hash.except(:a, :b) # => { c: nil }
8
+ # hash # => { a: true, b: false, c: nil }
4
9
  #
10
+ # This is useful for limiting a set of parameters to everything but a few known toggles:
5
11
  # @person.update(params[:person].except(:admin))
6
12
  def except(*keys)
7
- dup.except!(*keys)
8
- end
13
+ slice(*self.keys - keys)
14
+ end unless method_defined?(:except)
9
15
 
10
- # Replaces the hash without the given keys.
16
+ # Removes the given keys from hash and returns it.
17
+ # hash = { a: true, b: false, c: nil }
18
+ # hash.except!(:c) # => { a: true, b: false }
19
+ # hash # => { a: true, b: false }
11
20
  def except!(*keys)
12
21
  keys.each { |key| delete(key) }
13
22
  self
@@ -1,12 +1,13 @@
1
- require 'active_support/hash_with_indifferent_access'
1
+ # frozen_string_literal: true
2
2
 
3
- class Hash
3
+ require "active_support/hash_with_indifferent_access"
4
4
 
5
+ class Hash
5
6
  # Returns an <tt>ActiveSupport::HashWithIndifferentAccess</tt> out of its receiver:
6
7
  #
7
8
  # { a: 1 }.with_indifferent_access['a'] # => 1
8
9
  def with_indifferent_access
9
- ActiveSupport::HashWithIndifferentAccess.new_from_hash_copying_default(self)
10
+ ActiveSupport::HashWithIndifferentAccess.new(self)
10
11
  end
11
12
 
12
13
  # Called when object is nested under an object that receives
@@ -18,5 +19,6 @@ class Hash
18
19
  #
19
20
  # b = { b: 1 }
20
21
  # { a: b }.with_indifferent_access['a'] # calls b.nested_under_indifferent_access
22
+ # # => {"b"=>1}
21
23
  alias nested_under_indifferent_access with_indifferent_access
22
24
  end
@@ -1,143 +1,126 @@
1
- class Hash
2
- # Return a new hash with all keys converted using the block operation.
3
- #
4
- # hash = { name: 'Rob', age: '28' }
5
- #
6
- # hash.transform_keys{ |key| key.to_s.upcase }
7
- # # => { "NAME" => "Rob", "AGE" => "28" }
8
- def transform_keys
9
- result = {}
10
- each_key do |key|
11
- result[yield(key)] = self[key]
12
- end
13
- result
14
- end
1
+ # frozen_string_literal: true
15
2
 
16
- # Destructively convert all keys using the block operations.
17
- # Same as transform_keys but modifies +self+.
18
- def transform_keys!
19
- keys.each do |key|
20
- self[yield(key)] = delete(key)
21
- end
22
- self
23
- end
24
-
25
- # Return a new hash with all keys converted to strings.
3
+ class Hash
4
+ # Returns a new hash with all keys converted to strings.
26
5
  #
27
6
  # hash = { name: 'Rob', age: '28' }
28
7
  #
29
8
  # hash.stringify_keys
30
- # #=> { "name" => "Rob", "age" => "28" }
9
+ # # => {"name"=>"Rob", "age"=>"28"}
31
10
  def stringify_keys
32
- transform_keys{ |key| key.to_s }
11
+ transform_keys(&:to_s)
33
12
  end
34
13
 
35
- # Destructively convert all keys to strings. Same as
14
+ # Destructively converts all keys to strings. Same as
36
15
  # +stringify_keys+, but modifies +self+.
37
16
  def stringify_keys!
38
- transform_keys!{ |key| key.to_s }
17
+ transform_keys!(&:to_s)
39
18
  end
40
19
 
41
- # Return a new hash with all keys converted to symbols, as long as
20
+ # Returns a new hash with all keys converted to symbols, as long as
42
21
  # they respond to +to_sym+.
43
22
  #
44
23
  # hash = { 'name' => 'Rob', 'age' => '28' }
45
24
  #
46
25
  # hash.symbolize_keys
47
- # #=> { name: "Rob", age: "28" }
26
+ # # => {:name=>"Rob", :age=>"28"}
48
27
  def symbolize_keys
49
- transform_keys{ |key| key.to_sym rescue key }
28
+ transform_keys { |key| key.to_sym rescue key }
50
29
  end
51
30
  alias_method :to_options, :symbolize_keys
52
31
 
53
- # Destructively convert all keys to symbols, as long as they respond
32
+ # Destructively converts all keys to symbols, as long as they respond
54
33
  # to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
55
34
  def symbolize_keys!
56
- transform_keys!{ |key| key.to_sym rescue key }
35
+ transform_keys! { |key| key.to_sym rescue key }
57
36
  end
58
37
  alias_method :to_options!, :symbolize_keys!
59
38
 
60
- # Validate all keys in a hash match <tt>*valid_keys</tt>, raising ArgumentError
61
- # on a mismatch. Note that keys are NOT treated indifferently, meaning if you
62
- # use strings for keys but assert symbols as keys, this will fail.
39
+ # Validates all keys in a hash match <tt>*valid_keys</tt>, raising
40
+ # +ArgumentError+ on a mismatch.
63
41
  #
64
- # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: years"
65
- # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: name"
42
+ # Note that keys are treated differently than HashWithIndifferentAccess,
43
+ # meaning that string and symbol keys will not match.
44
+ #
45
+ # { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
46
+ # { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
66
47
  # { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
67
48
  def assert_valid_keys(*valid_keys)
68
49
  valid_keys.flatten!
69
50
  each_key do |k|
70
- raise ArgumentError.new("Unknown key: #{k}") unless valid_keys.include?(k)
51
+ unless valid_keys.include?(k)
52
+ raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
53
+ end
71
54
  end
72
55
  end
73
56
 
74
- # Return a new hash with all keys converted by the block operation.
57
+ # Returns a new hash with all keys converted by the block operation.
75
58
  # This includes the keys from the root hash and from all
76
59
  # nested hashes and arrays.
77
60
  #
78
61
  # hash = { person: { name: 'Rob', age: '28' } }
79
62
  #
80
63
  # hash.deep_transform_keys{ |key| key.to_s.upcase }
81
- # # => { "PERSON" => { "NAME" => "Rob", "AGE" => "28" } }
64
+ # # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
82
65
  def deep_transform_keys(&block)
83
66
  _deep_transform_keys_in_object(self, &block)
84
67
  end
85
68
 
86
- # Destructively convert all keys by using the block operation.
69
+ # Destructively converts all keys by using the block operation.
87
70
  # This includes the keys from the root hash and from all
88
71
  # nested hashes and arrays.
89
72
  def deep_transform_keys!(&block)
90
73
  _deep_transform_keys_in_object!(self, &block)
91
74
  end
92
75
 
93
- # Return a new hash with all keys converted to strings.
76
+ # Returns a new hash with all keys converted to strings.
94
77
  # This includes the keys from the root hash and from all
95
78
  # nested hashes and arrays.
96
79
  #
97
80
  # hash = { person: { name: 'Rob', age: '28' } }
98
81
  #
99
82
  # hash.deep_stringify_keys
100
- # # => { "person" => { "name" => "Rob", "age" => "28" } }
83
+ # # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
101
84
  def deep_stringify_keys
102
- deep_transform_keys{ |key| key.to_s }
85
+ deep_transform_keys(&:to_s)
103
86
  end
104
87
 
105
- # Destructively convert all keys to strings.
88
+ # Destructively converts all keys to strings.
106
89
  # This includes the keys from the root hash and from all
107
90
  # nested hashes and arrays.
108
91
  def deep_stringify_keys!
109
- deep_transform_keys!{ |key| key.to_s }
92
+ deep_transform_keys!(&:to_s)
110
93
  end
111
94
 
112
- # Return a new hash with all keys converted to symbols, as long as
95
+ # Returns a new hash with all keys converted to symbols, as long as
113
96
  # they respond to +to_sym+. This includes the keys from the root hash
114
97
  # and from all nested hashes and arrays.
115
98
  #
116
99
  # hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } }
117
100
  #
118
101
  # hash.deep_symbolize_keys
119
- # # => { person: { name: "Rob", age: "28" } }
102
+ # # => {:person=>{:name=>"Rob", :age=>"28"}}
120
103
  def deep_symbolize_keys
121
- deep_transform_keys{ |key| key.to_sym rescue key }
104
+ deep_transform_keys { |key| key.to_sym rescue key }
122
105
  end
123
106
 
124
- # Destructively convert all keys to symbols, as long as they respond
107
+ # Destructively converts all keys to symbols, as long as they respond
125
108
  # to +to_sym+. This includes the keys from the root hash and from all
126
109
  # nested hashes and arrays.
127
110
  def deep_symbolize_keys!
128
- deep_transform_keys!{ |key| key.to_sym rescue key }
111
+ deep_transform_keys! { |key| key.to_sym rescue key }
129
112
  end
130
113
 
131
114
  private
132
- # support methods for deep transforming nested hashes and arrays
115
+ # Support methods for deep transforming nested hashes and arrays.
133
116
  def _deep_transform_keys_in_object(object, &block)
134
117
  case object
135
118
  when Hash
136
- object.each_with_object({}) do |(key, value), result|
119
+ object.each_with_object(self.class.new) do |(key, value), result|
137
120
  result[yield(key)] = _deep_transform_keys_in_object(value, &block)
138
121
  end
139
122
  when Array
140
- object.map {|e| _deep_transform_keys_in_object(e, &block) }
123
+ object.map { |e| _deep_transform_keys_in_object(e, &block) }
141
124
  else
142
125
  object
143
126
  end
@@ -152,7 +135,7 @@ class Hash
152
135
  end
153
136
  object
154
137
  when Array
155
- object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
138
+ object.map! { |e| _deep_transform_keys_in_object!(e, &block) }
156
139
  else
157
140
  object
158
141
  end
@@ -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,29 +1,13 @@
1
- class Hash
2
- # Slice a hash to include only the given keys. This is useful for
3
- # limiting an options hash to valid keys before passing to a method:
4
- #
5
- # def search(criteria = {})
6
- # criteria.assert_valid_keys(:mass, :velocity, :time)
7
- # end
8
- #
9
- # search(options.slice(:mass, :velocity, :time))
10
- #
11
- # If you have an array of keys you want to limit to, you should splat them:
12
- #
13
- # valid_keys = [:mass, :velocity, :time]
14
- # search(options.slice(*valid_keys))
15
- def slice(*keys)
16
- keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
17
- keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
18
- end
1
+ # frozen_string_literal: true
19
2
 
3
+ class Hash
20
4
  # Replaces the hash with only the given keys.
21
5
  # Returns a hash containing the removed key/value pairs.
22
6
  #
23
- # { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
24
- # # => {: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}
25
10
  def slice!(*keys)
26
- keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
27
11
  omit = slice(*self.keys - keys)
28
12
  hash = slice(*keys)
29
13
  hash.default = default
@@ -34,8 +18,9 @@ class Hash
34
18
 
35
19
  # Removes and returns the key/value pairs matching the given keys.
36
20
  #
37
- # { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
38
- # { 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}
39
24
  def extract!(*keys)
40
25
  keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
41
26
  end
@@ -1,8 +1,10 @@
1
- require 'active_support/core_ext/hash/conversions'
2
- require 'active_support/core_ext/hash/deep_merge'
3
- require 'active_support/core_ext/hash/diff'
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'
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,44 +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
14
- #
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
7
+ # Returns a Duration instance matching the number of months provided.
20
8
  #
21
- # While these methods provide precise calculation when used as in the examples
22
- # above, care should be taken to note that this is not true if the result of
23
- # +months+, +years+, etc is converted before use:
24
- #
25
- # # equivalent to 30.days.to_i.from_now
26
- # 1.month.to_i.from_now
27
- #
28
- # # equivalent to 365.25.days.to_f.from_now
29
- # 1.year.to_f.from_now
30
- #
31
- # In such cases, Ruby's core
32
- # Date[http://ruby-doc.org/stdlib/libdoc/date/rdoc/Date.html] and
33
- # Time[http://ruby-doc.org/stdlib/libdoc/time/rdoc/Time.html] should be used for precision
34
- # date and time arithmetic.
9
+ # 2.months # => 2 months
35
10
  def months
36
- ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
11
+ ActiveSupport::Duration.months(self)
37
12
  end
38
13
  alias :month :months
39
14
 
15
+ # Returns a Duration instance matching the number of years provided.
16
+ #
17
+ # 2.years # => 2 years
40
18
  def years
41
- ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]])
19
+ ActiveSupport::Duration.years(self)
42
20
  end
43
21
  alias :year :years
44
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"
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/module/concerning"
4
+
5
+ module Kernel
6
+ module_function
7
+
8
+ # A shortcut to define a toplevel concern, not within a module.
9
+ #
10
+ # See Module::Concerning for more.
11
+ def concern(topic, &module_definition)
12
+ Object.concern topic, &module_definition
13
+ end
14
+ end
@@ -1,8 +1,9 @@
1
- require 'rbconfig'
2
- require 'tempfile'
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Kernel
5
- # Sets $VERBOSE to nil for the duration of the block and back to its original
4
+ module_function
5
+
6
+ # Sets $VERBOSE to +nil+ for the duration of the block and back to its original
6
7
  # value afterwards.
7
8
  #
8
9
  # silence_warnings do
@@ -10,14 +11,14 @@ module Kernel
10
11
  # end
11
12
  #
12
13
  # noisy_call # warning voiced
13
- def silence_warnings
14
- with_warnings(nil) { yield }
14
+ def silence_warnings(&block)
15
+ with_warnings(nil, &block)
15
16
  end
16
17
 
17
18
  # Sets $VERBOSE to +true+ for the duration of the block and back to its
18
19
  # original value afterwards.
19
- def enable_warnings
20
- with_warnings(true) { yield }
20
+ def enable_warnings(&block)
21
+ with_warnings(true, &block)
21
22
  end
22
23
 
23
24
  # Sets $VERBOSE for the duration of the block and back to its original
@@ -29,30 +30,6 @@ module Kernel
29
30
  $VERBOSE = old_verbose
30
31
  end
31
32
 
32
- # For compatibility
33
- def silence_stderr #:nodoc:
34
- silence_stream(STDERR) { yield }
35
- end
36
-
37
- # Silences any stream for the duration of the block.
38
- #
39
- # silence_stream(STDOUT) do
40
- # puts 'This will never be seen'
41
- # end
42
- #
43
- # puts 'But this will'
44
- #
45
- # This method is not thread-safe.
46
- def silence_stream(stream)
47
- old_stream = stream.dup
48
- stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
49
- stream.sync = true
50
- yield
51
- ensure
52
- stream.reopen(old_stream)
53
- old_stream.close
54
- end
55
-
56
33
  # Blocks and ignores any exception passed as argument if raised within the block.
57
34
  #
58
35
  # suppress(ZeroDivisionError) do
@@ -63,52 +40,6 @@ module Kernel
63
40
  # puts 'This code gets executed and nothing related to ZeroDivisionError was seen'
64
41
  def suppress(*exception_classes)
65
42
  yield
66
- rescue Exception => e
67
- raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
68
- end
69
-
70
- # Captures the given stream and returns it:
71
- #
72
- # stream = capture(:stdout) { puts 'notice' }
73
- # stream # => "notice\n"
74
- #
75
- # stream = capture(:stderr) { warn 'error' }
76
- # stream # => "error\n"
77
- #
78
- # even for subprocesses:
79
- #
80
- # stream = capture(:stdout) { system('echo notice') }
81
- # stream # => "notice\n"
82
- #
83
- # stream = capture(:stderr) { system('echo error 1>&2') }
84
- # stream # => "error\n"
85
- def capture(stream)
86
- stream = stream.to_s
87
- captured_stream = Tempfile.new(stream)
88
- stream_io = eval("$#{stream}")
89
- origin_stream = stream_io.dup
90
- stream_io.reopen(captured_stream)
91
-
92
- yield
93
-
94
- stream_io.rewind
95
- return captured_stream.read
96
- ensure
97
- captured_stream.unlink
98
- stream_io.reopen(origin_stream)
99
- end
100
- alias :silence :capture
101
-
102
- # Silences both STDOUT and STDERR, even for subprocesses.
103
- #
104
- # quietly { system 'bundle install' }
105
- #
106
- # This method is not thread-safe.
107
- def quietly
108
- silence_stream(STDOUT) do
109
- silence_stream(STDERR) do
110
- yield
111
- end
112
- end
43
+ rescue *exception_classes
113
44
  end
114
45
  end
@@ -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/reporting'
2
- require 'active_support/core_ext/kernel/agnostics'
3
- require 'active_support/core_ext/kernel/debugger'
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"