【发布时间】:2012-10-03 22:17:32
【问题描述】:
是否可以在类方法上使用ActiveSupport::Callbacks?更具体地说,在下面的模块中,您会将include ActiveSupport::Callbacks 放在哪里以使define_callbacks 和run_callbacks 共享相同的状态?
module Handler
extend ActiveSupport::Concern
# If you put it here, define_callbacks is defined but run_callbacks is not
# include ActiveSupport::Callbacks
included do
class_eval do
define_callbacks :handle
end
end
module ClassMethods
# If you put it here, run_callbacks is defined but define_callbacks is not
# include ActiveSupport::Callbacks
def handle(task)
run_callbacks :handle do
p task
end
end
end
end
class HandlerExample
include Handler
end
更新
如果有可能,但我无法确定,那肯定不是 ActiveSupport::Callbacks 的设计目的。最好在#handle中创建一个无状态实例对象。
【问题讨论】:
标签: ruby-on-rails ruby callback activesupport eigenclass