You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the8472 edited this page Aug 30, 2012
·
1 revision
Scenario: A state-machine is assembled by modular code. Due to some reasons a module might be included twice.
How to add callbacks without registering them twice?
Since StateMachine::Callback does not have a getter for the methods it calls one has to define the callback in a uniquely identifiable way, i.e. the DSL cannot be used.
Solution (using Rails here)
moduleMyModuleextendActiveSupport::ConcernCALLBACK=StateMachine::Callback.new:after,:from_state=>:to_state,:do=>:some_methodincludeddocallbacks=state_machines[:my_machine].callbacks[:after]callbacks << CALLBACKunlesscallbacks.include?CALLBACKenddefsome_method(object,transaction)# your callback code hereendend
Instantiating the callback only once allows for identity comparison and thus checking if the callback is already present in the registered callbacks of the state machine without inspecting the callback itself.