statemachine, conditional transitions

Posted by astropanic on Stack Overflow See other posts from Stack Overflow or by astropanic
Published on 2010-05-29T10:31:41Z Indexed on 2010/05/30 8:32 UTC
Read the original article Hit count: 338

Filed under:
|

I'm currently using Workflow.

class Link < ActiveRecord::Base
  include Workflow
  workflow do
    state :new do
      event :process, :transitions_to => :checking #checking http_response_code & content_type
    end

    state :checking do
      event :process, :transitions_to => :fetching_links # fetching all links
    end
    state :fetching_links do
      event :process, :transitions_to => :checking #ready for next check
    end
  end
end

Now, I can do:

l = Link.new
l.process!
l.process!
l.process!
l.process!
# n times l.process! (in a loop, or cron job for example)

But it can happens, some link will not respond or give me an invalid response durning the checking process.

How I can conditionally switch to another state ?

I mean something like this:

class Link < ActiveRecord::Base
  include Workflow
  workflow do
    state :new do
      event :process, :transitions_to => :checking #checking http_response_code & content_type
    end

    state :checking do
      event :process, :transitions_to => :fetching_links # if all is fine
      event :process, :transitions_to => :failded # if something goes wrong
    end
    state :fetching_links do
      event :process, :transitions_to => :checking #ready for next check
    end
  end
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about statemachine