activesupport 4.2.11.1 → 5.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 (256) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +399 -440
  3. data/MIT-LICENSE +2 -2
  4. data/README.rdoc +4 -5
  5. data/lib/active_support/all.rb +5 -3
  6. data/lib/active_support/array_inquirer.rb +48 -0
  7. data/lib/active_support/backtrace_cleaner.rb +7 -5
  8. data/lib/active_support/benchmarkable.rb +6 -4
  9. data/lib/active_support/builder.rb +3 -1
  10. data/lib/active_support/cache/file_store.rb +41 -35
  11. data/lib/active_support/cache/mem_cache_store.rb +97 -88
  12. data/lib/active_support/cache/memory_store.rb +27 -30
  13. data/lib/active_support/cache/null_store.rb +7 -8
  14. data/lib/active_support/cache/redis_cache_store.rb +461 -0
  15. data/lib/active_support/cache/strategy/local_cache.rb +67 -34
  16. data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
  17. data/lib/active_support/cache.rb +287 -196
  18. data/lib/active_support/callbacks.rb +640 -590
  19. data/lib/active_support/concern.rb +11 -5
  20. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -0
  21. data/lib/active_support/concurrency/share_lock.rb +227 -0
  22. data/lib/active_support/configurable.rb +8 -5
  23. data/lib/active_support/core_ext/array/access.rb +29 -1
  24. data/lib/active_support/core_ext/array/conversions.rb +22 -18
  25. data/lib/active_support/core_ext/array/extract_options.rb +2 -0
  26. data/lib/active_support/core_ext/array/grouping.rb +11 -18
  27. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  28. data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
  29. data/lib/active_support/core_ext/array/wrap.rb +7 -4
  30. data/lib/active_support/core_ext/array.rb +9 -6
  31. data/lib/active_support/core_ext/benchmark.rb +3 -1
  32. data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
  33. data/lib/active_support/core_ext/big_decimal.rb +3 -1
  34. data/lib/active_support/core_ext/class/attribute.rb +41 -22
  35. data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
  36. data/lib/active_support/core_ext/class/subclasses.rb +20 -6
  37. data/lib/active_support/core_ext/class.rb +4 -3
  38. data/lib/active_support/core_ext/date/acts_like.rb +3 -1
  39. data/lib/active_support/core_ext/date/blank.rb +14 -0
  40. data/lib/active_support/core_ext/date/calculations.rb +11 -9
  41. data/lib/active_support/core_ext/date/conversions.rb +25 -23
  42. data/lib/active_support/core_ext/date/zones.rb +4 -2
  43. data/lib/active_support/core_ext/date.rb +6 -4
  44. data/lib/active_support/core_ext/date_and_time/calculations.rb +170 -58
  45. data/lib/active_support/core_ext/date_and_time/compatibility.rb +4 -3
  46. data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
  47. data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
  48. data/lib/active_support/core_ext/date_time/blank.rb +14 -0
  49. data/lib/active_support/core_ext/date_time/calculations.rb +36 -18
  50. data/lib/active_support/core_ext/date_time/compatibility.rb +8 -6
  51. data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
  52. data/lib/active_support/core_ext/date_time.rb +7 -5
  53. data/lib/active_support/core_ext/digest/uuid.rb +7 -5
  54. data/lib/active_support/core_ext/digest.rb +3 -0
  55. data/lib/active_support/core_ext/enumerable.rb +101 -33
  56. data/lib/active_support/core_ext/file/atomic.rb +38 -31
  57. data/lib/active_support/core_ext/file.rb +3 -1
  58. data/lib/active_support/core_ext/hash/compact.rb +14 -9
  59. data/lib/active_support/core_ext/hash/conversions.rb +62 -41
  60. data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
  61. data/lib/active_support/core_ext/hash/except.rb +11 -8
  62. data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
  63. data/lib/active_support/core_ext/hash/keys.rb +33 -27
  64. data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
  65. data/lib/active_support/core_ext/hash/slice.rb +8 -8
  66. data/lib/active_support/core_ext/hash/transform_values.rb +14 -5
  67. data/lib/active_support/core_ext/hash.rb +11 -9
  68. data/lib/active_support/core_ext/integer/inflections.rb +3 -1
  69. data/lib/active_support/core_ext/integer/multiple.rb +2 -0
  70. data/lib/active_support/core_ext/integer/time.rb +11 -18
  71. data/lib/active_support/core_ext/integer.rb +5 -3
  72. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
  73. data/lib/active_support/core_ext/kernel/concern.rb +5 -1
  74. data/lib/active_support/core_ext/kernel/reporting.rb +4 -84
  75. data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
  76. data/lib/active_support/core_ext/kernel.rb +6 -5
  77. data/lib/active_support/core_ext/load_error.rb +3 -22
  78. data/lib/active_support/core_ext/marshal.rb +8 -8
  79. data/lib/active_support/core_ext/module/aliasing.rb +6 -44
  80. data/lib/active_support/core_ext/module/anonymous.rb +12 -1
  81. data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
  82. data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
  83. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
  84. data/lib/active_support/core_ext/module/concerning.rb +11 -12
  85. data/lib/active_support/core_ext/module/delegation.rb +99 -29
  86. data/lib/active_support/core_ext/module/deprecation.rb +4 -2
  87. data/lib/active_support/core_ext/module/introspection.rb +9 -9
  88. data/lib/active_support/core_ext/module/reachable.rb +5 -2
  89. data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
  90. data/lib/active_support/core_ext/module/remove_method.rb +8 -3
  91. data/lib/active_support/core_ext/module.rb +14 -11
  92. data/lib/active_support/core_ext/name_error.rb +22 -2
  93. data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
  94. data/lib/active_support/core_ext/numeric/conversions.rb +78 -81
  95. data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
  96. data/lib/active_support/core_ext/numeric/time.rb +35 -23
  97. data/lib/active_support/core_ext/numeric.rb +6 -3
  98. data/lib/active_support/core_ext/object/acts_like.rb +12 -1
  99. data/lib/active_support/core_ext/object/blank.rb +27 -2
  100. data/lib/active_support/core_ext/object/conversions.rb +6 -4
  101. data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
  102. data/lib/active_support/core_ext/object/duplicable.rb +41 -14
  103. data/lib/active_support/core_ext/object/inclusion.rb +5 -3
  104. data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
  105. data/lib/active_support/core_ext/object/json.rb +49 -19
  106. data/lib/active_support/core_ext/object/to_param.rb +3 -1
  107. data/lib/active_support/core_ext/object/to_query.rb +10 -5
  108. data/lib/active_support/core_ext/object/try.rb +69 -21
  109. data/lib/active_support/core_ext/object/with_options.rb +16 -3
  110. data/lib/active_support/core_ext/object.rb +14 -13
  111. data/lib/active_support/core_ext/range/compare_range.rb +61 -0
  112. data/lib/active_support/core_ext/range/conversions.rb +27 -7
  113. data/lib/active_support/core_ext/range/each.rb +19 -17
  114. data/lib/active_support/core_ext/range/include_range.rb +2 -22
  115. data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
  116. data/lib/active_support/core_ext/range/overlaps.rb +2 -0
  117. data/lib/active_support/core_ext/range.rb +7 -4
  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/access.rb +8 -6
  121. data/lib/active_support/core_ext/string/behavior.rb +3 -1
  122. data/lib/active_support/core_ext/string/conversions.rb +7 -4
  123. data/lib/active_support/core_ext/string/exclude.rb +2 -0
  124. data/lib/active_support/core_ext/string/filters.rb +6 -5
  125. data/lib/active_support/core_ext/string/indent.rb +6 -4
  126. data/lib/active_support/core_ext/string/inflections.rb +61 -24
  127. data/lib/active_support/core_ext/string/inquiry.rb +3 -1
  128. data/lib/active_support/core_ext/string/multibyte.rb +15 -7
  129. data/lib/active_support/core_ext/string/output_safety.rb +34 -38
  130. data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
  131. data/lib/active_support/core_ext/string/strip.rb +4 -5
  132. data/lib/active_support/core_ext/string/zones.rb +4 -2
  133. data/lib/active_support/core_ext/string.rb +15 -13
  134. data/lib/active_support/core_ext/time/acts_like.rb +3 -1
  135. data/lib/active_support/core_ext/time/calculations.rb +85 -51
  136. data/lib/active_support/core_ext/time/compatibility.rb +4 -2
  137. data/lib/active_support/core_ext/time/conversions.rb +20 -13
  138. data/lib/active_support/core_ext/time/zones.rb +41 -7
  139. data/lib/active_support/core_ext/time.rb +7 -6
  140. data/lib/active_support/core_ext/uri.rb +6 -8
  141. data/lib/active_support/core_ext.rb +3 -1
  142. data/lib/active_support/current_attributes.rb +195 -0
  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/dependencies.rb +152 -161
  146. data/lib/active_support/deprecation/behaviors.rb +44 -11
  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 +66 -20
  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/deprecation.rb +12 -9
  153. data/lib/active_support/descendants_tracker.rb +2 -0
  154. data/lib/active_support/digest.rb +20 -0
  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/duration.rb +307 -35
  158. data/lib/active_support/encrypted_configuration.rb +49 -0
  159. data/lib/active_support/encrypted_file.rb +99 -0
  160. data/lib/active_support/evented_file_update_checker.rb +205 -0
  161. data/lib/active_support/execution_wrapper.rb +128 -0
  162. data/lib/active_support/executor.rb +8 -0
  163. data/lib/active_support/file_update_checker.rb +63 -37
  164. data/lib/active_support/gem_version.rb +6 -4
  165. data/lib/active_support/gzip.rb +7 -5
  166. data/lib/active_support/hash_with_indifferent_access.rb +123 -28
  167. data/lib/active_support/i18n.rb +8 -6
  168. data/lib/active_support/i18n_railtie.rb +37 -13
  169. data/lib/active_support/inflections.rb +13 -11
  170. data/lib/active_support/inflector/inflections.rb +61 -12
  171. data/lib/active_support/inflector/methods.rb +163 -136
  172. data/lib/active_support/inflector/transliterate.rb +48 -27
  173. data/lib/active_support/inflector.rb +7 -5
  174. data/lib/active_support/json/decoding.rb +16 -13
  175. data/lib/active_support/json/encoding.rb +11 -58
  176. data/lib/active_support/json.rb +4 -2
  177. data/lib/active_support/key_generator.rb +25 -25
  178. data/lib/active_support/lazy_load_hooks.rb +50 -20
  179. data/lib/active_support/locale/en.yml +2 -0
  180. data/lib/active_support/log_subscriber/test_helper.rb +14 -12
  181. data/lib/active_support/log_subscriber.rb +13 -10
  182. data/lib/active_support/logger.rb +8 -7
  183. data/lib/active_support/logger_silence.rb +6 -4
  184. data/lib/active_support/logger_thread_safe_level.rb +7 -5
  185. data/lib/active_support/message_encryptor.rb +168 -53
  186. data/lib/active_support/message_verifier.rb +150 -17
  187. data/lib/active_support/messages/metadata.rb +71 -0
  188. data/lib/active_support/messages/rotation_configuration.rb +22 -0
  189. data/lib/active_support/messages/rotator.rb +56 -0
  190. data/lib/active_support/multibyte/chars.rb +36 -23
  191. data/lib/active_support/multibyte/unicode.rb +100 -96
  192. data/lib/active_support/multibyte.rb +4 -2
  193. data/lib/active_support/notifications/fanout.rb +11 -9
  194. data/lib/active_support/notifications/instrumenter.rb +27 -7
  195. data/lib/active_support/notifications.rb +11 -7
  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/number_helper.rb +94 -68
  206. data/lib/active_support/option_merger.rb +3 -1
  207. data/lib/active_support/ordered_hash.rb +6 -4
  208. data/lib/active_support/ordered_options.rb +23 -5
  209. data/lib/active_support/per_thread_registry.rb +9 -4
  210. data/lib/active_support/proxy_object.rb +2 -0
  211. data/lib/active_support/rails.rb +16 -8
  212. data/lib/active_support/railtie.rb +43 -9
  213. data/lib/active_support/reloader.rb +131 -0
  214. data/lib/active_support/rescuable.rb +108 -53
  215. data/lib/active_support/security_utils.rb +15 -11
  216. data/lib/active_support/string_inquirer.rb +11 -3
  217. data/lib/active_support/subscriber.rb +21 -16
  218. data/lib/active_support/tagged_logging.rb +14 -11
  219. data/lib/active_support/test_case.rb +19 -47
  220. data/lib/active_support/testing/assertions.rb +137 -20
  221. data/lib/active_support/testing/autorun.rb +4 -2
  222. data/lib/active_support/testing/constant_lookup.rb +2 -1
  223. data/lib/active_support/testing/declarative.rb +3 -1
  224. data/lib/active_support/testing/deprecation.rb +14 -10
  225. data/lib/active_support/testing/file_fixtures.rb +36 -0
  226. data/lib/active_support/testing/isolation.rb +34 -25
  227. data/lib/active_support/testing/method_call_assertions.rb +43 -0
  228. data/lib/active_support/testing/setup_and_teardown.rb +13 -8
  229. data/lib/active_support/testing/stream.rb +44 -0
  230. data/lib/active_support/testing/tagged_logging.rb +3 -1
  231. data/lib/active_support/testing/time_helpers.rb +81 -15
  232. data/lib/active_support/time.rb +14 -12
  233. data/lib/active_support/time_with_zone.rb +169 -39
  234. data/lib/active_support/values/time_zone.rb +196 -61
  235. data/lib/active_support/values/unicode_tables.dat +0 -0
  236. data/lib/active_support/version.rb +3 -1
  237. data/lib/active_support/xml_mini/jdom.rb +116 -114
  238. data/lib/active_support/xml_mini/libxml.rb +16 -13
  239. data/lib/active_support/xml_mini/libxmlsax.rb +15 -14
  240. data/lib/active_support/xml_mini/nokogiri.rb +14 -12
  241. data/lib/active_support/xml_mini/nokogirisax.rb +14 -13
  242. data/lib/active_support/xml_mini/rexml.rb +11 -9
  243. data/lib/active_support/xml_mini.rb +37 -37
  244. data/lib/active_support.rb +12 -11
  245. metadata +54 -24
  246. data/lib/active_support/concurrency/latch.rb +0 -27
  247. data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
  248. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
  249. data/lib/active_support/core_ext/date_time/zones.rb +0 -6
  250. data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
  251. data/lib/active_support/core_ext/module/method_transplanting.rb +0 -13
  252. data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
  253. data/lib/active_support/core_ext/object/itself.rb +0 -15
  254. data/lib/active_support/core_ext/struct.rb +0 -6
  255. data/lib/active_support/core_ext/thread.rb +0 -86
  256. data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -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,11 +1,18 @@
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)
@@ -13,11 +20,13 @@ class Hash
13
20
  result
14
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
30
  end unless method_defined? :transform_values!
31
+ # TODO: Remove this file when supporting only Ruby 2.4+.
23
32
  end
@@ -1,9 +1,11 @@
1
- require 'active_support/core_ext/hash/compact'
2
- require 'active_support/core_ext/hash/conversions'
3
- require 'active_support/core_ext/hash/deep_merge'
4
- require 'active_support/core_ext/hash/except'
5
- require 'active_support/core_ext/hash/indifferent_access'
6
- require 'active_support/core_ext/hash/keys'
7
- require 'active_support/core_ext/hash/reverse_merge'
8
- require 'active_support/core_ext/hash/slice'
9
- require 'active_support/core_ext/hash/transform_values'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/hash/compact"
4
+ require "active_support/core_ext/hash/conversions"
5
+ require "active_support/core_ext/hash/deep_merge"
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"
11
+ require "active_support/core_ext/hash/transform_values"
@@ -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,29 +1,22 @@
1
- require 'active_support/duration'
2
- require 'active_support/core_ext/numeric/time'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/duration"
4
+ require "active_support/core_ext/numeric/time"
3
5
 
4
6
  class Integer
5
- # Enables the use of time calculations and declarations, like <tt>45.minutes +
6
- # 2.hours + 4.years</tt>.
7
- #
8
- # These methods use Time#advance for precise date calculations when using
9
- # <tt>from_now</tt>, +ago+, etc. as well as adding or subtracting their
10
- # results from a Time object.
11
- #
12
- # # equivalent to Time.now.advance(months: 1)
13
- # 1.month.from_now
7
+ # Returns a Duration instance matching the number of months provided.
14
8
  #
15
- # # equivalent to Time.now.advance(years: 2)
16
- # 2.years.from_now
17
- #
18
- # # equivalent to Time.now.advance(months: 4, years: 5)
19
- # (4.months + 5.years).from_now
9
+ # 2.months # => 2 months
20
10
  def months
21
- ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
11
+ ActiveSupport::Duration.months(self)
22
12
  end
23
13
  alias :month :months
24
14
 
15
+ # Returns a Duration instance matching the number of years provided.
16
+ #
17
+ # 2.years # => 2 years
25
18
  def years
26
- ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]])
19
+ ActiveSupport::Duration.years(self)
27
20
  end
28
21
  alias :year :years
29
22
  end
@@ -1,3 +1,5 @@
1
- require 'active_support/core_ext/integer/multiple'
2
- require 'active_support/core_ext/integer/inflections'
3
- require 'active_support/core_ext/integer/time'
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/integer/multiple"
4
+ require "active_support/core_ext/integer/inflections"
5
+ require "active_support/core_ext/integer/time"
@@ -1,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,9 +1,9 @@
1
- require 'rbconfig'
2
- require 'tempfile'
3
- require 'active_support/deprecation'
1
+ # frozen_string_literal: true
4
2
 
5
3
  module Kernel
6
- # 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
7
7
  # value afterwards.
8
8
  #
9
9
  # silence_warnings do
@@ -30,34 +30,6 @@ module Kernel
30
30
  $VERBOSE = old_verbose
31
31
  end
32
32
 
33
- # For compatibility
34
- def silence_stderr #:nodoc:
35
- ActiveSupport::Deprecation.warn(
36
- "`#silence_stderr` is deprecated and will be removed in the next release."
37
- ) #not thread-safe
38
- silence_stream(STDERR) { yield }
39
- end
40
-
41
- # Deprecated : this method is not thread safe
42
- # Silences any stream for the duration of the block.
43
- #
44
- # silence_stream(STDOUT) do
45
- # puts 'This will never be seen'
46
- # end
47
- #
48
- # puts 'But this will'
49
- #
50
- # This method is not thread-safe.
51
- def silence_stream(stream)
52
- old_stream = stream.dup
53
- stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
54
- stream.sync = true
55
- yield
56
- ensure
57
- stream.reopen(old_stream)
58
- old_stream.close
59
- end
60
-
61
33
  # Blocks and ignores any exception passed as argument if raised within the block.
62
34
  #
63
35
  # suppress(ZeroDivisionError) do
@@ -70,56 +42,4 @@ module Kernel
70
42
  yield
71
43
  rescue *exception_classes
72
44
  end
73
-
74
- # Captures the given stream and returns it:
75
- #
76
- # stream = capture(:stdout) { puts 'notice' }
77
- # stream # => "notice\n"
78
- #
79
- # stream = capture(:stderr) { warn 'error' }
80
- # stream # => "error\n"
81
- #
82
- # even for subprocesses:
83
- #
84
- # stream = capture(:stdout) { system('echo notice') }
85
- # stream # => "notice\n"
86
- #
87
- # stream = capture(:stderr) { system('echo error 1>&2') }
88
- # stream # => "error\n"
89
- def capture(stream)
90
- ActiveSupport::Deprecation.warn(
91
- "`#capture(stream)` is deprecated and will be removed in the next release."
92
- ) #not thread-safe
93
- stream = stream.to_s
94
- captured_stream = Tempfile.new(stream)
95
- stream_io = eval("$#{stream}")
96
- origin_stream = stream_io.dup
97
- stream_io.reopen(captured_stream)
98
-
99
- yield
100
-
101
- stream_io.rewind
102
- return captured_stream.read
103
- ensure
104
- captured_stream.close
105
- captured_stream.unlink
106
- stream_io.reopen(origin_stream)
107
- end
108
- alias :silence :capture
109
-
110
- # Silences both STDOUT and STDERR, even for subprocesses.
111
- #
112
- # quietly { system 'bundle install' }
113
- #
114
- # This method is not thread-safe.
115
- def quietly
116
- ActiveSupport::Deprecation.warn(
117
- "`#quietly` is deprecated and will be removed in the next release."
118
- ) #not thread-safe
119
- silence_stream(STDOUT) do
120
- silence_stream(STDERR) do
121
- yield
122
- end
123
- end
124
- end
125
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,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,28 +1,9 @@
1
- class LoadError
2
- REGEXPS = [
3
- /^no such file to load -- (.+)$/i,
4
- /^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
5
- /^Missing API definition file in (.+)$/i,
6
- /^cannot load such file -- (.+)$/i,
7
- ]
8
-
9
- unless method_defined?(:path)
10
- # Returns the path which was unable to be loaded.
11
- def path
12
- @path ||= begin
13
- REGEXPS.find do |regex|
14
- message =~ regex
15
- end
16
- $1
17
- end
18
- end
19
- end
1
+ # frozen_string_literal: true
20
2
 
3
+ class LoadError
21
4
  # Returns true if the given path name (except perhaps for the ".rb"
22
5
  # extension) is the missing file which caused the exception to be raised.
23
6
  def is_missing?(location)
24
- location.sub(/\.rb$/, '') == path.sub(/\.rb$/, '')
7
+ location.sub(/\.rb$/, "".freeze) == path.sub(/\.rb$/, "".freeze)
25
8
  end
26
9
  end
27
-
28
- MissingSourceFile = LoadError
@@ -1,11 +1,11 @@
1
- require 'active_support/core_ext/module/aliasing'
1
+ # frozen_string_literal: true
2
2
 
3
- module Marshal
4
- class << self
5
- def load_with_autoloading(source, proc = nil)
6
- load_without_autoloading(source, proc)
3
+ module ActiveSupport
4
+ module MarshalWithAutoloading # :nodoc:
5
+ def load(source, proc = nil)
6
+ super(source, proc)
7
7
  rescue ArgumentError, NameError => exc
8
- if exc.message.match(%r|undefined class/module (.+?)(::)?\z|)
8
+ if exc.message.match(%r|undefined class/module (.+?)(?:::)?\z|)
9
9
  # try loading the class/module
10
10
  loaded = $1.constantize
11
11
 
@@ -18,7 +18,7 @@ module Marshal
18
18
  raise exc
19
19
  end
20
20
  end
21
-
22
- alias_method_chain :load, :autoloading
23
21
  end
24
22
  end
23
+
24
+ Marshal.singleton_class.prepend(ActiveSupport::MarshalWithAutoloading)
@@ -1,49 +1,8 @@
1
- class Module
2
- # Encapsulates the common pattern of:
3
- #
4
- # alias_method :foo_without_feature, :foo
5
- # alias_method :foo, :foo_with_feature
6
- #
7
- # With this, you simply do:
8
- #
9
- # alias_method_chain :foo, :feature
10
- #
11
- # And both aliases are set up for you.
12
- #
13
- # Query and bang methods (foo?, foo!) keep the same punctuation:
14
- #
15
- # alias_method_chain :foo?, :feature
16
- #
17
- # is equivalent to
18
- #
19
- # alias_method :foo_without_feature?, :foo?
20
- # alias_method :foo?, :foo_with_feature?
21
- #
22
- # so you can safely chain foo, foo?, foo! and/or foo= with the same feature.
23
- def alias_method_chain(target, feature)
24
- # Strip out punctuation on predicates, bang or writer methods since
25
- # e.g. target?_without_feature is not a valid method name.
26
- aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
27
- yield(aliased_target, punctuation) if block_given?
28
-
29
- with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
30
- without_method = "#{aliased_target}_without_#{feature}#{punctuation}"
31
-
32
- alias_method without_method, target
33
- alias_method target, with_method
34
-
35
- case
36
- when public_method_defined?(without_method)
37
- public target
38
- when protected_method_defined?(without_method)
39
- protected target
40
- when private_method_defined?(without_method)
41
- private target
42
- end
43
- end
1
+ # frozen_string_literal: true
44
2
 
3
+ class Module
45
4
  # Allows you to make aliases for attributes, which includes
46
- # getter, setter, and query methods.
5
+ # getter, setter, and a predicate.
47
6
  #
48
7
  # class Content < ActiveRecord::Base
49
8
  # # has a title attribute
@@ -60,6 +19,9 @@ class Module
60
19
  # e.subject = "Megastars"
61
20
  # e.title # => "Megastars"
62
21
  def alias_attribute(new_name, old_name)
22
+ # The following reader methods use an explicit `self` receiver in order to
23
+ # support aliases that start with an uppercase letter. Otherwise, they would
24
+ # be resolved as constants instead.
63
25
  module_eval <<-STR, __FILE__, __LINE__ + 1
64
26
  def #{new_name}; self.#{old_name}; end # def subject; self.title; end
65
27
  def #{new_name}?; self.#{old_name}?; end # def subject?; self.title?; end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Module
2
4
  # A module may or may not have a name.
3
5
  #
@@ -7,12 +9,21 @@ class Module
7
9
  # m = Module.new
8
10
  # m.name # => nil
9
11
  #
12
+ # +anonymous?+ method returns true if module does not have a name, false otherwise:
13
+ #
14
+ # Module.new.anonymous? # => true
15
+ #
16
+ # module M; end
17
+ # M.anonymous? # => false
18
+ #
10
19
  # A module gets a name when it is first assigned to a constant. Either
11
20
  # via the +module+ or +class+ keyword or by an explicit assignment:
12
21
  #
13
22
  # m = Module.new # creates an anonymous module
14
- # M = m # => m gets a name here as a side-effect
23
+ # m.anonymous? # => true
24
+ # M = m # m gets a name here as a side-effect
15
25
  # m.name # => "M"
26
+ # m.anonymous? # => false
16
27
  def anonymous?
17
28
  name.nil?
18
29
  end