Created
June 15, 2012 15:45
-
-
Save electronicbites/2937134 to your computer and use it in GitHub Desktop.
play with state_machine gem
This file contains hidden or 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
class TestState | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
state_machine :state, initial: :authorized do | |
event :capture_money do | |
transition :authorized => :captured | |
end | |
event :error_with_card do | |
transition :authorized => :error | |
end | |
event :retry do | |
transition :error => :foo | |
transition :authorized => :bang | |
transition :bang => :error | |
end | |
before_transition :authorized => :captured, :do => :charge | |
after_transition :authorized => :captured, :do => :did_capture | |
after_transition :authorized => :error, :do => :show_error | |
end | |
def try_capture_money | |
capture_money | |
error_with_card if state == "authorized" | |
end | |
def show_error | |
puts "show error: state: #{state}" | |
end | |
def charge | |
puts "charge" | |
false | |
end | |
def did_capture | |
puts "did_capture" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment