activesupport 4.2.0 → 5.2.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 (254) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +366 -232
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +4 -5
  5. data/lib/active_support.rb +17 -7
  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 +7 -5
  9. data/lib/active_support/benchmarkable.rb +6 -4
  10. data/lib/active_support/builder.rb +3 -1
  11. data/lib/active_support/cache.rb +271 -177
  12. data/lib/active_support/cache/file_store.rb +41 -35
  13. data/lib/active_support/cache/mem_cache_store.rb +97 -88
  14. data/lib/active_support/cache/memory_store.rb +27 -30
  15. data/lib/active_support/cache/null_store.rb +7 -8
  16. data/lib/active_support/cache/redis_cache_store.rb +454 -0
  17. data/lib/active_support/cache/strategy/local_cache.rb +67 -34
  18. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  19. data/lib/active_support/callbacks.rb +654 -560
  20. data/lib/active_support/concern.rb +5 -3
  21. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  22. data/lib/active_support/concurrency/share_lock.rb +227 -0
  23. data/lib/active_support/configurable.rb +8 -5
  24. data/lib/active_support/core_ext.rb +3 -1
  25. data/lib/active_support/core_ext/array.rb +9 -6
  26. data/lib/active_support/core_ext/array/access.rb +29 -1
  27. data/lib/active_support/core_ext/array/conversions.rb +22 -18
  28. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  29. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  30. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  31. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
  32. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  33. data/lib/active_support/core_ext/benchmark.rb +3 -1
  34. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  35. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  36. data/lib/active_support/core_ext/class.rb +4 -3
  37. data/lib/active_support/core_ext/class/attribute.rb +41 -22
  38. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  39. data/lib/active_support/core_ext/class/subclasses.rb +20 -8
  40. data/lib/active_support/core_ext/date.rb +6 -4
  41. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  42. data/lib/active_support/core_ext/date/blank.rb +14 -0
  43. data/lib/active_support/core_ext/date/calculations.rb +11 -9
  44. data/lib/active_support/core_ext/date/conversions.rb +31 -23
  45. data/lib/active_support/core_ext/date/zones.rb +4 -2
  46. data/lib/active_support/core_ext/date_and_time/calculations.rb +179 -56
  47. data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
  48. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
  49. data/lib/active_support/core_ext/date_time.rb +7 -4
  50. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  51. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  52. data/lib/active_support/core_ext/date_time/calculations.rb +58 -20
  53. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  54. data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
  55. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  56. data/lib/active_support/core_ext/enumerable.rb +107 -28
  57. data/lib/active_support/core_ext/file.rb +3 -1
  58. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  59. data/lib/active_support/core_ext/hash.rb +11 -9
  60. data/lib/active_support/core_ext/hash/compact.rb +24 -15
  61. data/lib/active_support/core_ext/hash/conversions.rb +63 -43
  62. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  63. data/lib/active_support/core_ext/hash/except.rb +11 -8
  64. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  65. data/lib/active_support/core_ext/hash/keys.rb +33 -27
  66. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  67. data/lib/active_support/core_ext/hash/slice.rb +8 -8
  68. data/lib/active_support/core_ext/hash/transform_values.rb +16 -7
  69. data/lib/active_support/core_ext/integer.rb +5 -3
  70. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  71. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  72. data/lib/active_support/core_ext/integer/time.rb +11 -33
  73. data/lib/active_support/core_ext/kernel.rb +6 -5
  74. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  75. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  76. data/lib/active_support/core_ext/kernel/reporting.rb +4 -83
  77. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  78. data/lib/active_support/core_ext/load_error.rb +3 -22
  79. data/lib/active_support/core_ext/marshal.rb +13 -10
  80. data/lib/active_support/core_ext/module.rb +14 -11
  81. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  82. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  83. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  84. data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
  85. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
  86. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  87. data/lib/active_support/core_ext/module/delegation.rb +121 -39
  88. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  89. data/lib/active_support/core_ext/module/introspection.rb +9 -9
  90. data/lib/active_support/core_ext/module/reachable.rb +5 -2
  91. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  92. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  93. data/lib/active_support/core_ext/name_error.rb +22 -2
  94. data/lib/active_support/core_ext/numeric.rb +6 -3
  95. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  96. data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
  97. data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
  98. data/lib/active_support/core_ext/numeric/time.rb +35 -38
  99. data/lib/active_support/core_ext/object.rb +14 -13
  100. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  101. data/lib/active_support/core_ext/object/blank.rb +29 -4
  102. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  103. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  104. data/lib/active_support/core_ext/object/duplicable.rb +98 -45
  105. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  106. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  107. data/lib/active_support/core_ext/object/json.rb +49 -19
  108. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  109. data/lib/active_support/core_ext/object/to_query.rb +6 -4
  110. data/lib/active_support/core_ext/object/try.rb +70 -22
  111. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  112. data/lib/active_support/core_ext/range.rb +7 -4
  113. data/lib/active_support/core_ext/range/conversions.rb +27 -7
  114. data/lib/active_support/core_ext/range/each.rb +19 -17
  115. data/lib/active_support/core_ext/range/include_range.rb +21 -19
  116. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  117. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  118. data/lib/active_support/core_ext/regexp.rb +6 -0
  119. data/lib/active_support/core_ext/securerandom.rb +25 -0
  120. data/lib/active_support/core_ext/string.rb +15 -13
  121. data/lib/active_support/core_ext/string/access.rb +9 -7
  122. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  123. data/lib/active_support/core_ext/string/conversions.rb +8 -5
  124. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  125. data/lib/active_support/core_ext/string/filters.rb +10 -8
  126. data/lib/active_support/core_ext/string/indent.rb +6 -4
  127. data/lib/active_support/core_ext/string/inflections.rb +61 -24
  128. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  129. data/lib/active_support/core_ext/string/multibyte.rb +15 -7
  130. data/lib/active_support/core_ext/string/output_safety.rb +35 -35
  131. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  132. data/lib/active_support/core_ext/string/strip.rb +4 -5
  133. data/lib/active_support/core_ext/string/zones.rb +4 -2
  134. data/lib/active_support/core_ext/time.rb +7 -5
  135. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  136. data/lib/active_support/core_ext/time/calculations.rb +101 -51
  137. data/lib/active_support/core_ext/time/compatibility.rb +16 -0
  138. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  139. data/lib/active_support/core_ext/time/zones.rb +41 -7
  140. data/lib/active_support/core_ext/uri.rb +5 -4
  141. data/lib/active_support/current_attributes.rb +195 -0
  142. data/lib/active_support/dependencies.rb +143 -160
  143. data/lib/active_support/dependencies/autoload.rb +2 -0
  144. data/lib/active_support/dependencies/interlock.rb +57 -0
  145. data/lib/active_support/deprecation.rb +12 -9
  146. data/lib/active_support/deprecation/behaviors.rb +41 -12
  147. data/lib/active_support/deprecation/constant_accessor.rb +52 -0
  148. data/lib/active_support/deprecation/instance_delegator.rb +17 -2
  149. data/lib/active_support/deprecation/method_wrappers.rb +54 -21
  150. data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
  151. data/lib/active_support/deprecation/reporting.rb +32 -12
  152. data/lib/active_support/descendants_tracker.rb +2 -0
  153. data/lib/active_support/digest.rb +20 -0
  154. data/lib/active_support/duration.rb +326 -30
  155. data/lib/active_support/duration/iso8601_parser.rb +125 -0
  156. data/lib/active_support/duration/iso8601_serializer.rb +55 -0
  157. data/lib/active_support/encrypted_configuration.rb +49 -0
  158. data/lib/active_support/encrypted_file.rb +99 -0
  159. data/lib/active_support/evented_file_update_checker.rb +205 -0
  160. data/lib/active_support/execution_wrapper.rb +128 -0
  161. data/lib/active_support/executor.rb +8 -0
  162. data/lib/active_support/file_update_checker.rb +63 -37
  163. data/lib/active_support/gem_version.rb +4 -2
  164. data/lib/active_support/gzip.rb +7 -5
  165. data/lib/active_support/hash_with_indifferent_access.rb +130 -30
  166. data/lib/active_support/i18n.rb +8 -6
  167. data/lib/active_support/i18n_railtie.rb +34 -14
  168. data/lib/active_support/inflections.rb +13 -11
  169. data/lib/active_support/inflector.rb +7 -5
  170. data/lib/active_support/inflector/inflections.rb +61 -12
  171. data/lib/active_support/inflector/methods.rb +161 -136
  172. data/lib/active_support/inflector/transliterate.rb +48 -27
  173. data/lib/active_support/json.rb +4 -2
  174. data/lib/active_support/json/decoding.rb +16 -13
  175. data/lib/active_support/json/encoding.rb +15 -57
  176. data/lib/active_support/key_generator.rb +25 -25
  177. data/lib/active_support/lazy_load_hooks.rb +50 -20
  178. data/lib/active_support/locale/en.yml +2 -0
  179. data/lib/active_support/log_subscriber.rb +13 -10
  180. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  181. data/lib/active_support/logger.rb +54 -3
  182. data/lib/active_support/logger_silence.rb +12 -7
  183. data/lib/active_support/logger_thread_safe_level.rb +33 -0
  184. data/lib/active_support/message_encryptor.rb +173 -51
  185. data/lib/active_support/message_verifier.rb +150 -17
  186. data/lib/active_support/messages/metadata.rb +71 -0
  187. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  188. data/lib/active_support/messages/rotator.rb +56 -0
  189. data/lib/active_support/multibyte.rb +4 -2
  190. data/lib/active_support/multibyte/chars.rb +37 -24
  191. data/lib/active_support/multibyte/unicode.rb +100 -96
  192. data/lib/active_support/notifications.rb +11 -7
  193. data/lib/active_support/notifications/fanout.rb +10 -8
  194. data/lib/active_support/notifications/instrumenter.rb +27 -7
  195. data/lib/active_support/number_helper.rb +94 -68
  196. data/lib/active_support/number_helper/number_converter.rb +13 -11
  197. data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
  198. data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
  199. data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
  200. data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
  201. data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
  202. data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
  203. data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
  204. data/lib/active_support/number_helper/rounding_helper.rb +66 -0
  205. data/lib/active_support/option_merger.rb +3 -1
  206. data/lib/active_support/ordered_hash.rb +6 -4
  207. data/lib/active_support/ordered_options.rb +22 -4
  208. data/lib/active_support/per_thread_registry.rb +13 -6
  209. data/lib/active_support/proxy_object.rb +2 -0
  210. data/lib/active_support/rails.rb +16 -8
  211. data/lib/active_support/railtie.rb +43 -9
  212. data/lib/active_support/reloader.rb +131 -0
  213. data/lib/active_support/rescuable.rb +108 -53
  214. data/lib/active_support/security_utils.rb +17 -6
  215. data/lib/active_support/string_inquirer.rb +11 -3
  216. data/lib/active_support/subscriber.rb +15 -14
  217. data/lib/active_support/tagged_logging.rb +14 -11
  218. data/lib/active_support/test_case.rb +18 -46
  219. data/lib/active_support/testing/assertions.rb +137 -20
  220. data/lib/active_support/testing/autorun.rb +4 -2
  221. data/lib/active_support/testing/constant_lookup.rb +2 -1
  222. data/lib/active_support/testing/declarative.rb +3 -1
  223. data/lib/active_support/testing/deprecation.rb +14 -10
  224. data/lib/active_support/testing/file_fixtures.rb +36 -0
  225. data/lib/active_support/testing/isolation.rb +34 -25
  226. data/lib/active_support/testing/method_call_assertions.rb +43 -0
  227. data/lib/active_support/testing/setup_and_teardown.rb +12 -3
  228. data/lib/active_support/testing/stream.rb +44 -0
  229. data/lib/active_support/testing/tagged_logging.rb +3 -1
  230. data/lib/active_support/testing/time_helpers.rb +96 -27
  231. data/lib/active_support/time.rb +14 -12
  232. data/lib/active_support/time_with_zone.rb +195 -53
  233. data/lib/active_support/values/time_zone.rb +200 -61
  234. data/lib/active_support/values/unicode_tables.dat +0 -0
  235. data/lib/active_support/version.rb +3 -1
  236. data/lib/active_support/xml_mini.rb +69 -51
  237. data/lib/active_support/xml_mini/jdom.rb +116 -113
  238. data/lib/active_support/xml_mini/libxml.rb +17 -16
  239. data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
  240. data/lib/active_support/xml_mini/nokogiri.rb +15 -15
  241. data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
  242. data/lib/active_support/xml_mini/rexml.rb +17 -16
  243. metadata +55 -43
  244. data/lib/active_support/concurrency/latch.rb +0 -27
  245. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
  246. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  247. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  248. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  249. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -11
  250. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  251. data/lib/active_support/core_ext/object/itself.rb +0 -15
  252. data/lib/active_support/core_ext/struct.rb +0 -6
  253. data/lib/active_support/core_ext/thread.rb +0 -86
  254. data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -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
@@ -1,28 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Hash
2
- # Returns a new hash with all keys converted using the block operation.
4
+ # Returns a new hash with all keys converted using the +block+ operation.
3
5
  #
4
6
  # hash = { name: 'Rob', age: '28' }
5
7
  #
6
- # hash.transform_keys{ |key| key.to_s.upcase }
7
- # # => {"NAME"=>"Rob", "AGE"=>"28"}
8
+ # hash.transform_keys { |key| key.to_s.upcase } # => {"NAME"=>"Rob", "AGE"=>"28"}
9
+ #
10
+ # If you do not provide a +block+, it will return an Enumerator
11
+ # for chaining with other methods:
12
+ #
13
+ # hash.transform_keys.with_index { |k, i| [k, i].join } # => {"name0"=>"Rob", "age1"=>"28"}
8
14
  def transform_keys
9
- return enum_for(:transform_keys) unless block_given?
10
- result = self.class.new
15
+ return enum_for(:transform_keys) { size } unless block_given?
16
+ result = {}
11
17
  each_key do |key|
12
18
  result[yield(key)] = self[key]
13
19
  end
14
20
  result
15
- end
21
+ end unless method_defined? :transform_keys
16
22
 
17
- # Destructively convert all keys using the block operations.
18
- # Same as transform_keys but modifies +self+.
23
+ # Destructively converts all keys using the +block+ operations.
24
+ # Same as +transform_keys+ but modifies +self+.
19
25
  def transform_keys!
20
- return enum_for(:transform_keys!) unless block_given?
26
+ return enum_for(:transform_keys!) { size } unless block_given?
21
27
  keys.each do |key|
22
28
  self[yield(key)] = delete(key)
23
29
  end
24
30
  self
25
- end
31
+ end unless method_defined? :transform_keys!
26
32
 
27
33
  # Returns a new hash with all keys converted to strings.
28
34
  #
@@ -31,13 +37,13 @@ class Hash
31
37
  # hash.stringify_keys
32
38
  # # => {"name"=>"Rob", "age"=>"28"}
33
39
  def stringify_keys
34
- transform_keys{ |key| key.to_s }
40
+ transform_keys(&:to_s)
35
41
  end
36
42
 
37
- # Destructively convert all keys to strings. Same as
43
+ # Destructively converts all keys to strings. Same as
38
44
  # +stringify_keys+, but modifies +self+.
39
45
  def stringify_keys!
40
- transform_keys!{ |key| key.to_s }
46
+ transform_keys!(&:to_s)
41
47
  end
42
48
 
43
49
  # Returns a new hash with all keys converted to symbols, as long as
@@ -48,19 +54,19 @@ class Hash
48
54
  # hash.symbolize_keys
49
55
  # # => {:name=>"Rob", :age=>"28"}
50
56
  def symbolize_keys
51
- transform_keys{ |key| key.to_sym rescue key }
57
+ transform_keys { |key| key.to_sym rescue key }
52
58
  end
53
59
  alias_method :to_options, :symbolize_keys
54
60
 
55
- # Destructively convert all keys to symbols, as long as they respond
61
+ # Destructively converts all keys to symbols, as long as they respond
56
62
  # to +to_sym+. Same as +symbolize_keys+, but modifies +self+.
57
63
  def symbolize_keys!
58
- transform_keys!{ |key| key.to_sym rescue key }
64
+ transform_keys! { |key| key.to_sym rescue key }
59
65
  end
60
66
  alias_method :to_options!, :symbolize_keys!
61
67
 
62
- # Validate all keys in a hash match <tt>*valid_keys</tt>, raising
63
- # ArgumentError on a mismatch.
68
+ # Validates all keys in a hash match <tt>*valid_keys</tt>, raising
69
+ # +ArgumentError+ on a mismatch.
64
70
  #
65
71
  # Note that keys are treated differently than HashWithIndifferentAccess,
66
72
  # meaning that string and symbol keys will not match.
@@ -89,7 +95,7 @@ class Hash
89
95
  _deep_transform_keys_in_object(self, &block)
90
96
  end
91
97
 
92
- # Destructively convert all keys by using the block operation.
98
+ # Destructively converts all keys by using the block operation.
93
99
  # This includes the keys from the root hash and from all
94
100
  # nested hashes and arrays.
95
101
  def deep_transform_keys!(&block)
@@ -105,14 +111,14 @@ class Hash
105
111
  # hash.deep_stringify_keys
106
112
  # # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
107
113
  def deep_stringify_keys
108
- deep_transform_keys{ |key| key.to_s }
114
+ deep_transform_keys(&:to_s)
109
115
  end
110
116
 
111
- # Destructively convert all keys to strings.
117
+ # Destructively converts all keys to strings.
112
118
  # This includes the keys from the root hash and from all
113
119
  # nested hashes and arrays.
114
120
  def deep_stringify_keys!
115
- deep_transform_keys!{ |key| key.to_s }
121
+ deep_transform_keys!(&:to_s)
116
122
  end
117
123
 
118
124
  # Returns a new hash with all keys converted to symbols, as long as
@@ -124,14 +130,14 @@ class Hash
124
130
  # hash.deep_symbolize_keys
125
131
  # # => {:person=>{:name=>"Rob", :age=>"28"}}
126
132
  def deep_symbolize_keys
127
- deep_transform_keys{ |key| key.to_sym rescue key }
133
+ deep_transform_keys { |key| key.to_sym rescue key }
128
134
  end
129
135
 
130
- # Destructively convert all keys to symbols, as long as they respond
136
+ # Destructively converts all keys to symbols, as long as they respond
131
137
  # to +to_sym+. This includes the keys from the root hash and from all
132
138
  # nested hashes and arrays.
133
139
  def deep_symbolize_keys!
134
- deep_transform_keys!{ |key| key.to_sym rescue key }
140
+ deep_transform_keys! { |key| key.to_sym rescue key }
135
141
  end
136
142
 
137
143
  private
@@ -143,7 +149,7 @@ class Hash
143
149
  result[yield(key)] = _deep_transform_keys_in_object(value, &block)
144
150
  end
145
151
  when Array
146
- object.map {|e| _deep_transform_keys_in_object(e, &block) }
152
+ object.map { |e| _deep_transform_keys_in_object(e, &block) }
147
153
  else
148
154
  object
149
155
  end
@@ -158,7 +164,7 @@ class Hash
158
164
  end
159
165
  object
160
166
  when Array
161
- object.map! {|e| _deep_transform_keys_in_object!(e, &block)}
167
+ object.map! { |e| _deep_transform_keys_in_object!(e, &block) }
162
168
  else
163
169
  object
164
170
  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,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Hash
2
- # Slice a hash to include only the given keys. Returns a hash containing
4
+ # Slices a hash to include only the given keys. Returns a hash containing
3
5
  # the given keys.
4
- #
6
+ #
5
7
  # { a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b)
6
8
  # # => {:a=>1, :b=>2}
7
- #
8
- # This is useful for limiting an options hash to valid keys before
9
+ #
10
+ # This is useful for limiting an options hash to valid keys before
9
11
  # passing to a method:
10
12
  #
11
13
  # def search(criteria = {})
@@ -19,9 +21,8 @@ class Hash
19
21
  # valid_keys = [:mass, :velocity, :time]
20
22
  # search(options.slice(*valid_keys))
21
23
  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
24
+ keys.each_with_object(Hash.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
25
+ end unless method_defined?(:slice)
25
26
 
26
27
  # Replaces the hash with only the given keys.
27
28
  # Returns a hash containing the removed key/value pairs.
@@ -29,7 +30,6 @@ class Hash
29
30
  # { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
30
31
  # # => {:c=>3, :d=>4}
31
32
  def slice!(*keys)
32
- keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
33
33
  omit = slice(*self.keys - keys)
34
34
  hash = slice(*keys)
35
35
  hash.default = default
@@ -1,23 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Hash
2
4
  # Returns a new hash with the results of running +block+ once for every value.
3
5
  # The keys are unchanged.
4
6
  #
5
- # { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 }
6
- # # => { a: 2, b: 4, c: 6 }
7
+ # { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 } # => { a: 2, b: 4, c: 6 }
8
+ #
9
+ # If you do not provide a +block+, it will return an Enumerator
10
+ # for chaining with other methods:
11
+ #
12
+ # { a: 1, b: 2 }.transform_values.with_index { |v, i| [v, i].join.to_i } # => { a: 10, b: 21 }
7
13
  def transform_values
8
- return enum_for(:transform_values) unless block_given?
14
+ return enum_for(:transform_values) { size } unless block_given?
15
+ return {} if empty?
9
16
  result = self.class.new
10
17
  each do |key, value|
11
18
  result[key] = yield(value)
12
19
  end
13
20
  result
14
- end
21
+ end unless method_defined? :transform_values
15
22
 
16
- # Destructive +transform_values+
23
+ # Destructively converts all values using the +block+ operations.
24
+ # Same as +transform_values+ but modifies +self+.
17
25
  def transform_values!
18
- return enum_for(:transform_values!) unless block_given?
26
+ return enum_for(:transform_values!) { size } unless block_given?
19
27
  each do |key, value|
20
28
  self[key] = yield(value)
21
29
  end
22
- end
30
+ end unless method_defined? :transform_values!
31
+ # TODO: Remove this file when supporting only Ruby 2.4+.
23
32
  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/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
  #
@@ -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,5 +1,6 @@
1
- require 'active_support/core_ext/kernel/agnostics'
2
- require 'active_support/core_ext/kernel/concern'
3
- require 'active_support/core_ext/kernel/debugger' if RUBY_VERSION < '2.0.0'
4
- require 'active_support/core_ext/kernel/reporting'
5
- require 'active_support/core_ext/kernel/singleton_class'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/kernel/agnostics"
4
+ require "active_support/core_ext/kernel/concern"
5
+ require "active_support/core_ext/kernel/reporting"
6
+ require "active_support/core_ext/kernel/singleton_class"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Object
2
4
  # Makes backticks behave (somewhat more) similarly on all platforms.
3
5
  # On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the
@@ -1,6 +1,10 @@
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
6
+ module_function
7
+
4
8
  # A shortcut to define a toplevel concern, not within a module.
5
9
  #
6
10
  # See Module::Concerning for more.
@@ -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
@@ -29,34 +30,6 @@ module Kernel
29
30
  $VERBOSE = old_verbose
30
31
  end
31
32
 
32
- # For compatibility
33
- def silence_stderr #:nodoc:
34
- ActiveSupport::Deprecation.warn(
35
- "`#silence_stderr` is deprecated and will be removed in the next release."
36
- ) #not thread-safe
37
- silence_stream(STDERR) { yield }
38
- end
39
-
40
- # Deprecated : this method is not thread safe
41
- # Silences any stream for the duration of the block.
42
- #
43
- # silence_stream(STDOUT) do
44
- # puts 'This will never be seen'
45
- # end
46
- #
47
- # puts 'But this will'
48
- #
49
- # This method is not thread-safe.
50
- def silence_stream(stream)
51
- old_stream = stream.dup
52
- stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
53
- stream.sync = true
54
- yield
55
- ensure
56
- stream.reopen(old_stream)
57
- old_stream.close
58
- end
59
-
60
33
  # Blocks and ignores any exception passed as argument if raised within the block.
61
34
  #
62
35
  # suppress(ZeroDivisionError) do
@@ -69,56 +42,4 @@ module Kernel
69
42
  yield
70
43
  rescue *exception_classes
71
44
  end
72
-
73
- # Captures the given stream and returns it:
74
- #
75
- # stream = capture(:stdout) { puts 'notice' }
76
- # stream # => "notice\n"
77
- #
78
- # stream = capture(:stderr) { warn 'error' }
79
- # stream # => "error\n"
80
- #
81
- # even for subprocesses:
82
- #
83
- # stream = capture(:stdout) { system('echo notice') }
84
- # stream # => "notice\n"
85
- #
86
- # stream = capture(:stderr) { system('echo error 1>&2') }
87
- # stream # => "error\n"
88
- def capture(stream)
89
- ActiveSupport::Deprecation.warn(
90
- "`#capture(stream)` is deprecated and will be removed in the next release."
91
- ) #not thread-safe
92
- stream = stream.to_s
93
- captured_stream = Tempfile.new(stream)
94
- stream_io = eval("$#{stream}")
95
- origin_stream = stream_io.dup
96
- stream_io.reopen(captured_stream)
97
-
98
- yield
99
-
100
- stream_io.rewind
101
- return captured_stream.read
102
- ensure
103
- captured_stream.close
104
- captured_stream.unlink
105
- stream_io.reopen(origin_stream)
106
- end
107
- alias :silence :capture
108
-
109
- # Silences both STDOUT and STDERR, even for subprocesses.
110
- #
111
- # quietly { system 'bundle install' }
112
- #
113
- # This method is not thread-safe.
114
- def quietly
115
- ActiveSupport::Deprecation.warn(
116
- "`#quietly` is deprecated and will be removed in the next release."
117
- ) #not thread-safe
118
- silence_stream(STDOUT) do
119
- silence_stream(STDERR) do
120
- yield
121
- end
122
- end
123
- end
124
45
  end