piotrmurach / finite_machine

A minimal finite state machine with a straightforward syntax.
https://piotrmurach.github.io/finite_machine/
MIT License
808 stars 38 forks source link

Terminal state #49

Closed mits87 closed 6 years ago

mits87 commented 7 years ago

Hi,

I have a little problem because I would like read terminated state but I can't If:

terminal [:failed, :finished]

  1. I don't see any callback for terminated
state_machine.on_terminated do |event|
...
end
  1. If I have few states of terminal then method terminated? is always false. In my opinion if one of states is in terminal array then should be true. What are you thinking about it?

Thanks Peter

piotrmurach commented 7 years ago

Hi, thanks for using the library.

Terminal state is not different from any other state therefore you should be able to use standard callbacks:

state_machine.on_enter :failed do |event|
...
end

Currently the terminal allows to specify only a single terminal state. Expanding this to include more than one terminal state is a good idea.

mits87 commented 7 years ago

Thank you for your answer. It would be great if the terminal could take more states than one.

PS. Is it possible do something like this:

state_machine.on_enter(:failed, :finished) do |event|
...
end

?

Because I didn't find anywhere this case.

piotrmurach commented 7 years ago

I agree it would be great. Currently there are few quite important issues to address first.

The callbacks currently work with single state names due to dynamic method definitions. However, the underlying code accepts any number of arguments so I would just test if your callback gets fired for two different states.

Unfortunately I'm without internet now and my provider is less than helpful restoring it so may be unavailable for couple days.

piotrmurach commented 6 years ago

This was actually always implemented, the terminal expects comma delimited symbols:

terminal :failed, :finished

I've updated docs to explain multiple terminal states.