SuperState
Super Simple state machine for Ruby and Rails (designed for ActiveRecord, but hopefully easily extended to any ActiveModel)
Example
a simple multi-state model
class MyModel < ActiveRecord::Base
include SuperState
super_state :pending, :initial => true
super_state :processing
super_state :completed
super_state :failed
super_state_group :outstanding, ["pending", "processing"]
# first part of a two stage transition
# eg.
# def process
# start_processing!
# do_the_stuff
# complete_processing!
# end
#
state_transition :start_processing, :pending => :processing
# second part of a two stage transition
state_transition :complete_processing, :processing => :completed
# transition direct from pending to complete
state_transition :complete, :pending => :completed
# failed to process
state_transition :fail, :processing => :failed
# back to processing
state_transition :restart, :failed => :processing
end
we can also take arguments in our transitions
class Loan < ActiveRecord::Base
include SuperState
super_state :requested, :initial => true
super_state :disbursed
state_transition :disburse, :requested => :disbursed do |params|
self.disbursed_amount = params[:amount]
self.disbursed_date = params[:disbursed_date]
end
end
Copyright © 2010 [Matthew Rudy Jacobs], released under the MIT license