Created
December 8, 2010 12:07
-
-
Save kasperbn/733201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Workflow | |
class Specification | |
attr_accessor :before_transition_proc | |
private | |
def before_transition(&proc) | |
@before_transition_proc = proc | |
end | |
end | |
module WorkflowInstanceMethods | |
def process_event!(name, *args) | |
event = current_state.events[name.to_sym] | |
raise NoTransitionAllowed.new( | |
"There is no event #{name.to_sym} defined for the #{current_state} state") \ | |
if event.nil? | |
@halted_because = nil | |
@halted = false | |
check_transition(event) | |
run_before_transition(current_state, spec.states[event.transitions_to], name, *args) | |
return false if @halted | |
return_value = run_action(event.action, *args) || run_action_callback(event.name, *args) | |
return false if @halted | |
run_on_transition(current_state, spec.states[event.transitions_to], name, *args) | |
transition_value = transition(current_state, spec.states[event.transitions_to], name, *args) | |
return_value.nil? ? transition_value : return_value | |
end | |
private | |
def run_on_transition(from, to, event, *args) | |
instance_exec(from.name, to.name, event, *args, &spec.on_transition_proc) if spec.on_transition_proc | |
end | |
def run_before_transition(from, to, event, *args) | |
instance_exec(from.name, to.name, event, *args, &spec.before_transition_proc) if spec.before_transition_proc | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment