omg-activesupport 8.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (289) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +86 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +40 -0
  5. data/lib/active_support/actionable_error.rb +50 -0
  6. data/lib/active_support/all.rb +5 -0
  7. data/lib/active_support/array_inquirer.rb +50 -0
  8. data/lib/active_support/backtrace_cleaner.rb +163 -0
  9. data/lib/active_support/benchmark.rb +21 -0
  10. data/lib/active_support/benchmarkable.rb +53 -0
  11. data/lib/active_support/broadcast_logger.rb +251 -0
  12. data/lib/active_support/builder.rb +8 -0
  13. data/lib/active_support/cache/coder.rb +153 -0
  14. data/lib/active_support/cache/entry.rb +134 -0
  15. data/lib/active_support/cache/file_store.rb +244 -0
  16. data/lib/active_support/cache/mem_cache_store.rb +290 -0
  17. data/lib/active_support/cache/memory_store.rb +262 -0
  18. data/lib/active_support/cache/null_store.rb +62 -0
  19. data/lib/active_support/cache/redis_cache_store.rb +492 -0
  20. data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
  21. data/lib/active_support/cache/strategy/local_cache.rb +201 -0
  22. data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
  23. data/lib/active_support/cache.rb +1104 -0
  24. data/lib/active_support/callbacks.rb +944 -0
  25. data/lib/active_support/class_attribute.rb +26 -0
  26. data/lib/active_support/code_generator.rb +79 -0
  27. data/lib/active_support/concern.rb +217 -0
  28. data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +72 -0
  29. data/lib/active_support/concurrency/null_lock.rb +13 -0
  30. data/lib/active_support/concurrency/share_lock.rb +225 -0
  31. data/lib/active_support/configurable.rb +159 -0
  32. data/lib/active_support/configuration_file.rb +60 -0
  33. data/lib/active_support/core_ext/array/access.rb +100 -0
  34. data/lib/active_support/core_ext/array/conversions.rb +213 -0
  35. data/lib/active_support/core_ext/array/extract.rb +21 -0
  36. data/lib/active_support/core_ext/array/extract_options.rb +31 -0
  37. data/lib/active_support/core_ext/array/grouping.rb +109 -0
  38. data/lib/active_support/core_ext/array/inquiry.rb +19 -0
  39. data/lib/active_support/core_ext/array/wrap.rb +48 -0
  40. data/lib/active_support/core_ext/array.rb +9 -0
  41. data/lib/active_support/core_ext/benchmark.rb +13 -0
  42. data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
  43. data/lib/active_support/core_ext/big_decimal.rb +3 -0
  44. data/lib/active_support/core_ext/class/attribute.rb +122 -0
  45. data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
  46. data/lib/active_support/core_ext/class/subclasses.rb +24 -0
  47. data/lib/active_support/core_ext/class.rb +4 -0
  48. data/lib/active_support/core_ext/date/acts_like.rb +10 -0
  49. data/lib/active_support/core_ext/date/blank.rb +18 -0
  50. data/lib/active_support/core_ext/date/calculations.rb +161 -0
  51. data/lib/active_support/core_ext/date/conversions.rb +98 -0
  52. data/lib/active_support/core_ext/date/zones.rb +8 -0
  53. data/lib/active_support/core_ext/date.rb +7 -0
  54. data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
  55. data/lib/active_support/core_ext/date_and_time/compatibility.rb +58 -0
  56. data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
  57. data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
  58. data/lib/active_support/core_ext/date_time/blank.rb +18 -0
  59. data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
  60. data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
  61. data/lib/active_support/core_ext/date_time/conversions.rb +106 -0
  62. data/lib/active_support/core_ext/date_time.rb +7 -0
  63. data/lib/active_support/core_ext/digest/uuid.rb +76 -0
  64. data/lib/active_support/core_ext/digest.rb +3 -0
  65. data/lib/active_support/core_ext/enumerable.rb +267 -0
  66. data/lib/active_support/core_ext/erb/util.rb +201 -0
  67. data/lib/active_support/core_ext/file/atomic.rb +72 -0
  68. data/lib/active_support/core_ext/file.rb +3 -0
  69. data/lib/active_support/core_ext/hash/conversions.rb +262 -0
  70. data/lib/active_support/core_ext/hash/deep_merge.rb +42 -0
  71. data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
  72. data/lib/active_support/core_ext/hash/except.rb +12 -0
  73. data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
  74. data/lib/active_support/core_ext/hash/keys.rb +143 -0
  75. data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
  76. data/lib/active_support/core_ext/hash/slice.rb +27 -0
  77. data/lib/active_support/core_ext/hash.rb +10 -0
  78. data/lib/active_support/core_ext/integer/inflections.rb +31 -0
  79. data/lib/active_support/core_ext/integer/multiple.rb +12 -0
  80. data/lib/active_support/core_ext/integer/time.rb +22 -0
  81. data/lib/active_support/core_ext/integer.rb +5 -0
  82. data/lib/active_support/core_ext/kernel/concern.rb +14 -0
  83. data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
  84. data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
  85. data/lib/active_support/core_ext/kernel.rb +5 -0
  86. data/lib/active_support/core_ext/load_error.rb +9 -0
  87. data/lib/active_support/core_ext/module/aliasing.rb +31 -0
  88. data/lib/active_support/core_ext/module/anonymous.rb +30 -0
  89. data/lib/active_support/core_ext/module/attr_internal.rb +49 -0
  90. data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
  91. data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
  92. data/lib/active_support/core_ext/module/concerning.rb +140 -0
  93. data/lib/active_support/core_ext/module/delegation.rb +225 -0
  94. data/lib/active_support/core_ext/module/deprecation.rb +25 -0
  95. data/lib/active_support/core_ext/module/introspection.rb +62 -0
  96. data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
  97. data/lib/active_support/core_ext/module/remove_method.rb +17 -0
  98. data/lib/active_support/core_ext/module.rb +13 -0
  99. data/lib/active_support/core_ext/name_error.rb +59 -0
  100. data/lib/active_support/core_ext/numeric/bytes.rb +75 -0
  101. data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
  102. data/lib/active_support/core_ext/numeric/time.rb +66 -0
  103. data/lib/active_support/core_ext/numeric.rb +5 -0
  104. data/lib/active_support/core_ext/object/acts_like.rb +45 -0
  105. data/lib/active_support/core_ext/object/blank.rb +199 -0
  106. data/lib/active_support/core_ext/object/conversions.rb +6 -0
  107. data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
  108. data/lib/active_support/core_ext/object/duplicable.rb +69 -0
  109. data/lib/active_support/core_ext/object/inclusion.rb +37 -0
  110. data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
  111. data/lib/active_support/core_ext/object/json.rb +260 -0
  112. data/lib/active_support/core_ext/object/to_param.rb +3 -0
  113. data/lib/active_support/core_ext/object/to_query.rb +87 -0
  114. data/lib/active_support/core_ext/object/try.rb +158 -0
  115. data/lib/active_support/core_ext/object/with.rb +46 -0
  116. data/lib/active_support/core_ext/object/with_options.rb +101 -0
  117. data/lib/active_support/core_ext/object.rb +17 -0
  118. data/lib/active_support/core_ext/pathname/blank.rb +20 -0
  119. data/lib/active_support/core_ext/pathname/existence.rb +23 -0
  120. data/lib/active_support/core_ext/pathname.rb +4 -0
  121. data/lib/active_support/core_ext/range/compare_range.rb +57 -0
  122. data/lib/active_support/core_ext/range/conversions.rb +62 -0
  123. data/lib/active_support/core_ext/range/each.rb +24 -0
  124. data/lib/active_support/core_ext/range/overlap.rb +40 -0
  125. data/lib/active_support/core_ext/range.rb +6 -0
  126. data/lib/active_support/core_ext/regexp.rb +14 -0
  127. data/lib/active_support/core_ext/securerandom.rb +41 -0
  128. data/lib/active_support/core_ext/string/access.rb +95 -0
  129. data/lib/active_support/core_ext/string/behavior.rb +8 -0
  130. data/lib/active_support/core_ext/string/conversions.rb +60 -0
  131. data/lib/active_support/core_ext/string/exclude.rb +13 -0
  132. data/lib/active_support/core_ext/string/filters.rb +151 -0
  133. data/lib/active_support/core_ext/string/indent.rb +45 -0
  134. data/lib/active_support/core_ext/string/inflections.rb +300 -0
  135. data/lib/active_support/core_ext/string/inquiry.rb +16 -0
  136. data/lib/active_support/core_ext/string/multibyte.rb +58 -0
  137. data/lib/active_support/core_ext/string/output_safety.rb +228 -0
  138. data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
  139. data/lib/active_support/core_ext/string/strip.rb +27 -0
  140. data/lib/active_support/core_ext/string/zones.rb +16 -0
  141. data/lib/active_support/core_ext/string.rb +15 -0
  142. data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
  143. data/lib/active_support/core_ext/symbol.rb +3 -0
  144. data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
  145. data/lib/active_support/core_ext/time/acts_like.rb +10 -0
  146. data/lib/active_support/core_ext/time/calculations.rb +386 -0
  147. data/lib/active_support/core_ext/time/compatibility.rb +32 -0
  148. data/lib/active_support/core_ext/time/conversions.rb +75 -0
  149. data/lib/active_support/core_ext/time/zones.rb +97 -0
  150. data/lib/active_support/core_ext/time.rb +7 -0
  151. data/lib/active_support/core_ext.rb +5 -0
  152. data/lib/active_support/current_attributes/test_helper.rb +13 -0
  153. data/lib/active_support/current_attributes.rb +233 -0
  154. data/lib/active_support/deep_mergeable.rb +53 -0
  155. data/lib/active_support/delegation.rb +202 -0
  156. data/lib/active_support/dependencies/autoload.rb +72 -0
  157. data/lib/active_support/dependencies/interlock.rb +49 -0
  158. data/lib/active_support/dependencies/require_dependency.rb +28 -0
  159. data/lib/active_support/dependencies.rb +98 -0
  160. data/lib/active_support/deprecation/behaviors.rb +148 -0
  161. data/lib/active_support/deprecation/constant_accessor.rb +74 -0
  162. data/lib/active_support/deprecation/deprecators.rb +104 -0
  163. data/lib/active_support/deprecation/disallowed.rb +54 -0
  164. data/lib/active_support/deprecation/method_wrappers.rb +68 -0
  165. data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
  166. data/lib/active_support/deprecation/reporting.rb +179 -0
  167. data/lib/active_support/deprecation.rb +81 -0
  168. data/lib/active_support/deprecator.rb +7 -0
  169. data/lib/active_support/descendants_tracker.rb +112 -0
  170. data/lib/active_support/digest.rb +22 -0
  171. data/lib/active_support/duration/iso8601_parser.rb +123 -0
  172. data/lib/active_support/duration/iso8601_serializer.rb +64 -0
  173. data/lib/active_support/duration.rb +520 -0
  174. data/lib/active_support/encrypted_configuration.rb +126 -0
  175. data/lib/active_support/encrypted_file.rb +133 -0
  176. data/lib/active_support/environment_inquirer.rb +40 -0
  177. data/lib/active_support/error_reporter/test_helper.rb +15 -0
  178. data/lib/active_support/error_reporter.rb +265 -0
  179. data/lib/active_support/evented_file_update_checker.rb +182 -0
  180. data/lib/active_support/execution_context/test_helper.rb +13 -0
  181. data/lib/active_support/execution_context.rb +53 -0
  182. data/lib/active_support/execution_wrapper.rb +150 -0
  183. data/lib/active_support/executor/test_helper.rb +7 -0
  184. data/lib/active_support/executor.rb +8 -0
  185. data/lib/active_support/file_update_checker.rb +164 -0
  186. data/lib/active_support/fork_tracker.rb +43 -0
  187. data/lib/active_support/gem_version.rb +17 -0
  188. data/lib/active_support/gzip.rb +40 -0
  189. data/lib/active_support/hash_with_indifferent_access.rb +445 -0
  190. data/lib/active_support/html_safe_translation.rb +56 -0
  191. data/lib/active_support/i18n.rb +17 -0
  192. data/lib/active_support/i18n_railtie.rb +138 -0
  193. data/lib/active_support/inflections.rb +72 -0
  194. data/lib/active_support/inflector/inflections.rb +273 -0
  195. data/lib/active_support/inflector/methods.rb +387 -0
  196. data/lib/active_support/inflector/transliterate.rb +149 -0
  197. data/lib/active_support/inflector.rb +9 -0
  198. data/lib/active_support/isolated_execution_state.rb +75 -0
  199. data/lib/active_support/json/decoding.rb +76 -0
  200. data/lib/active_support/json/encoding.rb +120 -0
  201. data/lib/active_support/json.rb +4 -0
  202. data/lib/active_support/key_generator.rb +66 -0
  203. data/lib/active_support/lazy_load_hooks.rb +107 -0
  204. data/lib/active_support/locale/en.rb +33 -0
  205. data/lib/active_support/locale/en.yml +141 -0
  206. data/lib/active_support/log_subscriber/test_helper.rb +106 -0
  207. data/lib/active_support/log_subscriber.rb +192 -0
  208. data/lib/active_support/logger.rb +55 -0
  209. data/lib/active_support/logger_silence.rb +21 -0
  210. data/lib/active_support/logger_thread_safe_level.rb +47 -0
  211. data/lib/active_support/message_encryptor.rb +374 -0
  212. data/lib/active_support/message_encryptors.rb +141 -0
  213. data/lib/active_support/message_pack/cache_serializer.rb +23 -0
  214. data/lib/active_support/message_pack/extensions.rb +305 -0
  215. data/lib/active_support/message_pack/serializer.rb +63 -0
  216. data/lib/active_support/message_pack.rb +50 -0
  217. data/lib/active_support/message_verifier.rb +368 -0
  218. data/lib/active_support/message_verifiers.rb +135 -0
  219. data/lib/active_support/messages/codec.rb +65 -0
  220. data/lib/active_support/messages/metadata.rb +146 -0
  221. data/lib/active_support/messages/rotation_configuration.rb +23 -0
  222. data/lib/active_support/messages/rotation_coordinator.rb +93 -0
  223. data/lib/active_support/messages/rotator.rb +59 -0
  224. data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
  225. data/lib/active_support/multibyte/chars.rb +178 -0
  226. data/lib/active_support/multibyte/unicode.rb +42 -0
  227. data/lib/active_support/multibyte.rb +23 -0
  228. data/lib/active_support/notifications/fanout.rb +446 -0
  229. data/lib/active_support/notifications/instrumenter.rb +240 -0
  230. data/lib/active_support/notifications.rb +281 -0
  231. data/lib/active_support/number_helper/number_converter.rb +190 -0
  232. data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
  233. data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
  234. data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
  235. data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
  236. data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
  237. data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
  238. data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
  239. data/lib/active_support/number_helper/rounding_helper.rb +46 -0
  240. data/lib/active_support/number_helper.rb +479 -0
  241. data/lib/active_support/option_merger.rb +38 -0
  242. data/lib/active_support/ordered_hash.rb +50 -0
  243. data/lib/active_support/ordered_options.rb +147 -0
  244. data/lib/active_support/parameter_filter.rb +157 -0
  245. data/lib/active_support/proxy_object.rb +20 -0
  246. data/lib/active_support/rails.rb +26 -0
  247. data/lib/active_support/railtie.rb +161 -0
  248. data/lib/active_support/reloader.rb +138 -0
  249. data/lib/active_support/rescuable.rb +176 -0
  250. data/lib/active_support/secure_compare_rotator.rb +58 -0
  251. data/lib/active_support/security_utils.rb +38 -0
  252. data/lib/active_support/string_inquirer.rb +35 -0
  253. data/lib/active_support/subscriber.rb +146 -0
  254. data/lib/active_support/syntax_error_proxy.rb +60 -0
  255. data/lib/active_support/tagged_logging.rb +152 -0
  256. data/lib/active_support/test_case.rb +304 -0
  257. data/lib/active_support/testing/assertions.rb +332 -0
  258. data/lib/active_support/testing/autorun.rb +5 -0
  259. data/lib/active_support/testing/constant_lookup.rb +51 -0
  260. data/lib/active_support/testing/constant_stubbing.rb +54 -0
  261. data/lib/active_support/testing/declarative.rb +28 -0
  262. data/lib/active_support/testing/deprecation.rb +82 -0
  263. data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
  264. data/lib/active_support/testing/file_fixtures.rb +38 -0
  265. data/lib/active_support/testing/isolation.rb +121 -0
  266. data/lib/active_support/testing/method_call_assertions.rb +69 -0
  267. data/lib/active_support/testing/parallelization/server.rb +85 -0
  268. data/lib/active_support/testing/parallelization/worker.rb +103 -0
  269. data/lib/active_support/testing/parallelization.rb +55 -0
  270. data/lib/active_support/testing/parallelize_executor.rb +81 -0
  271. data/lib/active_support/testing/setup_and_teardown.rb +57 -0
  272. data/lib/active_support/testing/stream.rb +41 -0
  273. data/lib/active_support/testing/strict_warnings.rb +43 -0
  274. data/lib/active_support/testing/tagged_logging.rb +27 -0
  275. data/lib/active_support/testing/tests_without_assertions.rb +19 -0
  276. data/lib/active_support/testing/time_helpers.rb +269 -0
  277. data/lib/active_support/time.rb +20 -0
  278. data/lib/active_support/time_with_zone.rb +609 -0
  279. data/lib/active_support/values/time_zone.rb +614 -0
  280. data/lib/active_support/version.rb +10 -0
  281. data/lib/active_support/xml_mini/jdom.rb +175 -0
  282. data/lib/active_support/xml_mini/libxml.rb +80 -0
  283. data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
  284. data/lib/active_support/xml_mini/nokogiri.rb +83 -0
  285. data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
  286. data/lib/active_support/xml_mini/rexml.rb +137 -0
  287. data/lib/active_support/xml_mini.rb +211 -0
  288. data/lib/active_support.rb +144 -0
  289. metadata +526 -0
@@ -0,0 +1,944 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+ require "active_support/descendants_tracker"
5
+ require "active_support/core_ext/array/extract_options"
6
+ require "active_support/core_ext/class/attribute"
7
+ require "active_support/core_ext/string/filters"
8
+ require "active_support/core_ext/object/blank"
9
+
10
+ module ActiveSupport
11
+ # = Active Support \Callbacks
12
+ #
13
+ # \Callbacks are code hooks that are run at key points in an object's life cycle.
14
+ # The typical use case is to have a base class define a set of callbacks
15
+ # relevant to the other functionality it supplies, so that subclasses can
16
+ # install callbacks that enhance or modify the base functionality without
17
+ # needing to override or redefine methods of the base class.
18
+ #
19
+ # Mixing in this module allows you to define the events in the object's
20
+ # life cycle that will support callbacks (via ClassMethods#define_callbacks),
21
+ # set the instance methods, procs, or callback objects to be called (via
22
+ # ClassMethods#set_callback), and run the installed callbacks at the
23
+ # appropriate times (via +run_callbacks+).
24
+ #
25
+ # By default callbacks are halted by throwing +:abort+.
26
+ # See ClassMethods#define_callbacks for details.
27
+ #
28
+ # Three kinds of callbacks are supported: before callbacks, run before a
29
+ # certain event; after callbacks, run after the event; and around callbacks,
30
+ # blocks that surround the event, triggering it when they yield. Callback code
31
+ # can be contained in instance methods, procs or lambdas, or callback objects
32
+ # that respond to certain predetermined methods. See ClassMethods#set_callback
33
+ # for details.
34
+ #
35
+ # class Record
36
+ # include ActiveSupport::Callbacks
37
+ # define_callbacks :save
38
+ #
39
+ # def save
40
+ # run_callbacks :save do
41
+ # puts "- save"
42
+ # end
43
+ # end
44
+ # end
45
+ #
46
+ # class PersonRecord < Record
47
+ # set_callback :save, :before, :saving_message
48
+ # def saving_message
49
+ # puts "saving..."
50
+ # end
51
+ #
52
+ # set_callback :save, :after do |object|
53
+ # puts "saved"
54
+ # end
55
+ # end
56
+ #
57
+ # person = PersonRecord.new
58
+ # person.save
59
+ #
60
+ # Output:
61
+ # saving...
62
+ # - save
63
+ # saved
64
+ module Callbacks
65
+ extend Concern
66
+
67
+ included do
68
+ extend ActiveSupport::DescendantsTracker
69
+ class_attribute :__callbacks, instance_writer: false, instance_predicate: false, default: {}
70
+ end
71
+
72
+ CALLBACK_FILTER_TYPES = [:before, :after, :around].freeze
73
+
74
+ # Runs the callbacks for the given event.
75
+ #
76
+ # Calls the before and around callbacks in the order they were set, yields
77
+ # the block (if given one), and then runs the after callbacks in reverse
78
+ # order.
79
+ #
80
+ # If the callback chain was halted, returns +false+. Otherwise returns the
81
+ # result of the block, +nil+ if no callbacks have been set, or +true+
82
+ # if callbacks have been set but no block is given.
83
+ #
84
+ # run_callbacks :save do
85
+ # save
86
+ # end
87
+ #
88
+ #--
89
+ #
90
+ # As this method is used in many places, and often wraps large portions of
91
+ # user code, it has an additional design goal of minimizing its impact on
92
+ # the visible call stack. An exception from inside a :before or :after
93
+ # callback can be as noisy as it likes -- but when control has passed
94
+ # smoothly through and into the supplied block, we want as little evidence
95
+ # as possible that we were here.
96
+ def run_callbacks(kind, type = nil)
97
+ callbacks = __callbacks[kind.to_sym]
98
+
99
+ if callbacks.empty?
100
+ yield if block_given?
101
+ else
102
+ env = Filters::Environment.new(self, false, nil)
103
+
104
+ next_sequence = callbacks.compile(type)
105
+
106
+ # Common case: no 'around' callbacks defined
107
+ if next_sequence.final?
108
+ next_sequence.invoke_before(env)
109
+ env.value = !env.halted && (!block_given? || yield)
110
+ next_sequence.invoke_after(env)
111
+ env.value
112
+ else
113
+ invoke_sequence = Proc.new do
114
+ skipped = nil
115
+
116
+ while true
117
+ current = next_sequence
118
+ current.invoke_before(env)
119
+ if current.final?
120
+ env.value = !env.halted && (!block_given? || yield)
121
+ elsif current.skip?(env)
122
+ (skipped ||= []) << current
123
+ next_sequence = next_sequence.nested
124
+ next
125
+ else
126
+ next_sequence = next_sequence.nested
127
+ begin
128
+ target, block, method, *arguments = current.expand_call_template(env, invoke_sequence)
129
+ target.send(method, *arguments, &block)
130
+ ensure
131
+ next_sequence = current
132
+ end
133
+ end
134
+ current.invoke_after(env)
135
+ skipped.pop.invoke_after(env) while skipped&.first
136
+ break env.value
137
+ end
138
+ end
139
+
140
+ invoke_sequence.call
141
+ end
142
+ end
143
+ end
144
+
145
+ private
146
+ # A hook invoked every time a before callback is halted.
147
+ # This can be overridden in ActiveSupport::Callbacks implementors in order
148
+ # to provide better debugging/logging.
149
+ def halted_callback_hook(filter, name)
150
+ end
151
+
152
+ module Conditionals # :nodoc: all
153
+ class Value
154
+ def initialize(&block)
155
+ @block = block
156
+ end
157
+ def call(target, value); @block.call(value); end
158
+ end
159
+ end
160
+
161
+ module Filters # :nodoc: all
162
+ Environment = Struct.new(:target, :halted, :value)
163
+
164
+ class Before
165
+ def initialize(user_callback, user_conditions, chain_config, filter, name)
166
+ halted_lambda = chain_config[:terminator]
167
+ @user_callback, @user_conditions, @halted_lambda, @filter, @name = user_callback, user_conditions, halted_lambda, filter, name
168
+ freeze
169
+ end
170
+ attr_reader :user_callback, :user_conditions, :halted_lambda, :filter, :name
171
+
172
+ def call(env)
173
+ target = env.target
174
+ value = env.value
175
+ halted = env.halted
176
+
177
+ if !halted && user_conditions.all? { |c| c.call(target, value) }
178
+ result_lambda = -> { user_callback.call target, value }
179
+ env.halted = halted_lambda.call(target, result_lambda)
180
+ if env.halted
181
+ target.send :halted_callback_hook, filter, name
182
+ end
183
+ end
184
+
185
+ env
186
+ end
187
+
188
+ def apply(callback_sequence)
189
+ callback_sequence.before(self)
190
+ end
191
+ end
192
+
193
+ class After
194
+ attr_reader :user_callback, :user_conditions, :halting
195
+ def initialize(user_callback, user_conditions, chain_config)
196
+ halting = chain_config[:skip_after_callbacks_if_terminated]
197
+ @user_callback, @user_conditions, @halting = user_callback, user_conditions, halting
198
+ freeze
199
+ end
200
+
201
+ def call(env)
202
+ target = env.target
203
+ value = env.value
204
+ halted = env.halted
205
+
206
+ if (!halted || !@halting) && user_conditions.all? { |c| c.call(target, value) }
207
+ user_callback.call target, value
208
+ end
209
+
210
+ env
211
+ end
212
+
213
+ def apply(callback_sequence)
214
+ callback_sequence.after(self)
215
+ end
216
+ end
217
+
218
+ class Around
219
+ def initialize(user_callback, user_conditions)
220
+ @user_callback, @user_conditions = user_callback, user_conditions
221
+ freeze
222
+ end
223
+
224
+ def apply(callback_sequence)
225
+ callback_sequence.around(@user_callback, @user_conditions)
226
+ end
227
+ end
228
+ end
229
+
230
+ class Callback # :nodoc:
231
+ def self.build(chain, filter, kind, options)
232
+ if filter.is_a?(String)
233
+ raise ArgumentError, <<-MSG.squish
234
+ Passing string to define a callback is not supported. See the `.set_callback`
235
+ documentation to see supported values.
236
+ MSG
237
+ end
238
+
239
+ new chain.name, filter, kind, options, chain.config
240
+ end
241
+
242
+ attr_accessor :kind, :name
243
+ attr_reader :chain_config, :filter
244
+
245
+ def initialize(name, filter, kind, options, chain_config)
246
+ @chain_config = chain_config
247
+ @name = name
248
+ @kind = kind
249
+ @filter = filter
250
+ @if = check_conditionals(options[:if])
251
+ @unless = check_conditionals(options[:unless])
252
+
253
+ compiled
254
+ end
255
+
256
+ def merge_conditional_options(chain, if_option:, unless_option:)
257
+ options = {
258
+ if: @if.dup,
259
+ unless: @unless.dup
260
+ }
261
+
262
+ options[:if].concat Array(unless_option)
263
+ options[:unless].concat Array(if_option)
264
+
265
+ self.class.build chain, @filter, @kind, options
266
+ end
267
+
268
+ def matches?(_kind, _filter)
269
+ @kind == _kind && filter == _filter
270
+ end
271
+
272
+ def duplicates?(other)
273
+ case @filter
274
+ when Symbol
275
+ matches?(other.kind, other.filter)
276
+ else
277
+ false
278
+ end
279
+ end
280
+
281
+ def compiled
282
+ @compiled ||=
283
+ begin
284
+ user_conditions = conditions_lambdas
285
+ user_callback = CallTemplate.build(@filter, self)
286
+
287
+ case kind
288
+ when :before
289
+ Filters::Before.new(user_callback.make_lambda, user_conditions, chain_config, @filter, name)
290
+ when :after
291
+ Filters::After.new(user_callback.make_lambda, user_conditions, chain_config)
292
+ when :around
293
+ Filters::Around.new(user_callback, user_conditions)
294
+ end
295
+ end
296
+ end
297
+
298
+ # Wraps code with filter
299
+ def apply(callback_sequence)
300
+ compiled.apply(callback_sequence)
301
+ end
302
+
303
+ def current_scopes
304
+ Array(chain_config[:scope]).map { |s| public_send(s) }
305
+ end
306
+
307
+ private
308
+ EMPTY_ARRAY = [].freeze
309
+ private_constant :EMPTY_ARRAY
310
+
311
+ def check_conditionals(conditionals)
312
+ return EMPTY_ARRAY if conditionals.blank?
313
+
314
+ conditionals = Array(conditionals)
315
+ if conditionals.any?(String)
316
+ raise ArgumentError, <<-MSG.squish
317
+ Passing string to be evaluated in :if and :unless conditional
318
+ options is not supported. Pass a symbol for an instance method,
319
+ or a lambda, proc or block, instead.
320
+ MSG
321
+ end
322
+
323
+ conditionals.freeze
324
+ end
325
+
326
+ def conditions_lambdas
327
+ conditions =
328
+ @if.map { |c| CallTemplate.build(c, self).make_lambda } +
329
+ @unless.map { |c| CallTemplate.build(c, self).inverted_lambda }
330
+ conditions.empty? ? EMPTY_ARRAY : conditions
331
+ end
332
+ end
333
+
334
+ # A future invocation of user-supplied code (either as a callback,
335
+ # or a condition filter).
336
+ module CallTemplate # :nodoc: all
337
+ class MethodCall
338
+ def initialize(method)
339
+ @method_name = method
340
+ end
341
+
342
+ # Return the parts needed to make this call, with the given
343
+ # input values.
344
+ #
345
+ # Returns an array of the form:
346
+ #
347
+ # [target, block, method, *arguments]
348
+ #
349
+ # This array can be used as such:
350
+ #
351
+ # target.send(method, *arguments, &block)
352
+ #
353
+ # The actual invocation is left up to the caller to minimize
354
+ # call stack pollution.
355
+ def expand(target, value, block)
356
+ [target, block, @method_name]
357
+ end
358
+
359
+ def make_lambda
360
+ lambda do |target, value, &block|
361
+ target.send(@method_name, &block)
362
+ end
363
+ end
364
+
365
+ def inverted_lambda
366
+ lambda do |target, value, &block|
367
+ !target.send(@method_name, &block)
368
+ end
369
+ end
370
+ end
371
+
372
+ class ObjectCall
373
+ def initialize(target, method)
374
+ @override_target = target
375
+ @method_name = method
376
+ end
377
+
378
+ def expand(target, value, block)
379
+ [@override_target || target, block, @method_name, target]
380
+ end
381
+
382
+ def make_lambda
383
+ lambda do |target, value, &block|
384
+ (@override_target || target).send(@method_name, target, &block)
385
+ end
386
+ end
387
+
388
+ def inverted_lambda
389
+ lambda do |target, value, &block|
390
+ !(@override_target || target).send(@method_name, target, &block)
391
+ end
392
+ end
393
+ end
394
+
395
+ class InstanceExec0
396
+ def initialize(block)
397
+ @override_block = block
398
+ end
399
+
400
+ def expand(target, value, block)
401
+ [target, @override_block, :instance_exec]
402
+ end
403
+
404
+ def make_lambda
405
+ lambda do |target, value, &block|
406
+ target.instance_exec(&@override_block)
407
+ end
408
+ end
409
+
410
+ def inverted_lambda
411
+ lambda do |target, value, &block|
412
+ !target.instance_exec(&@override_block)
413
+ end
414
+ end
415
+ end
416
+
417
+ class InstanceExec1
418
+ def initialize(block)
419
+ @override_block = block
420
+ end
421
+
422
+ def expand(target, value, block)
423
+ [target, @override_block, :instance_exec, target]
424
+ end
425
+
426
+ def make_lambda
427
+ lambda do |target, value, &block|
428
+ target.instance_exec(target, &@override_block)
429
+ end
430
+ end
431
+
432
+ def inverted_lambda
433
+ lambda do |target, value, &block|
434
+ !target.instance_exec(target, &@override_block)
435
+ end
436
+ end
437
+ end
438
+
439
+ class InstanceExec2
440
+ def initialize(block)
441
+ @override_block = block
442
+ end
443
+
444
+ def expand(target, value, block)
445
+ raise ArgumentError unless block
446
+ [target, @override_block || block, :instance_exec, target, block]
447
+ end
448
+
449
+ def make_lambda
450
+ lambda do |target, value, &block|
451
+ raise ArgumentError unless block
452
+ target.instance_exec(target, block, &@override_block)
453
+ end
454
+ end
455
+
456
+ def inverted_lambda
457
+ lambda do |target, value, &block|
458
+ raise ArgumentError unless block
459
+ !target.instance_exec(target, block, &@override_block)
460
+ end
461
+ end
462
+ end
463
+
464
+ class ProcCall
465
+ def initialize(target)
466
+ @override_target = target
467
+ end
468
+
469
+ def expand(target, value, block)
470
+ [@override_target || target, block, :call, target, value]
471
+ end
472
+
473
+ def make_lambda
474
+ lambda do |target, value, &block|
475
+ (@override_target || target).call(target, value, &block)
476
+ end
477
+ end
478
+
479
+ def inverted_lambda
480
+ lambda do |target, value, &block|
481
+ !(@override_target || target).call(target, value, &block)
482
+ end
483
+ end
484
+ end
485
+
486
+ # Filters support:
487
+ #
488
+ # Symbols:: A method to call.
489
+ # Procs:: A proc to call with the object.
490
+ # Objects:: An object with a <tt>before_foo</tt> method on it to call.
491
+ #
492
+ # All of these objects are converted into a CallTemplate and handled
493
+ # the same after this point.
494
+ def self.build(filter, callback)
495
+ case filter
496
+ when Symbol
497
+ MethodCall.new(filter)
498
+ when Conditionals::Value
499
+ ProcCall.new(filter)
500
+ when ::Proc
501
+ if filter.arity > 1
502
+ InstanceExec2.new(filter)
503
+ elsif filter.arity > 0
504
+ InstanceExec1.new(filter)
505
+ else
506
+ InstanceExec0.new(filter)
507
+ end
508
+ else
509
+ ObjectCall.new(filter, callback.current_scopes.join("_").to_sym)
510
+ end
511
+ end
512
+ end
513
+
514
+ # Execute before and after filters in a sequence instead of
515
+ # chaining them with nested lambda calls, see:
516
+ # https://github.com/rails/rails/issues/18011
517
+ class CallbackSequence # :nodoc:
518
+ def initialize(nested = nil, call_template = nil, user_conditions = nil)
519
+ @nested = nested
520
+ @call_template = call_template
521
+ @user_conditions = user_conditions
522
+
523
+ @before = nil
524
+ @after = nil
525
+ end
526
+
527
+ def before(before)
528
+ @before ||= []
529
+ @before.unshift(before)
530
+ self
531
+ end
532
+
533
+ def after(after)
534
+ @after ||= []
535
+ @after.push(after)
536
+ self
537
+ end
538
+
539
+ def around(call_template, user_conditions)
540
+ CallbackSequence.new(self, call_template, user_conditions)
541
+ end
542
+
543
+ def skip?(arg)
544
+ arg.halted || !@user_conditions.all? { |c| c.call(arg.target, arg.value) }
545
+ end
546
+
547
+ attr_reader :nested
548
+
549
+ def final?
550
+ !@call_template
551
+ end
552
+
553
+ def expand_call_template(arg, block)
554
+ @call_template.expand(arg.target, arg.value, block)
555
+ end
556
+
557
+ def invoke_before(arg)
558
+ @before&.each { |b| b.call(arg) }
559
+ end
560
+
561
+ def invoke_after(arg)
562
+ @after&.each { |a| a.call(arg) }
563
+ end
564
+ end
565
+
566
+ class CallbackChain # :nodoc:
567
+ include Enumerable
568
+
569
+ attr_reader :name, :config
570
+
571
+ def initialize(name, config)
572
+ @name = name
573
+ @config = {
574
+ scope: [:kind],
575
+ terminator: default_terminator
576
+ }.merge!(config)
577
+ @chain = []
578
+ @all_callbacks = nil
579
+ @single_callbacks = {}
580
+ @mutex = Mutex.new
581
+ end
582
+
583
+ def each(&block); @chain.each(&block); end
584
+ def index(o); @chain.index(o); end
585
+ def empty?; @chain.empty?; end
586
+
587
+ def insert(index, o)
588
+ @all_callbacks = nil
589
+ @single_callbacks.clear
590
+ @chain.insert(index, o)
591
+ end
592
+
593
+ def delete(o)
594
+ @all_callbacks = nil
595
+ @single_callbacks.clear
596
+ @chain.delete(o)
597
+ end
598
+
599
+ def clear
600
+ @all_callbacks = nil
601
+ @single_callbacks.clear
602
+ @chain.clear
603
+ self
604
+ end
605
+
606
+ def initialize_copy(other)
607
+ @all_callbacks = nil
608
+ @single_callbacks = {}
609
+ @chain = other.chain.dup
610
+ @mutex = Mutex.new
611
+ end
612
+
613
+ def compile(type)
614
+ if type.nil?
615
+ @all_callbacks || @mutex.synchronize do
616
+ final_sequence = CallbackSequence.new
617
+ @all_callbacks ||= @chain.reverse.inject(final_sequence) do |callback_sequence, callback|
618
+ callback.apply(callback_sequence)
619
+ end
620
+ end
621
+ else
622
+ @single_callbacks[type] || @mutex.synchronize do
623
+ final_sequence = CallbackSequence.new
624
+ @single_callbacks[type] ||= @chain.reverse.inject(final_sequence) do |callback_sequence, callback|
625
+ type == callback.kind ? callback.apply(callback_sequence) : callback_sequence
626
+ end
627
+ end
628
+ end
629
+ end
630
+
631
+ def append(*callbacks)
632
+ callbacks.each { |c| append_one(c) }
633
+ end
634
+
635
+ def prepend(*callbacks)
636
+ callbacks.each { |c| prepend_one(c) }
637
+ end
638
+
639
+ protected
640
+ attr_reader :chain
641
+
642
+ private
643
+ def append_one(callback)
644
+ @all_callbacks = nil
645
+ @single_callbacks.clear
646
+ remove_duplicates(callback)
647
+ @chain.push(callback)
648
+ end
649
+
650
+ def prepend_one(callback)
651
+ @all_callbacks = nil
652
+ @single_callbacks.clear
653
+ remove_duplicates(callback)
654
+ @chain.unshift(callback)
655
+ end
656
+
657
+ def remove_duplicates(callback)
658
+ @all_callbacks = nil
659
+ @single_callbacks.clear
660
+ @chain.delete_if { |c| callback.duplicates?(c) }
661
+ end
662
+
663
+ def default_terminator
664
+ Proc.new do |target, result_lambda|
665
+ terminate = true
666
+ catch(:abort) do
667
+ result_lambda.call
668
+ terminate = false
669
+ end
670
+ terminate
671
+ end
672
+ end
673
+ end
674
+
675
+ module ClassMethods
676
+ def normalize_callback_params(filters, block) # :nodoc:
677
+ type = CALLBACK_FILTER_TYPES.include?(filters.first) ? filters.shift : :before
678
+ options = filters.extract_options!
679
+ filters.unshift(block) if block
680
+ [type, filters, options.dup]
681
+ end
682
+
683
+ # This is used internally to append, prepend and skip callbacks to the
684
+ # CallbackChain.
685
+ def __update_callbacks(name) # :nodoc:
686
+ self.descendants.prepend(self).reverse_each do |target|
687
+ chain = target.get_callbacks name
688
+ yield target, chain.dup
689
+ end
690
+ end
691
+
692
+ # Install a callback for the given event.
693
+ #
694
+ # set_callback :save, :before, :before_method
695
+ # set_callback :save, :after, :after_method, if: :condition
696
+ # set_callback :save, :around, ->(r, block) { stuff; result = block.call; stuff }
697
+ #
698
+ # The second argument indicates whether the callback is to be run +:before+,
699
+ # +:after+, or +:around+ the event. If omitted, +:before+ is assumed. This
700
+ # means the first example above can also be written as:
701
+ #
702
+ # set_callback :save, :before_method
703
+ #
704
+ # The callback can be specified as a symbol naming an instance method; as a
705
+ # proc, lambda, or block; or as an object that responds to a certain method
706
+ # determined by the <tt>:scope</tt> argument to #define_callbacks.
707
+ #
708
+ # If a proc, lambda, or block is given, its body is evaluated in the context
709
+ # of the current object. It can also optionally accept the current object as
710
+ # an argument.
711
+ #
712
+ # Before and around callbacks are called in the order that they are set;
713
+ # after callbacks are called in the reverse order.
714
+ #
715
+ # Around callbacks can access the return value from the event, if it
716
+ # wasn't halted, from the +yield+ call.
717
+ #
718
+ # ===== Options
719
+ #
720
+ # * <tt>:if</tt> - A symbol or an array of symbols, each naming an instance
721
+ # method or a proc; the callback will be called only when they all return
722
+ # a true value.
723
+ #
724
+ # If a proc is given, its body is evaluated in the context of the
725
+ # current object. It can also optionally accept the current object as
726
+ # an argument.
727
+ # * <tt>:unless</tt> - A symbol or an array of symbols, each naming an
728
+ # instance method or a proc; the callback will be called only when they
729
+ # all return a false value.
730
+ #
731
+ # If a proc is given, its body is evaluated in the context of the
732
+ # current object. It can also optionally accept the current object as
733
+ # an argument.
734
+ # * <tt>:prepend</tt> - If +true+, the callback will be prepended to the
735
+ # existing chain rather than appended.
736
+ def set_callback(name, *filter_list, &block)
737
+ type, filters, options = normalize_callback_params(filter_list, block)
738
+
739
+ self_chain = get_callbacks name
740
+ mapped = filters.map do |filter|
741
+ Callback.build(self_chain, filter, type, options)
742
+ end
743
+
744
+ __update_callbacks(name) do |target, chain|
745
+ options[:prepend] ? chain.prepend(*mapped) : chain.append(*mapped)
746
+ target.set_callbacks name, chain
747
+ end
748
+ end
749
+
750
+ # Skip a previously set callback. Like #set_callback, <tt>:if</tt> or
751
+ # <tt>:unless</tt> options may be passed in order to control when the
752
+ # callback is skipped.
753
+ #
754
+ # Note: this example uses +PersonRecord+ and +#saving_message+, which you
755
+ # can see defined here[rdoc-ref:ActiveSupport::Callbacks]
756
+ #
757
+ # class Writer < PersonRecord
758
+ # attr_accessor :age
759
+ # skip_callback :save, :before, :saving_message, if: -> { age > 18 }
760
+ # end
761
+ #
762
+ # When if option returns true, callback is skipped.
763
+ #
764
+ # writer = Writer.new
765
+ # writer.age = 20
766
+ # writer.save
767
+ #
768
+ # Output:
769
+ # - save
770
+ # saved
771
+ #
772
+ # When if option returns false, callback is NOT skipped.
773
+ #
774
+ # young_writer = Writer.new
775
+ # young_writer.age = 17
776
+ # young_writer.save
777
+ #
778
+ # Output:
779
+ # saving...
780
+ # - save
781
+ # saved
782
+ #
783
+ # An <tt>ArgumentError</tt> will be raised if the callback has not
784
+ # already been set (unless the <tt>:raise</tt> option is set to <tt>false</tt>).
785
+ def skip_callback(name, *filter_list, &block)
786
+ type, filters, options = normalize_callback_params(filter_list, block)
787
+
788
+ options[:raise] = true unless options.key?(:raise)
789
+
790
+ __update_callbacks(name) do |target, chain|
791
+ filters.each do |filter|
792
+ callback = chain.find { |c| c.matches?(type, filter) }
793
+
794
+ if !callback && options[:raise]
795
+ raise ArgumentError, "#{type.to_s.capitalize} #{name} callback #{filter.inspect} has not been defined"
796
+ end
797
+
798
+ if callback && (options.key?(:if) || options.key?(:unless))
799
+ new_callback = callback.merge_conditional_options(chain, if_option: options[:if], unless_option: options[:unless])
800
+ chain.insert(chain.index(callback), new_callback)
801
+ end
802
+
803
+ chain.delete(callback)
804
+ end
805
+ target.set_callbacks name, chain
806
+ end
807
+ end
808
+
809
+ # Remove all set callbacks for the given event.
810
+ def reset_callbacks(name)
811
+ callbacks = get_callbacks name
812
+
813
+ self.descendants.each do |target|
814
+ chain = target.get_callbacks(name).dup
815
+ callbacks.each { |c| chain.delete(c) }
816
+ target.set_callbacks name, chain
817
+ end
818
+
819
+ set_callbacks(name, callbacks.dup.clear)
820
+ end
821
+
822
+ # Define sets of events in the object life cycle that support callbacks.
823
+ #
824
+ # define_callbacks :validate
825
+ # define_callbacks :initialize, :save, :destroy
826
+ #
827
+ # ===== Options
828
+ #
829
+ # * <tt>:terminator</tt> - Determines when a before filter will halt the
830
+ # callback chain, preventing following before and around callbacks from
831
+ # being called and the event from being triggered.
832
+ # This should be a lambda to be executed.
833
+ # The current object and the result lambda of the callback will be provided
834
+ # to the terminator lambda.
835
+ #
836
+ # define_callbacks :validate, terminator: ->(target, result_lambda) { result_lambda.call == false }
837
+ #
838
+ # In this example, if any before validate callbacks returns +false+,
839
+ # any successive before and around callback is not executed.
840
+ #
841
+ # The default terminator halts the chain when a callback throws +:abort+.
842
+ #
843
+ # * <tt>:skip_after_callbacks_if_terminated</tt> - Determines if after
844
+ # callbacks should be terminated by the <tt>:terminator</tt> option. By
845
+ # default after callbacks are executed no matter if callback chain was
846
+ # terminated or not. This option has no effect if <tt>:terminator</tt>
847
+ # option is set to +nil+.
848
+ #
849
+ # * <tt>:scope</tt> - Indicates which methods should be executed when an
850
+ # object is used as a callback.
851
+ #
852
+ # class Audit
853
+ # def before(caller)
854
+ # puts 'Audit: before is called'
855
+ # end
856
+ #
857
+ # def before_save(caller)
858
+ # puts 'Audit: before_save is called'
859
+ # end
860
+ # end
861
+ #
862
+ # class Account
863
+ # include ActiveSupport::Callbacks
864
+ #
865
+ # define_callbacks :save
866
+ # set_callback :save, :before, Audit.new
867
+ #
868
+ # def save
869
+ # run_callbacks :save do
870
+ # puts 'save in main'
871
+ # end
872
+ # end
873
+ # end
874
+ #
875
+ # In the above case whenever you save an account the method
876
+ # <tt>Audit#before</tt> will be called. On the other hand
877
+ #
878
+ # define_callbacks :save, scope: [:kind, :name]
879
+ #
880
+ # would trigger <tt>Audit#before_save</tt> instead. That's constructed
881
+ # by calling <tt>#{kind}_#{name}</tt> on the given instance. In this
882
+ # case "kind" is "before" and "name" is "save". In this context +:kind+
883
+ # and +:name+ have special meanings: +:kind+ refers to the kind of
884
+ # callback (before/after/around) and +:name+ refers to the method on
885
+ # which callbacks are being defined.
886
+ #
887
+ # A declaration like
888
+ #
889
+ # define_callbacks :save, scope: [:name]
890
+ #
891
+ # would call <tt>Audit#save</tt>.
892
+ #
893
+ # ===== Notes
894
+ #
895
+ # +names+ passed to +define_callbacks+ must not end with
896
+ # <tt>!</tt>, <tt>?</tt> or <tt>=</tt>.
897
+ #
898
+ # Calling +define_callbacks+ multiple times with the same +names+ will
899
+ # overwrite previous callbacks registered with #set_callback.
900
+ def define_callbacks(*names)
901
+ options = names.extract_options!
902
+
903
+ names.each do |name|
904
+ name = name.to_sym
905
+
906
+ ([self] + self.descendants).each do |target|
907
+ target.set_callbacks name, CallbackChain.new(name, options)
908
+ end
909
+
910
+ module_eval <<-RUBY, __FILE__, __LINE__ + 1
911
+ def _run_#{name}_callbacks(&block)
912
+ run_callbacks #{name.inspect}, &block
913
+ end
914
+
915
+ def self._#{name}_callbacks
916
+ get_callbacks(#{name.inspect})
917
+ end
918
+
919
+ def self._#{name}_callbacks=(value)
920
+ set_callbacks(#{name.inspect}, value)
921
+ end
922
+
923
+ def _#{name}_callbacks
924
+ __callbacks[#{name.inspect}]
925
+ end
926
+ RUBY
927
+ end
928
+ end
929
+
930
+ protected
931
+ def get_callbacks(name) # :nodoc:
932
+ __callbacks[name.to_sym]
933
+ end
934
+
935
+ def set_callbacks(name, callbacks) # :nodoc:
936
+ unless singleton_class.method_defined?(:__callbacks, false)
937
+ self.__callbacks = __callbacks.dup
938
+ end
939
+ self.__callbacks[name.to_sym] = callbacks
940
+ self.__callbacks
941
+ end
942
+ end
943
+ end
944
+ end