Skip to content

Instantly share code, notes, and snippets.

@kasperbn
Created December 8, 2010 12:07
Show Gist options
  • Save kasperbn/733201 to your computer and use it in GitHub Desktop.
Save kasperbn/733201 to your computer and use it in GitHub Desktop.
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