FiniteMachine is a minimal finite state machine with a straightforward and intuitive syntax.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
fm = FiniteMachine.new do
  initial :red

  event :ready, :red    => :yellow
  event :go,    :yellow => :green
  event :stop,  :green  => :red

  on_before(:ready) { |event| ... }
  on_after(:go)     { |event| ... }
  on_enter(:yellow) { |event| ... }
  on_transition     { |event| ... }
  on_exit(:green)   { |event| ... }
end

fm.current # => :red
fm.ready
fm.current # => :yellow

Powerful state machine features

ORM agnostic

Value flexibility and maintainabilty? Since FiniteMachine is just an object it allows for easy integration with the rest of the system.

Expressive syntax

If you want simplicity and readability, FiniteMachine provides a natural DSL for declaring events, callbacks and exception handlers.

Highly flexible

Transition guard conditions paired with dynamic conditional branching allow for modelling any state flows.

Getting started

  1. If you are using bundler then add the following line to your Gemfile

  2. gem 'finite_machine'
  3. And then execute command

  4. bundle

  5. Install FiniteMachine using rubygems package manager by running shell command:

$ gem install finite_machine