activesupport 3.1.0 → 5.0.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 (276) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +798 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +13 -7
  5. data/lib/active_support/array_inquirer.rb +44 -0
  6. data/lib/active_support/backtrace_cleaner.rb +38 -34
  7. data/lib/active_support/benchmarkable.rb +17 -28
  8. data/lib/active_support/cache/file_store.rb +85 -70
  9. data/lib/active_support/cache/mem_cache_store.rb +75 -66
  10. data/lib/active_support/cache/memory_store.rb +31 -23
  11. data/lib/active_support/cache/null_store.rb +41 -0
  12. data/lib/active_support/cache/strategy/local_cache.rb +73 -70
  13. data/lib/active_support/cache/strategy/local_cache_middleware.rb +44 -0
  14. data/lib/active_support/cache.rb +360 -294
  15. data/lib/active_support/callbacks.rb +563 -393
  16. data/lib/active_support/concern.rb +42 -34
  17. data/lib/active_support/concurrency/latch.rb +19 -0
  18. data/lib/active_support/concurrency/share_lock.rb +186 -0
  19. data/lib/active_support/configurable.rb +70 -12
  20. data/lib/active_support/core_ext/array/access.rb +53 -9
  21. data/lib/active_support/core_ext/array/conversions.rb +109 -62
  22. data/lib/active_support/core_ext/array/extract_options.rb +2 -2
  23. data/lib/active_support/core_ext/array/grouping.rb +39 -32
  24. data/lib/active_support/core_ext/array/inquiry.rb +17 -0
  25. data/lib/active_support/core_ext/array/prepend_and_append.rb +7 -0
  26. data/lib/active_support/core_ext/array/wrap.rb +16 -18
  27. data/lib/active_support/core_ext/array.rb +2 -2
  28. data/lib/active_support/core_ext/benchmark.rb +7 -0
  29. data/lib/active_support/core_ext/big_decimal/conversions.rb +8 -36
  30. data/lib/active_support/core_ext/class/attribute.rb +47 -34
  31. data/lib/active_support/core_ext/class/attribute_accessors.rb +4 -79
  32. data/lib/active_support/core_ext/class/subclasses.rb +12 -7
  33. data/lib/active_support/core_ext/class.rb +0 -3
  34. data/lib/active_support/core_ext/date/blank.rb +12 -0
  35. data/lib/active_support/core_ext/date/calculations.rb +57 -167
  36. data/lib/active_support/core_ext/date/conversions.rb +31 -42
  37. data/lib/active_support/core_ext/date/zones.rb +2 -10
  38. data/lib/active_support/core_ext/date.rb +5 -0
  39. data/lib/active_support/core_ext/date_and_time/calculations.rb +335 -0
  40. data/lib/active_support/core_ext/date_and_time/compatibility.rb +18 -0
  41. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  42. data/lib/active_support/core_ext/date_time/acts_like.rb +1 -0
  43. data/lib/active_support/core_ext/date_time/blank.rb +12 -0
  44. data/lib/active_support/core_ext/date_time/calculations.rb +132 -65
  45. data/lib/active_support/core_ext/date_time/compatibility.rb +5 -0
  46. data/lib/active_support/core_ext/date_time/conversions.rb +36 -34
  47. data/lib/active_support/core_ext/date_time.rb +5 -0
  48. data/lib/active_support/core_ext/digest/uuid.rb +51 -0
  49. data/lib/active_support/core_ext/enumerable.rb +81 -74
  50. data/lib/active_support/core_ext/file/atomic.rb +53 -26
  51. data/lib/active_support/core_ext/file.rb +0 -1
  52. data/lib/active_support/core_ext/hash/compact.rb +20 -0
  53. data/lib/active_support/core_ext/hash/conversions.rb +175 -70
  54. data/lib/active_support/core_ext/hash/deep_merge.rb +30 -8
  55. data/lib/active_support/core_ext/hash/except.rb +11 -12
  56. data/lib/active_support/core_ext/hash/indifferent_access.rb +7 -8
  57. data/lib/active_support/core_ext/hash/keys.rb +147 -24
  58. data/lib/active_support/core_ext/hash/reverse_merge.rb +2 -3
  59. data/lib/active_support/core_ext/hash/slice.rb +22 -14
  60. data/lib/active_support/core_ext/hash/transform_values.rb +29 -0
  61. data/lib/active_support/core_ext/hash.rb +2 -2
  62. data/lib/active_support/core_ext/integer/inflections.rb +13 -1
  63. data/lib/active_support/core_ext/integer/multiple.rb +4 -0
  64. data/lib/active_support/core_ext/integer/time.rb +12 -22
  65. data/lib/active_support/core_ext/kernel/agnostics.rb +2 -2
  66. data/lib/active_support/core_ext/kernel/concern.rb +12 -0
  67. data/lib/active_support/core_ext/kernel/debugger.rb +2 -15
  68. data/lib/active_support/core_ext/kernel/reporting.rb +12 -62
  69. data/lib/active_support/core_ext/kernel/singleton_class.rb +0 -7
  70. data/lib/active_support/core_ext/kernel.rb +2 -3
  71. data/lib/active_support/core_ext/load_error.rb +14 -7
  72. data/lib/active_support/core_ext/marshal.rb +22 -0
  73. data/lib/active_support/core_ext/module/aliasing.rb +16 -12
  74. data/lib/active_support/core_ext/module/anonymous.rb +12 -8
  75. data/lib/active_support/core_ext/module/attr_internal.rb +2 -5
  76. data/lib/active_support/core_ext/module/attribute_accessors.rb +165 -13
  77. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +141 -0
  78. data/lib/active_support/core_ext/module/concerning.rb +135 -0
  79. data/lib/active_support/core_ext/module/delegation.rb +141 -68
  80. data/lib/active_support/core_ext/module/deprecation.rb +17 -3
  81. data/lib/active_support/core_ext/module/introspection.rb +9 -31
  82. data/lib/active_support/core_ext/module/method_transplanting.rb +3 -0
  83. data/lib/active_support/core_ext/module/qualified_const.rb +70 -0
  84. data/lib/active_support/core_ext/module/reachable.rb +1 -3
  85. data/lib/active_support/core_ext/module/remove_method.rb +24 -5
  86. data/lib/active_support/core_ext/module.rb +3 -3
  87. data/lib/active_support/core_ext/name_error.rb +15 -2
  88. data/lib/active_support/core_ext/numeric/bytes.rb +20 -0
  89. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  90. data/lib/active_support/core_ext/numeric/inquiry.rb +26 -0
  91. data/lib/active_support/core_ext/numeric/time.rb +31 -36
  92. data/lib/active_support/core_ext/numeric.rb +2 -0
  93. data/lib/active_support/core_ext/object/acts_like.rb +4 -4
  94. data/lib/active_support/core_ext/object/blank.rb +52 -18
  95. data/lib/active_support/core_ext/object/deep_dup.rb +53 -0
  96. data/lib/active_support/core_ext/object/duplicable.rb +12 -20
  97. data/lib/active_support/core_ext/object/inclusion.rb +13 -1
  98. data/lib/active_support/core_ext/object/instance_variables.rb +7 -12
  99. data/lib/active_support/core_ext/object/json.rb +205 -0
  100. data/lib/active_support/core_ext/object/to_param.rb +1 -55
  101. data/lib/active_support/core_ext/object/to_query.rb +66 -9
  102. data/lib/active_support/core_ext/object/try.rb +124 -33
  103. data/lib/active_support/core_ext/object/with_options.rb +37 -11
  104. data/lib/active_support/core_ext/object.rb +2 -1
  105. data/lib/active_support/core_ext/range/conversions.rb +17 -7
  106. data/lib/active_support/core_ext/range/each.rb +21 -0
  107. data/lib/active_support/core_ext/range/include_range.rb +20 -18
  108. data/lib/active_support/core_ext/range/overlaps.rb +1 -1
  109. data/lib/active_support/core_ext/range.rb +1 -2
  110. data/lib/active_support/core_ext/securerandom.rb +23 -0
  111. data/lib/active_support/core_ext/string/access.rb +95 -90
  112. data/lib/active_support/core_ext/string/behavior.rb +1 -1
  113. data/lib/active_support/core_ext/string/conversions.rb +41 -38
  114. data/lib/active_support/core_ext/string/exclude.rb +6 -1
  115. data/lib/active_support/core_ext/string/filters.rb +70 -17
  116. data/lib/active_support/core_ext/string/indent.rb +43 -0
  117. data/lib/active_support/core_ext/string/inflections.rb +139 -59
  118. data/lib/active_support/core_ext/string/inquiry.rb +2 -2
  119. data/lib/active_support/core_ext/string/multibyte.rb +46 -65
  120. data/lib/active_support/core_ext/string/output_safety.rb +153 -56
  121. data/lib/active_support/core_ext/string/strip.rb +3 -6
  122. data/lib/active_support/core_ext/string/zones.rb +14 -0
  123. data/lib/active_support/core_ext/string.rb +2 -3
  124. data/lib/active_support/core_ext/struct.rb +3 -0
  125. data/lib/active_support/core_ext/time/calculations.rb +173 -173
  126. data/lib/active_support/core_ext/time/compatibility.rb +5 -0
  127. data/lib/active_support/core_ext/time/conversions.rb +33 -29
  128. data/lib/active_support/core_ext/time/marshal.rb +2 -56
  129. data/lib/active_support/core_ext/time/zones.rb +57 -32
  130. data/lib/active_support/core_ext/time.rb +5 -0
  131. data/lib/active_support/core_ext/uri.rb +13 -19
  132. data/lib/active_support/core_ext.rb +3 -2
  133. data/lib/active_support/dependencies/autoload.rb +47 -20
  134. data/lib/active_support/dependencies/interlock.rb +51 -0
  135. data/lib/active_support/dependencies.rb +315 -265
  136. data/lib/active_support/deprecation/behaviors.rb +71 -30
  137. data/lib/active_support/deprecation/instance_delegator.rb +24 -0
  138. data/lib/active_support/deprecation/method_wrappers.rb +59 -18
  139. data/lib/active_support/deprecation/proxy_wrappers.rb +82 -14
  140. data/lib/active_support/deprecation/reporting.rb +61 -14
  141. data/lib/active_support/deprecation.rb +38 -13
  142. data/lib/active_support/descendants_tracker.rb +34 -19
  143. data/lib/active_support/duration/iso8601_parser.rb +122 -0
  144. data/lib/active_support/duration/iso8601_serializer.rb +51 -0
  145. data/lib/active_support/duration.rb +85 -14
  146. data/lib/active_support/evented_file_update_checker.rb +194 -0
  147. data/lib/active_support/execution_wrapper.rb +117 -0
  148. data/lib/active_support/executor.rb +6 -0
  149. data/lib/active_support/file_update_checker.rb +138 -17
  150. data/lib/active_support/gem_version.rb +15 -0
  151. data/lib/active_support/gzip.rb +11 -5
  152. data/lib/active_support/hash_with_indifferent_access.rb +199 -49
  153. data/lib/active_support/i18n.rb +6 -2
  154. data/lib/active_support/i18n_railtie.rb +40 -21
  155. data/lib/active_support/inflections.rb +22 -13
  156. data/lib/active_support/inflector/inflections.rb +175 -144
  157. data/lib/active_support/inflector/methods.rb +328 -91
  158. data/lib/active_support/inflector/transliterate.rb +51 -37
  159. data/lib/active_support/json/decoding.rb +31 -22
  160. data/lib/active_support/json/encoding.rb +88 -248
  161. data/lib/active_support/key_generator.rb +71 -0
  162. data/lib/active_support/lazy_load_hooks.rb +27 -25
  163. data/lib/active_support/locale/en.yml +102 -3
  164. data/lib/active_support/log_subscriber/test_helper.rb +24 -21
  165. data/lib/active_support/log_subscriber.rb +36 -49
  166. data/lib/active_support/logger.rb +106 -0
  167. data/lib/active_support/logger_silence.rb +28 -0
  168. data/lib/active_support/logger_thread_safe_level.rb +31 -0
  169. data/lib/active_support/message_encryptor.rb +72 -36
  170. data/lib/active_support/message_verifier.rb +96 -24
  171. data/lib/active_support/multibyte/chars.rb +88 -333
  172. data/lib/active_support/multibyte/unicode.rb +156 -136
  173. data/lib/active_support/multibyte.rb +5 -28
  174. data/lib/active_support/notifications/fanout.rb +115 -19
  175. data/lib/active_support/notifications/instrumenter.rb +52 -15
  176. data/lib/active_support/notifications.rb +168 -33
  177. data/lib/active_support/number_helper/number_converter.rb +182 -0
  178. data/lib/active_support/number_helper/number_to_currency_converter.rb +44 -0
  179. data/lib/active_support/number_helper/number_to_delimited_converter.rb +28 -0
  180. data/lib/active_support/number_helper/number_to_human_converter.rb +68 -0
  181. data/lib/active_support/number_helper/number_to_human_size_converter.rb +62 -0
  182. data/lib/active_support/number_helper/number_to_percentage_converter.rb +12 -0
  183. data/lib/active_support/number_helper/number_to_phone_converter.rb +58 -0
  184. data/lib/active_support/number_helper/number_to_rounded_converter.rb +92 -0
  185. data/lib/active_support/number_helper.rb +368 -0
  186. data/lib/active_support/option_merger.rb +1 -1
  187. data/lib/active_support/ordered_hash.rb +18 -183
  188. data/lib/active_support/ordered_options.rb +44 -24
  189. data/lib/active_support/per_thread_registry.rb +58 -0
  190. data/lib/active_support/proxy_object.rb +13 -0
  191. data/lib/active_support/rails.rb +27 -0
  192. data/lib/active_support/railtie.rb +25 -34
  193. data/lib/active_support/reloader.rb +129 -0
  194. data/lib/active_support/rescuable.rb +98 -48
  195. data/lib/active_support/security_utils.rb +27 -0
  196. data/lib/active_support/string_inquirer.rb +14 -9
  197. data/lib/active_support/subscriber.rb +120 -0
  198. data/lib/active_support/tagged_logging.rb +78 -0
  199. data/lib/active_support/test_case.rb +69 -17
  200. data/lib/active_support/testing/assertions.rb +43 -41
  201. data/lib/active_support/testing/autorun.rb +12 -0
  202. data/lib/active_support/testing/constant_lookup.rb +50 -0
  203. data/lib/active_support/testing/declarative.rb +7 -21
  204. data/lib/active_support/testing/deprecation.rb +14 -33
  205. data/lib/active_support/testing/file_fixtures.rb +34 -0
  206. data/lib/active_support/testing/isolation.rb +53 -95
  207. data/lib/active_support/testing/method_call_assertions.rb +41 -0
  208. data/lib/active_support/testing/setup_and_teardown.rb +21 -82
  209. data/lib/active_support/testing/stream.rb +42 -0
  210. data/lib/active_support/testing/tagged_logging.rb +25 -0
  211. data/lib/active_support/testing/time_helpers.rb +134 -0
  212. data/lib/active_support/time.rb +6 -23
  213. data/lib/active_support/time_with_zone.rb +239 -92
  214. data/lib/active_support/values/time_zone.rb +236 -160
  215. data/lib/active_support/values/unicode_tables.dat +0 -0
  216. data/lib/active_support/version.rb +5 -7
  217. data/lib/active_support/xml_mini/jdom.rb +19 -13
  218. data/lib/active_support/xml_mini/libxml.rb +3 -4
  219. data/lib/active_support/xml_mini/libxmlsax.rb +2 -3
  220. data/lib/active_support/xml_mini/nokogiri.rb +3 -4
  221. data/lib/active_support/xml_mini/nokogirisax.rb +2 -3
  222. data/lib/active_support/xml_mini/rexml.rb +8 -10
  223. data/lib/active_support/xml_mini.rb +66 -34
  224. data/lib/active_support.rb +40 -23
  225. metadata +185 -134
  226. data/CHANGELOG +0 -1534
  227. data/lib/active_support/base64.rb +0 -42
  228. data/lib/active_support/basic_object.rb +0 -21
  229. data/lib/active_support/buffered_logger.rb +0 -137
  230. data/lib/active_support/cache/compressed_mem_cache_store.rb +0 -13
  231. data/lib/active_support/cache/synchronized_memory_store.rb +0 -11
  232. data/lib/active_support/core_ext/array/random_access.rb +0 -30
  233. data/lib/active_support/core_ext/array/uniq_by.rb +0 -16
  234. data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -44
  235. data/lib/active_support/core_ext/class/inheritable_attributes.rb +0 -178
  236. data/lib/active_support/core_ext/date/freeze.rb +0 -31
  237. data/lib/active_support/core_ext/date_time/zones.rb +0 -21
  238. data/lib/active_support/core_ext/exception.rb +0 -3
  239. data/lib/active_support/core_ext/file/path.rb +0 -5
  240. data/lib/active_support/core_ext/float/rounding.rb +0 -19
  241. data/lib/active_support/core_ext/float.rb +0 -1
  242. data/lib/active_support/core_ext/hash/deep_dup.rb +0 -11
  243. data/lib/active_support/core_ext/hash/diff.rb +0 -13
  244. data/lib/active_support/core_ext/kernel/requires.rb +0 -28
  245. data/lib/active_support/core_ext/logger.rb +0 -81
  246. data/lib/active_support/core_ext/module/attr_accessor_with_default.rb +0 -31
  247. data/lib/active_support/core_ext/module/method_names.rb +0 -14
  248. data/lib/active_support/core_ext/module/synchronization.rb +0 -43
  249. data/lib/active_support/core_ext/object/to_json.rb +0 -19
  250. data/lib/active_support/core_ext/proc.rb +0 -14
  251. data/lib/active_support/core_ext/process/daemon.rb +0 -23
  252. data/lib/active_support/core_ext/process.rb +0 -1
  253. data/lib/active_support/core_ext/range/blockless_step.rb +0 -29
  254. data/lib/active_support/core_ext/range/cover.rb +0 -3
  255. data/lib/active_support/core_ext/rexml.rb +0 -46
  256. data/lib/active_support/core_ext/string/encoding.rb +0 -11
  257. data/lib/active_support/core_ext/string/interpolation.rb +0 -2
  258. data/lib/active_support/core_ext/string/xchar.rb +0 -18
  259. data/lib/active_support/core_ext/time/publicize_conversion_methods.rb +0 -10
  260. data/lib/active_support/file_watcher.rb +0 -36
  261. data/lib/active_support/json/variable.rb +0 -9
  262. data/lib/active_support/memoizable.rb +0 -105
  263. data/lib/active_support/multibyte/exceptions.rb +0 -8
  264. data/lib/active_support/multibyte/utils.rb +0 -60
  265. data/lib/active_support/ruby/shim.rb +0 -22
  266. data/lib/active_support/secure_random.rb +0 -6
  267. data/lib/active_support/testing/mochaing.rb +0 -7
  268. data/lib/active_support/testing/pending.rb +0 -52
  269. data/lib/active_support/testing/performance/jruby.rb +0 -115
  270. data/lib/active_support/testing/performance/rubinius.rb +0 -113
  271. data/lib/active_support/testing/performance/ruby/mri.rb +0 -57
  272. data/lib/active_support/testing/performance/ruby/yarv.rb +0 -57
  273. data/lib/active_support/testing/performance/ruby.rb +0 -152
  274. data/lib/active_support/testing/performance.rb +0 -317
  275. data/lib/active_support/time/autoload.rb +0 -5
  276. data/lib/active_support/whiny_nil.rb +0 -60
@@ -1,9 +1,15 @@
1
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:
2
+ # Slices a hash to include only the given keys. Returns a hash containing
3
+ # the given keys.
4
+ #
5
+ # { a: 1, b: 2, c: 3, d: 4 }.slice(:a, :b)
6
+ # # => {:a=>1, :b=>2}
7
+ #
8
+ # This is useful for limiting an options hash to valid keys before
9
+ # passing to a method:
4
10
  #
5
11
  # def search(criteria = {})
6
- # assert_valid_keys(:mass, :velocity, :time)
12
+ # criteria.assert_valid_keys(:mass, :velocity, :time)
7
13
  # end
8
14
  #
9
15
  # search(options.slice(:mass, :velocity, :time))
@@ -13,28 +19,30 @@ class Hash
13
19
  # valid_keys = [:mass, :velocity, :time]
14
20
  # search(options.slice(*valid_keys))
15
21
  def slice(*keys)
16
- keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
17
- hash = self.class.new
18
- keys.each { |k| hash[k] = self[k] if has_key?(k) }
19
- hash
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) }
20
24
  end
21
25
 
22
26
  # Replaces the hash with only the given keys.
23
- # Returns a hash contained the removed key/value pairs
24
- # {:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d => 4}
27
+ # Returns a hash containing the removed key/value pairs.
28
+ #
29
+ # { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
30
+ # # => {:c=>3, :d=>4}
25
31
  def slice!(*keys)
26
- keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
32
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
27
33
  omit = slice(*self.keys - keys)
28
34
  hash = slice(*keys)
35
+ hash.default = default
36
+ hash.default_proc = default_proc if default_proc
29
37
  replace(hash)
30
38
  omit
31
39
  end
32
40
 
33
41
  # Removes and returns the key/value pairs matching the given keys.
34
- # {:a => 1, :b => 2, :c => 3, :d => 4}.extract!(:a, :b) # => {:a => 1, :b => 2}
42
+ #
43
+ # { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
44
+ # { a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
35
45
  def extract!(*keys)
36
- result = {}
37
- keys.each {|key| result[key] = delete(key) }
38
- result
46
+ keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
39
47
  end
40
48
  end
@@ -0,0 +1,29 @@
1
+ class Hash
2
+ # Returns a new hash with the results of running +block+ once for every value.
3
+ # The keys are unchanged.
4
+ #
5
+ # { a: 1, b: 2, c: 3 }.transform_values { |x| x * 2 } # => { a: 2, b: 4, c: 6 }
6
+ #
7
+ # If you do not provide a +block+, it will return an Enumerator
8
+ # for chaining with other methods:
9
+ #
10
+ # { a: 1, b: 2 }.transform_values.with_index { |v, i| [v, i].join.to_i } # => { a: 10, b: 21 }
11
+ def transform_values
12
+ return enum_for(:transform_values) { size } unless block_given?
13
+ return {} if empty?
14
+ result = self.class.new
15
+ each do |key, value|
16
+ result[key] = yield(value)
17
+ end
18
+ result
19
+ end
20
+
21
+ # Destructively converts all values using the +block+ operations.
22
+ # Same as +transform_values+ but modifies +self+.
23
+ def transform_values!
24
+ return enum_for(:transform_values!) { size } unless block_given?
25
+ each do |key, value|
26
+ self[key] = yield(value)
27
+ end
28
+ end
29
+ end
@@ -1,9 +1,9 @@
1
+ require 'active_support/core_ext/hash/compact'
1
2
  require 'active_support/core_ext/hash/conversions'
2
3
  require 'active_support/core_ext/hash/deep_merge'
3
- require 'active_support/core_ext/hash/deep_dup'
4
- require 'active_support/core_ext/hash/diff'
5
4
  require 'active_support/core_ext/hash/except'
6
5
  require 'active_support/core_ext/hash/indifferent_access'
7
6
  require 'active_support/core_ext/hash/keys'
8
7
  require 'active_support/core_ext/hash/reverse_merge'
9
8
  require 'active_support/core_ext/hash/slice'
9
+ require 'active_support/core_ext/hash/transform_values'
@@ -10,8 +10,20 @@ class Integer
10
10
  # 1003.ordinalize # => "1003rd"
11
11
  # -11.ordinalize # => "-11th"
12
12
  # -1001.ordinalize # => "-1001st"
13
- #
14
13
  def ordinalize
15
14
  ActiveSupport::Inflector.ordinalize(self)
16
15
  end
16
+
17
+ # Ordinal returns the suffix used to denote the position
18
+ # in an ordered sequence such as 1st, 2nd, 3rd, 4th.
19
+ #
20
+ # 1.ordinal # => "st"
21
+ # 2.ordinal # => "nd"
22
+ # 1002.ordinal # => "nd"
23
+ # 1003.ordinal # => "rd"
24
+ # -11.ordinal # => "th"
25
+ # -1001.ordinal # => "st"
26
+ def ordinal
27
+ ActiveSupport::Inflector.ordinal(self)
28
+ end
17
29
  end
@@ -1,5 +1,9 @@
1
1
  class Integer
2
2
  # Check whether the integer is evenly divisible by the argument.
3
+ #
4
+ # 0.multiple_of?(0) # => true
5
+ # 6.multiple_of?(5) # => false
6
+ # 10.multiple_of?(2) # => true
3
7
  def multiple_of?(number)
4
8
  number != 0 ? self % number == 0 : zero?
5
9
  end
@@ -1,39 +1,29 @@
1
+ require 'active_support/duration'
2
+ require 'active_support/core_ext/numeric/time'
3
+
1
4
  class Integer
2
- # Enables the use of time calculations and declarations, like 45.minutes + 2.hours + 4.years.
5
+ # Enables the use of time calculations and declarations, like <tt>45.minutes +
6
+ # 2.hours + 4.years</tt>.
3
7
  #
4
- # These methods use Time#advance for precise date calculations when using from_now, ago, etc.
5
- # as well as adding or subtracting their results from a Time object. For example:
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.
6
11
  #
7
- # # equivalent to Time.now.advance(:months => 1)
12
+ # # equivalent to Time.now.advance(months: 1)
8
13
  # 1.month.from_now
9
14
  #
10
- # # equivalent to Time.now.advance(:years => 2)
15
+ # # equivalent to Time.now.advance(years: 2)
11
16
  # 2.years.from_now
12
17
  #
13
- # # equivalent to Time.now.advance(:months => 4, :years => 5)
18
+ # # equivalent to Time.now.advance(months: 4, years: 5)
14
19
  # (4.months + 5.years).from_now
15
- #
16
- # While these methods provide precise calculation when used as in the examples above, care
17
- # should be taken to note that this is not true if the result of `months', `years', etc is
18
- # converted before use:
19
- #
20
- # # equivalent to 30.days.to_i.from_now
21
- # 1.month.to_i.from_now
22
- #
23
- # # equivalent to 365.25.days.to_f.from_now
24
- # 1.year.to_f.from_now
25
- #
26
- # In such cases, Ruby's core
27
- # Date[http://stdlib.rubyonrails.org/libdoc/date/rdoc/index.html] and
28
- # Time[http://stdlib.rubyonrails.org/libdoc/time/rdoc/index.html] should be used for precision
29
- # date and time arithmetic
30
20
  def months
31
21
  ActiveSupport::Duration.new(self * 30.days, [[:months, self]])
32
22
  end
33
23
  alias :month :months
34
24
 
35
25
  def years
36
- ActiveSupport::Duration.new(self * 365.25.days, [[:years, self]])
26
+ ActiveSupport::Duration.new(self * 365.25.days.to_i, [[:years, self]])
37
27
  end
38
28
  alias :year :years
39
29
  end
@@ -1,11 +1,11 @@
1
1
  class Object
2
2
  # Makes backticks behave (somewhat more) similarly on all platforms.
3
3
  # On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the
4
- # spawned shell prints a message to stderr and sets $?. We emulate
4
+ # spawned shell prints a message to stderr and sets $?. We emulate
5
5
  # Unix on the former but not the latter.
6
6
  def `(command) #:nodoc:
7
7
  super
8
8
  rescue Errno::ENOENT => e
9
9
  STDERR.puts "#$0: #{e}"
10
10
  end
11
- end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'active_support/core_ext/module/concerning'
2
+
3
+ module Kernel
4
+ module_function
5
+
6
+ # A shortcut to define a toplevel concern, not within a module.
7
+ #
8
+ # See Module::Concerning for more.
9
+ def concern(topic, &module_definition)
10
+ Object.concern topic, &module_definition
11
+ end
12
+ end
@@ -1,16 +1,3 @@
1
- module Kernel
2
- unless respond_to?(:debugger)
3
- # Starts a debugging session if ruby-debug has been loaded (call rails server --debugger to do load it).
4
- def debugger
5
- message = "\n***** Debugger requested, but was not available (ensure ruby-debug is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n"
6
- defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message)
7
- end
8
- end
1
+ require 'active_support/deprecation'
9
2
 
10
- undef :breakpoint if respond_to?(:breakpoint)
11
- def breakpoint
12
- message = "\n***** The 'breakpoint' command has been renamed 'debugger' -- please change *****\n"
13
- defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message)
14
- debugger
15
- end
16
- end
3
+ ActiveSupport::Deprecation.warn("This file is deprecated and will be removed in Rails 5.1 with no replacement.")
@@ -1,6 +1,8 @@
1
- require 'rbconfig'
2
1
  module Kernel
3
- # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
2
+ module_function
3
+
4
+ # Sets $VERBOSE to nil for the duration of the block and back to its original
5
+ # value afterwards.
4
6
  #
5
7
  # silence_warnings do
6
8
  # value = noisy_call # no warning voiced
@@ -11,12 +13,14 @@ module Kernel
11
13
  with_warnings(nil) { yield }
12
14
  end
13
15
 
14
- # Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
16
+ # Sets $VERBOSE to +true+ for the duration of the block and back to its
17
+ # original value afterwards.
15
18
  def enable_warnings
16
19
  with_warnings(true) { yield }
17
20
  end
18
21
 
19
- # Sets $VERBOSE for the duration of the block and back to its original value afterwards.
22
+ # Sets $VERBOSE for the duration of the block and back to its original
23
+ # value afterwards.
20
24
  def with_warnings(flag)
21
25
  old_verbose, $VERBOSE = $VERBOSE, flag
22
26
  yield
@@ -24,70 +28,16 @@ module Kernel
24
28
  $VERBOSE = old_verbose
25
29
  end
26
30
 
27
- # For compatibility
28
- def silence_stderr #:nodoc:
29
- silence_stream(STDERR) { yield }
30
- end
31
-
32
- # Silences any stream for the duration of the block.
33
- #
34
- # silence_stream(STDOUT) do
35
- # puts 'This will never be seen'
36
- # end
37
- #
38
- # puts 'But this will'
39
- def silence_stream(stream)
40
- old_stream = stream.dup
41
- stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
42
- stream.sync = true
43
- yield
44
- ensure
45
- stream.reopen(old_stream)
46
- end
47
-
48
31
  # Blocks and ignores any exception passed as argument if raised within the block.
49
32
  #
50
33
  # suppress(ZeroDivisionError) do
51
34
  # 1/0
52
- # puts "This code is NOT reached"
35
+ # puts 'This code is NOT reached'
53
36
  # end
54
37
  #
55
- # puts "This code gets executed and nothing related to ZeroDivisionError was seen"
38
+ # puts 'This code gets executed and nothing related to ZeroDivisionError was seen'
56
39
  def suppress(*exception_classes)
57
- begin yield
58
- rescue Exception => e
59
- raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
60
- end
61
- end
62
-
63
- # Captures the given stream and returns it:
64
- #
65
- # stream = capture(:stdout) { puts "Cool" }
66
- # stream # => "Cool\n"
67
- #
68
- def capture(stream)
69
- begin
70
- stream = stream.to_s
71
- eval "$#{stream} = StringIO.new"
72
- yield
73
- result = eval("$#{stream}").string
74
- ensure
75
- eval("$#{stream} = #{stream.upcase}")
76
- end
77
-
78
- result
79
- end
80
- alias :silence :capture
81
-
82
- # Silences both STDOUT and STDERR, even for subprocesses.
83
- #
84
- # quietly { system 'bundle install' }
85
- #
86
- def quietly
87
- silence_stream(STDOUT) do
88
- silence_stream(STDERR) do
89
- yield
90
- end
91
- end
40
+ yield
41
+ rescue *exception_classes
92
42
  end
93
43
  end
@@ -1,11 +1,4 @@
1
1
  module Kernel
2
- # Returns the object's singleton class.
3
- def singleton_class
4
- class << self
5
- self
6
- end
7
- end unless respond_to?(:singleton_class) # exists in 1.9.2
8
-
9
2
  # class_eval on an object acts like singleton_class.class_eval.
10
3
  def class_eval(*args, &block)
11
4
  singleton_class.class_eval(*args, &block)
@@ -1,5 +1,4 @@
1
- require 'active_support/core_ext/kernel/reporting'
2
1
  require 'active_support/core_ext/kernel/agnostics'
3
- require 'active_support/core_ext/kernel/requires'
4
- require 'active_support/core_ext/kernel/debugger'
2
+ require 'active_support/core_ext/kernel/concern'
3
+ require 'active_support/core_ext/kernel/reporting'
5
4
  require 'active_support/core_ext/kernel/singleton_class'
@@ -1,3 +1,5 @@
1
+ require 'active_support/deprecation/proxy_wrappers'
2
+
1
3
  class LoadError
2
4
  REGEXPS = [
3
5
  /^no such file to load -- (.+)$/i,
@@ -6,18 +8,23 @@ class LoadError
6
8
  /^cannot load such file -- (.+)$/i,
7
9
  ]
8
10
 
9
- def path
10
- @path ||= begin
11
- REGEXPS.find do |regex|
12
- message =~ regex
11
+ unless method_defined?(:path)
12
+ # Returns the path which was unable to be loaded.
13
+ def path
14
+ @path ||= begin
15
+ REGEXPS.find do |regex|
16
+ message =~ regex
17
+ end
18
+ $1
13
19
  end
14
- $1
15
20
  end
16
21
  end
17
22
 
23
+ # Returns true if the given path name (except perhaps for the ".rb"
24
+ # extension) is the missing file which caused the exception to be raised.
18
25
  def is_missing?(location)
19
- location.sub(/\.rb$/, '') == path.sub(/\.rb$/, '')
26
+ location.sub(/\.rb$/, ''.freeze) == path.sub(/\.rb$/, ''.freeze)
20
27
  end
21
28
  end
22
29
 
23
- MissingSourceFile = LoadError
30
+ MissingSourceFile = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('MissingSourceFile', 'LoadError')
@@ -0,0 +1,22 @@
1
+ module ActiveSupport
2
+ module MarshalWithAutoloading # :nodoc:
3
+ def load(source)
4
+ super(source)
5
+ rescue ArgumentError, NameError => exc
6
+ if exc.message.match(%r|undefined class/module (.+?)(?:::)?\z|)
7
+ # try loading the class/module
8
+ loaded = $1.constantize
9
+
10
+ raise unless $1 == loaded.name
11
+
12
+ # if it is an IO we need to go back to read the object
13
+ source.rewind if source.respond_to?(:rewind)
14
+ retry
15
+ else
16
+ raise exc
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Marshal.singleton_class.prepend(ActiveSupport::MarshalWithAutoloading)
@@ -1,4 +1,7 @@
1
1
  class Module
2
+ # NOTE: This method is deprecated. Please use <tt>Module#prepend</tt> that
3
+ # comes with Ruby 2.0 or newer instead.
4
+ #
2
5
  # Encapsulates the common pattern of:
3
6
  #
4
7
  # alias_method :foo_without_feature, :foo
@@ -19,32 +22,33 @@ class Module
19
22
  # alias_method :foo_without_feature?, :foo?
20
23
  # alias_method :foo?, :foo_with_feature?
21
24
  #
22
- # so you can safely chain foo, foo?, and foo! with the same feature.
25
+ # so you can safely chain foo, foo?, foo! and/or foo= with the same feature.
23
26
  def alias_method_chain(target, feature)
24
- # Strip out punctuation on predicates or bang methods since
27
+ ActiveSupport::Deprecation.warn("alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super.")
28
+
29
+ # Strip out punctuation on predicates, bang or writer methods since
25
30
  # e.g. target?_without_feature is not a valid method name.
26
31
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
27
32
  yield(aliased_target, punctuation) if block_given?
28
33
 
29
- with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
34
+ with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
35
+ without_method = "#{aliased_target}_without_#{feature}#{punctuation}"
30
36
 
31
37
  alias_method without_method, target
32
38
  alias_method target, with_method
33
39
 
34
40
  case
35
- when public_method_defined?(without_method)
36
- public target
37
- when protected_method_defined?(without_method)
38
- protected target
39
- when private_method_defined?(without_method)
40
- private target
41
+ when public_method_defined?(without_method)
42
+ public target
43
+ when protected_method_defined?(without_method)
44
+ protected target
45
+ when private_method_defined?(without_method)
46
+ private target
41
47
  end
42
48
  end
43
49
 
44
50
  # Allows you to make aliases for attributes, which includes
45
- # getter, setter, and query methods.
46
- #
47
- # Example:
51
+ # getter, setter, and a predicate.
48
52
  #
49
53
  # class Content < ActiveRecord::Base
50
54
  # # has a title attribute
@@ -1,5 +1,3 @@
1
- require 'active_support/core_ext/object/blank'
2
-
3
1
  class Module
4
2
  # A module may or may not have a name.
5
3
  #
@@ -7,18 +5,24 @@ class Module
7
5
  # M.name # => "M"
8
6
  #
9
7
  # m = Module.new
10
- # m.name # => ""
8
+ # m.name # => nil
9
+ #
10
+ # +anonymous?+ method returns true if module does not have a name, false otherwise:
11
+ #
12
+ # Module.new.anonymous? # => true
13
+ #
14
+ # module M; end
15
+ # M.anonymous? # => false
11
16
  #
12
17
  # A module gets a name when it is first assigned to a constant. Either
13
18
  # via the +module+ or +class+ keyword or by an explicit assignment:
14
19
  #
15
20
  # m = Module.new # creates an anonymous module
16
- # M = m # => m gets a name here as a side-effect
21
+ # m.anonymous? # => true
22
+ # M = m # m gets a name here as a side-effect
17
23
  # m.name # => "M"
18
- #
24
+ # m.anonymous? # => false
19
25
  def anonymous?
20
- # Uses blank? because the name of an anonymous class is an empty
21
- # string in 1.8, and nil in 1.9.
22
- name.blank?
26
+ name.nil?
23
27
  end
24
28
  end
@@ -15,7 +15,6 @@ class Module
15
15
  attr_internal_reader(*attrs)
16
16
  attr_internal_writer(*attrs)
17
17
  end
18
-
19
18
  alias_method :attr_internal, :attr_internal_accessor
20
19
 
21
20
  class << self; attr_accessor :attr_internal_naming_format end
@@ -28,10 +27,8 @@ class Module
28
27
 
29
28
  def attr_internal_define(attr_name, type)
30
29
  internal_name = attr_internal_ivar_name(attr_name).sub(/\A@/, '')
31
- class_eval do # class_eval is necessary on 1.9 or else the methods a made private
32
- # use native attr_* methods as they are faster on some Ruby implementations
33
- send("attr_#{type}", internal_name)
34
- end
30
+ # use native attr_* methods as they are faster on some Ruby implementations
31
+ send("attr_#{type}", internal_name)
35
32
  attr_name, internal_name = "#{attr_name}=", "#{internal_name}=" if type == :writer
36
33
  alias_method attr_name, internal_name
37
34
  remove_method internal_name