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,8 +1,6 @@
1
1
  require 'active_support/concern'
2
2
  require 'active_support/core_ext/class/attribute'
3
- require 'active_support/core_ext/proc'
4
3
  require 'active_support/core_ext/string/inflections'
5
- require 'active_support/core_ext/array/extract_options'
6
4
 
7
5
  module ActiveSupport
8
6
  # Rescuable module adds support for easier exception handling.
@@ -31,11 +29,11 @@ module ActiveSupport
31
29
  # any.
32
30
  #
33
31
  # class ApplicationController < ActionController::Base
34
- # rescue_from User::NotAuthorized, :with => :deny_access # self defined exception
35
- # rescue_from ActiveRecord::RecordInvalid, :with => :show_errors
32
+ # rescue_from User::NotAuthorized, with: :deny_access # self defined exception
33
+ # rescue_from ActiveRecord::RecordInvalid, with: :show_errors
36
34
  #
37
35
  # rescue_from 'MyAppError::Base' do |exception|
38
- # render :xml => exception, :status => 500
36
+ # render xml: exception, status: 500
39
37
  # end
40
38
  #
41
39
  # protected
@@ -48,68 +46,120 @@ module ActiveSupport
48
46
  # end
49
47
  # end
50
48
  #
51
- def rescue_from(*klasses, &block)
52
- options = klasses.extract_options!
53
-
54
- unless options.has_key?(:with)
49
+ # Exceptions raised inside exception handlers are not propagated up.
50
+ def rescue_from(*klasses, with: nil, &block)
51
+ unless with
55
52
  if block_given?
56
- options[:with] = block
53
+ with = block
57
54
  else
58
- raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument."
55
+ raise ArgumentError, 'Need a handler. Pass the with: keyword argument or provide a block.'
59
56
  end
60
57
  end
61
58
 
62
59
  klasses.each do |klass|
63
- key = if klass.is_a?(Class) && klass <= Exception
60
+ key = if klass.is_a?(Module) && klass.respond_to?(:===)
64
61
  klass.name
65
62
  elsif klass.is_a?(String)
66
63
  klass
67
64
  else
68
- raise ArgumentError, "#{klass} is neither an Exception nor a String"
65
+ raise ArgumentError, "#{klass.inspect} must be an Exception class or a String referencing an Exception class"
69
66
  end
70
67
 
71
- # put the new handler at the end because the list is read in reverse
72
- self.rescue_handlers += [[key, options[:with]]]
68
+ # Put the new handler at the end because the list is read in reverse.
69
+ self.rescue_handlers += [[key, with]]
73
70
  end
74
71
  end
75
- end
76
72
 
77
- # Tries to rescue the exception by looking up and calling a registered handler.
78
- def rescue_with_handler(exception)
79
- if handler = handler_for_rescue(exception)
80
- handler.arity != 0 ? handler.call(exception) : handler.call
81
- true # don't rely on the return value of the handler
73
+ # Matches an exception to a handler based on the exception class.
74
+ #
75
+ # If no handler matches the exception, check for a handler matching the
76
+ # (optional) exception.cause. If no handler matches the exception or its
77
+ # cause, this returns nil so you can deal with unhandled exceptions.
78
+ # Be sure to re-raise unhandled exceptions if this is what you expect.
79
+ #
80
+ # begin
81
+ # …
82
+ # rescue => exception
83
+ # rescue_with_handler(exception) || raise
84
+ # end
85
+ #
86
+ # Returns the exception if it was handled and nil if it was not.
87
+ def rescue_with_handler(exception, object: self)
88
+ if handler = handler_for_rescue(exception, object: object)
89
+ handler.call exception
90
+ exception
91
+ end
82
92
  end
83
- end
84
93
 
85
- def handler_for_rescue(exception)
86
- # We go from right to left because pairs are pushed onto rescue_handlers
87
- # as rescue_from declarations are found.
88
- _, rescuer = self.class.rescue_handlers.reverse.detect do |klass_name, handler|
89
- # The purpose of allowing strings in rescue_from is to support the
90
- # declaration of handler associations for exception classes whose
91
- # definition is yet unknown.
92
- #
93
- # Since this loop needs the constants it would be inconsistent to
94
- # assume they should exist at this point. An early raised exception
95
- # could trigger some other handler and the array could include
96
- # precisely a string whose corresponding constant has not yet been
97
- # seen. This is why we are tolerant to unknown constants.
98
- #
99
- # Note that this tolerance only matters if the exception was given as
100
- # a string, otherwise a NameError will be raised by the interpreter
101
- # itself when rescue_from CONSTANT is executed.
102
- klass = self.class.const_get(klass_name) rescue nil
103
- klass ||= klass_name.constantize rescue nil
104
- exception.is_a?(klass) if klass
94
+ def handler_for_rescue(exception, object: self) #:nodoc:
95
+ case rescuer = find_rescue_handler(exception)
96
+ when Symbol
97
+ method = object.method(rescuer)
98
+ if method.arity == 0
99
+ -> e { method.call }
100
+ else
101
+ method
102
+ end
103
+ when Proc
104
+ if rescuer.arity == 0
105
+ -> e { object.instance_exec(&rescuer) }
106
+ else
107
+ -> e { object.instance_exec(e, &rescuer) }
108
+ end
109
+ end
105
110
  end
106
111
 
107
- case rescuer
108
- when Symbol
109
- method(rescuer)
110
- when Proc
111
- rescuer.bind(self)
112
- end
112
+ private
113
+ def find_rescue_handler(exception)
114
+ if exception
115
+ # Handlers are in order of declaration but the most recently declared
116
+ # is the highest priority match, so we search for matching handlers
117
+ # in reverse.
118
+ _, handler = rescue_handlers.reverse_each.detect do |class_or_name, _|
119
+ if klass = constantize_rescue_handler_class(class_or_name)
120
+ klass === exception
121
+ end
122
+ end
123
+
124
+ handler || find_rescue_handler(exception.cause)
125
+ end
126
+ end
127
+
128
+ def constantize_rescue_handler_class(class_or_name)
129
+ case class_or_name
130
+ when String, Symbol
131
+ begin
132
+ # Try a lexical lookup first since we support
133
+ #
134
+ # class Super
135
+ # rescue_from 'Error', with: …
136
+ # end
137
+ #
138
+ # class Sub
139
+ # class Error < StandardError; end
140
+ # end
141
+ #
142
+ # so an Error raised in Sub will hit the 'Error' handler.
143
+ const_get class_or_name
144
+ rescue NameError
145
+ class_or_name.safe_constantize
146
+ end
147
+ else
148
+ class_or_name
149
+ end
150
+ end
151
+ end
152
+
153
+ # Delegates to the class method, but uses the instance as the subject for
154
+ # rescue_from handlers (method calls, instance_exec blocks).
155
+ def rescue_with_handler(exception)
156
+ self.class.rescue_with_handler exception, object: self
157
+ end
158
+
159
+ # Internal handler lookup. Delegates to class method. Some libraries call
160
+ # this directly, so keeping it around for compatibility.
161
+ def handler_for_rescue(exception) #:nodoc:
162
+ self.class.handler_for_rescue exception, object: self
113
163
  end
114
164
  end
115
165
  end
@@ -0,0 +1,27 @@
1
+ require 'digest'
2
+
3
+ module ActiveSupport
4
+ module SecurityUtils
5
+ # Constant time string comparison.
6
+ #
7
+ # The values compared should be of fixed length, such as strings
8
+ # that have already been processed by HMAC. This should not be used
9
+ # on variable length plaintext strings because it could leak length info
10
+ # via timing attacks.
11
+ def secure_compare(a, b)
12
+ return false unless a.bytesize == b.bytesize
13
+
14
+ l = a.unpack "C#{a.bytesize}"
15
+
16
+ res = 0
17
+ b.each_byte { |byte| res |= byte ^ l.shift }
18
+ res == 0
19
+ end
20
+ module_function :secure_compare
21
+
22
+ def variable_size_secure_compare(a, b) # :nodoc:
23
+ secure_compare(::Digest::SHA256.hexdigest(a), ::Digest::SHA256.hexdigest(b))
24
+ end
25
+ module_function :variable_size_secure_compare
26
+ end
27
+ end
@@ -1,21 +1,26 @@
1
1
  module ActiveSupport
2
2
  # Wrapping a string in this class gives you a prettier way to test
3
3
  # for equality. The value returned by <tt>Rails.env</tt> is wrapped
4
- # in a StringInquirer object so instead of calling this:
4
+ # in a StringInquirer object, so instead of calling this:
5
5
  #
6
- # Rails.env == "production"
6
+ # Rails.env == 'production'
7
7
  #
8
8
  # you can call this:
9
9
  #
10
10
  # Rails.env.production?
11
- #
12
11
  class StringInquirer < String
13
- def method_missing(method_name, *arguments)
14
- if method_name.to_s[-1,1] == "?"
15
- self == method_name.to_s[0..-2]
16
- else
17
- super
12
+ private
13
+
14
+ def respond_to_missing?(method_name, include_private = false)
15
+ method_name[-1] == '?'
16
+ end
17
+
18
+ def method_missing(method_name, *arguments)
19
+ if method_name[-1] == '?'
20
+ self == method_name[0..-2]
21
+ else
22
+ super
23
+ end
18
24
  end
19
- end
20
25
  end
21
26
  end
@@ -0,0 +1,120 @@
1
+ require 'active_support/per_thread_registry'
2
+
3
+ module ActiveSupport
4
+ # ActiveSupport::Subscriber is an object set to consume
5
+ # ActiveSupport::Notifications. The subscriber dispatches notifications to
6
+ # a registered object based on its given namespace.
7
+ #
8
+ # An example would be an Active Record subscriber responsible for collecting
9
+ # statistics about queries:
10
+ #
11
+ # module ActiveRecord
12
+ # class StatsSubscriber < ActiveSupport::Subscriber
13
+ # attach_to :active_record
14
+ #
15
+ # def sql(event)
16
+ # Statsd.timing("sql.#{event.payload[:name]}", event.duration)
17
+ # end
18
+ # end
19
+ # end
20
+ #
21
+ # After configured, whenever a "sql.active_record" notification is published,
22
+ # it will properly dispatch the event (ActiveSupport::Notifications::Event) to
23
+ # the +sql+ method.
24
+ class Subscriber
25
+ class << self
26
+
27
+ # Attach the subscriber to a namespace.
28
+ def attach_to(namespace, subscriber=new, notifier=ActiveSupport::Notifications)
29
+ @namespace = namespace
30
+ @subscriber = subscriber
31
+ @notifier = notifier
32
+
33
+ subscribers << subscriber
34
+
35
+ # Add event subscribers for all existing methods on the class.
36
+ subscriber.public_methods(false).each do |event|
37
+ add_event_subscriber(event)
38
+ end
39
+ end
40
+
41
+ # Adds event subscribers for all new methods added to the class.
42
+ def method_added(event)
43
+ # Only public methods are added as subscribers, and only if a notifier
44
+ # has been set up. This means that subscribers will only be set up for
45
+ # classes that call #attach_to.
46
+ if public_method_defined?(event) && notifier
47
+ add_event_subscriber(event)
48
+ end
49
+ end
50
+
51
+ def subscribers
52
+ @@subscribers ||= []
53
+ end
54
+
55
+ protected
56
+
57
+ attr_reader :subscriber, :notifier, :namespace
58
+
59
+ def add_event_subscriber(event)
60
+ return if %w{ start finish }.include?(event.to_s)
61
+
62
+ pattern = "#{event}.#{namespace}"
63
+
64
+ # Don't add multiple subscribers (eg. if methods are redefined).
65
+ return if subscriber.patterns.include?(pattern)
66
+
67
+ subscriber.patterns << pattern
68
+ notifier.subscribe(pattern, subscriber)
69
+ end
70
+ end
71
+
72
+ attr_reader :patterns # :nodoc:
73
+
74
+ def initialize
75
+ @queue_key = [self.class.name, object_id].join "-"
76
+ @patterns = []
77
+ super
78
+ end
79
+
80
+ def start(name, id, payload)
81
+ e = ActiveSupport::Notifications::Event.new(name, Time.now, nil, id, payload)
82
+ parent = event_stack.last
83
+ parent << e if parent
84
+
85
+ event_stack.push e
86
+ end
87
+
88
+ def finish(name, id, payload)
89
+ finished = Time.now
90
+ event = event_stack.pop
91
+ event.end = finished
92
+ event.payload.merge!(payload)
93
+
94
+ method = name.split('.'.freeze).first
95
+ send(method, event)
96
+ end
97
+
98
+ private
99
+
100
+ def event_stack
101
+ SubscriberQueueRegistry.instance.get_queue(@queue_key)
102
+ end
103
+ end
104
+
105
+ # This is a registry for all the event stacks kept for subscribers.
106
+ #
107
+ # See the documentation of <tt>ActiveSupport::PerThreadRegistry</tt>
108
+ # for further details.
109
+ class SubscriberQueueRegistry # :nodoc:
110
+ extend PerThreadRegistry
111
+
112
+ def initialize
113
+ @registry = {}
114
+ end
115
+
116
+ def get_queue(queue_key)
117
+ @registry[queue_key] ||= []
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,78 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'logger'
4
+ require 'active_support/logger'
5
+
6
+ module ActiveSupport
7
+ # Wraps any standard Logger object to provide tagging capabilities.
8
+ #
9
+ # logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
10
+ # logger.tagged('BCX') { logger.info 'Stuff' } # Logs "[BCX] Stuff"
11
+ # logger.tagged('BCX', "Jason") { logger.info 'Stuff' } # Logs "[BCX] [Jason] Stuff"
12
+ # logger.tagged('BCX') { logger.tagged('Jason') { logger.info 'Stuff' } } # Logs "[BCX] [Jason] Stuff"
13
+ #
14
+ # This is used by the default Rails.logger as configured by Railties to make
15
+ # it easy to stamp log lines with subdomains, request ids, and anything else
16
+ # to aid debugging of multi-user production applications.
17
+ module TaggedLogging
18
+ module Formatter # :nodoc:
19
+ # This method is invoked when a log event occurs.
20
+ def call(severity, timestamp, progname, msg)
21
+ super(severity, timestamp, progname, "#{tags_text}#{msg}")
22
+ end
23
+
24
+ def tagged(*tags)
25
+ new_tags = push_tags(*tags)
26
+ yield self
27
+ ensure
28
+ pop_tags(new_tags.size)
29
+ end
30
+
31
+ def push_tags(*tags)
32
+ tags.flatten.reject(&:blank?).tap do |new_tags|
33
+ current_tags.concat new_tags
34
+ end
35
+ end
36
+
37
+ def pop_tags(size = 1)
38
+ current_tags.pop size
39
+ end
40
+
41
+ def clear_tags!
42
+ current_tags.clear
43
+ end
44
+
45
+ def current_tags
46
+ # We use our object ID here to avoid conflicting with other instances
47
+ thread_key = @thread_key ||= "activesupport_tagged_logging_tags:#{object_id}".freeze
48
+ Thread.current[thread_key] ||= []
49
+ end
50
+
51
+ private
52
+ def tags_text
53
+ tags = current_tags
54
+ if tags.any?
55
+ tags.collect { |tag| "[#{tag}] " }.join
56
+ end
57
+ end
58
+ end
59
+
60
+ def self.new(logger)
61
+ # Ensure we set a default formatter so we aren't extending nil!
62
+ logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new
63
+ logger.formatter.extend Formatter
64
+ logger.extend(self)
65
+ end
66
+
67
+ delegate :push_tags, :pop_tags, :clear_tags!, to: :formatter
68
+
69
+ def tagged(*tags)
70
+ formatter.tagged(*tags) { yield self }
71
+ end
72
+
73
+ def flush
74
+ clear_tags!
75
+ super if defined?(super)
76
+ end
77
+ end
78
+ end
@@ -1,34 +1,86 @@
1
- require 'test/unit/testcase'
1
+ gem 'minitest' # make sure we get the gem, not stdlib
2
+ require 'minitest'
3
+ require 'active_support/testing/tagged_logging'
2
4
  require 'active_support/testing/setup_and_teardown'
3
5
  require 'active_support/testing/assertions'
4
6
  require 'active_support/testing/deprecation'
5
7
  require 'active_support/testing/declarative'
6
- require 'active_support/testing/pending'
7
8
  require 'active_support/testing/isolation'
8
- require 'active_support/testing/mochaing'
9
+ require 'active_support/testing/constant_lookup'
10
+ require 'active_support/testing/time_helpers'
11
+ require 'active_support/testing/file_fixtures'
9
12
  require 'active_support/core_ext/kernel/reporting'
10
13
 
11
14
  module ActiveSupport
12
- class TestCase < ::Test::Unit::TestCase
13
- if defined? MiniTest
14
- Assertion = MiniTest::Assertion
15
- alias_method :method_name, :name if method_defined? :name
16
- alias_method :method_name, :__name__ if method_defined? :__name__
17
- else
18
- Assertion = Test::Unit::AssertionFailedError
19
-
20
- undef :default_test
21
- end
15
+ class TestCase < ::Minitest::Test
16
+ Assertion = Minitest::Assertion
17
+
18
+ class << self
19
+ # Sets the order in which test cases are run.
20
+ #
21
+ # ActiveSupport::TestCase.test_order = :random # => :random
22
+ #
23
+ # Valid values are:
24
+ # * +:random+ (to run tests in random order)
25
+ # * +:parallel+ (to run tests in parallel)
26
+ # * +:sorted+ (to run tests alphabetically by method name)
27
+ # * +:alpha+ (equivalent to +:sorted+)
28
+ def test_order=(new_order)
29
+ ActiveSupport.test_order = new_order
30
+ end
22
31
 
23
- $tags = {}
24
- def self.for_tag(tag)
25
- yield if $tags[tag]
32
+ # Returns the order in which test cases are run.
33
+ #
34
+ # ActiveSupport::TestCase.test_order # => :random
35
+ #
36
+ # Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
37
+ # Defaults to +:random+.
38
+ def test_order
39
+ ActiveSupport.test_order ||= :random
40
+ end
26
41
  end
27
42
 
43
+ alias_method :method_name, :name
44
+
45
+ include ActiveSupport::Testing::TaggedLogging
28
46
  include ActiveSupport::Testing::SetupAndTeardown
29
47
  include ActiveSupport::Testing::Assertions
30
48
  include ActiveSupport::Testing::Deprecation
31
- include ActiveSupport::Testing::Pending
49
+ include ActiveSupport::Testing::TimeHelpers
50
+ include ActiveSupport::Testing::FileFixtures
32
51
  extend ActiveSupport::Testing::Declarative
52
+
53
+ # test/unit backwards compatibility methods
54
+ alias :assert_raise :assert_raises
55
+ alias :assert_not_empty :refute_empty
56
+ alias :assert_not_equal :refute_equal
57
+ alias :assert_not_in_delta :refute_in_delta
58
+ alias :assert_not_in_epsilon :refute_in_epsilon
59
+ alias :assert_not_includes :refute_includes
60
+ alias :assert_not_instance_of :refute_instance_of
61
+ alias :assert_not_kind_of :refute_kind_of
62
+ alias :assert_no_match :refute_match
63
+ alias :assert_not_nil :refute_nil
64
+ alias :assert_not_operator :refute_operator
65
+ alias :assert_not_predicate :refute_predicate
66
+ alias :assert_not_respond_to :refute_respond_to
67
+ alias :assert_not_same :refute_same
68
+
69
+
70
+ # Assertion that the block should not raise an exception.
71
+ #
72
+ # Passes if evaluated code in the yielded block raises no exception.
73
+ #
74
+ # assert_nothing_raised do
75
+ # perform_service(param: 'no_exception')
76
+ # end
77
+ def assert_nothing_raised(*args)
78
+ if args.present?
79
+ ActiveSupport::Deprecation.warn(
80
+ "Passing arguments to assert_nothing_raised " \
81
+ "is deprecated and will be removed in Rails 5.1.")
82
+ end
83
+ yield
84
+ end
33
85
  end
34
86
  end