클래스 메서드에 ActiveSupport::Callbacks
을 사용할 수 있습니까? 보다 구체적으로 말하면, 아래 모듈에서 include ActiveSupport::Callbacks
을 넣으면 define_callbacks
과 run_callbacks
은 같은 상태를 공유합니까? 그것이 내가 확인하지 못하고있는, 가능하다면ActiveSupport :: 클래스 메서드에 대한 콜백
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
에 상태없는 인스턴스 개체를 만드는 것이 가장 좋습니다.
나는 그것들을 eigenclass에 넣으려고했으나 그것도 작동하지 않았다. 객체 ID를 살펴보면'ActiveSupport :: Concern'과'ActiveSupport :: Callbacks'가 상호 작용하는 방식이 클래스 메소드에서 잘 작동하지 않는 것처럼 보입니다. 오늘 아침 나는 당신이 제안한 것과 같은 결론에 도달했고 핸들러를위한 인스턴스 객체를 만들었다. 태스크 외부에는 상태가 없기 때문에 낭비입니다. 그러나 클래스 메소드에 잘 작동하는 콜백 구현을 작성하는 것보다 더 깔끔하고 버그 발생이 적습니다. – Sim