ryanza / stateflow

State machine that allows dynamic transitions for business workflows
MIT License
123 stars 41 forks source link

Handling of current state when persisted state is invalid #20

Open pascal-za opened 13 years ago

pascal-za commented 13 years ago

Given

class Robot
  include Stateflow

  stateflow do
    state :red, :green
  end
end

robot = Robot.new
def robot.load_from_persistence
  :some_invalid_state
end
> robot.green?
NoMethodError: undefined method `name' for nil:NilClass
    from .../stateflow-0.4.2/lib/stateflow.rb:25:in `new?'

The error isn't immediately obvious to the user/developer.

Possible solutions:

Let me know what direction you want to go with this and I'll submit a patch.

cgriego commented 13 years ago

I ran into this today. An unpersisted model was set with an invalid state to prove that the validations would not allow it and I later added an :if => :my_state? validation conditional and then validating the invalid model raised an exception.