Skip to content

Instantly share code, notes, and snippets.

@unakatsuo
Created August 23, 2011 17:52
Show Gist options
  • Save unakatsuo/1165990 to your computer and use it in GitHub Desktop.
Save unakatsuo/1165990 to your computer and use it in GitHub Desktop.
Simple state transition check code.
class StateTrack
def initialize
@state = :initialized
end
# Expected state transition:
# :initialized -> :starting -> :running -> :shuttingdown -> :terminated
def process_event(ev, *args)
case [ev, @state]
when [:on_start, :initialized]
@state = :starting
when [:on_run, :starting]
@state = :running
when [:on_shutdown, :running]
@state = :shuttingdown
when [:on_terminate, :shuttingdown]
@state = :terminated
else
raise "Unknown state transition: #{ev} #{@state}"
end
end
end
s = new StateTrack
s.process_event(:on_start)
s.process_event(:on_run)
s.process_event(:on_shutdown)
s.process_event(:on_terminate)
s2 = new StateTrack
s.process_event(:on_start)
s.process_event(:on_shutdown) # raise error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment