2010-05-29 5 views
0

J'utilise actuellement Workflow.statemachine, transitions conditionnelles

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 

Maintenant, je peux faire:

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

Mais il peut se produit, un lien ne répond pas ou me donner une réponse invalide Durning la procédure de vérification.

Comment puis-je passer à un autre état sous condition?

je veux dire quelque chose comme ceci:

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 

Répondre

1

Je ne sais pas où vos actions sont exécutées. on_entry ou on_exit d'un état. Ce que je supposais serait le plus gentil, c'est que lors de la transition vers mon prochain état, si une erreur se produisait, je pourrais passer à un autre état, par ex. :failed. Je n'ai pas essayé cela, mais j'ai trouvé que vous pouvez passer à un état, ce qui signifie que la transition a échoué.

Par exemple:

state :checking do 
    event :process, :transitions_to => :fetching_links do 
    fetch_links 
    halt if fetch_links_failed? 
    end 
end 

Vous l'utiliser comme suit:

l.process 
l.halted? => true # if fetching_links failed 

Est-ce que l'aide?