A tool for visualizing state machines defined using Statesman
Add this line to your application's Gemfile:
gem 'statesman_viz'
And then execute:
$ bundle
Or install it yourself as:
$ gem install statesman_viz
- Define a Statesman state machine like below.
class OrderStateMachine
include Statesman::Machine
state :pending, initial: true
state :checking_out
state :purchased
state :shipped
state :cancelled
state :failed
state :refunded
transition from: :pending, to: [:checking_out, :cancelled]
transition from: :checking_out, to: [:purchased, :cancelled]
transition from: :purchased, to: [:shipped, :failed]
transition from: :shipped, to: :refunded
guard_transition(to: :checking_out) do |order|
order.products_in_stock?
end
before_transition(from: :checking_out, to: :cancelled) do |order, transition|
order.reallocate_stock
end
before_transition(to: :purchased) do |order, transition|
PaymentService.new(order).submit
end
after_transition(to: :purchased) do |order, transition|
MailerService.order_confirmation(order).deliver
end
end
- Invoke
StatesmanViz
to generate the state machine diagram.
StatesmanViz.generate(OrderStateMachine)
- The output diagram is saved as
OrderStateMachine.png
in the current working directory.
TODO
Bug reports and pull requests are welcome on GitHub at https://github.com/sannim1/statesmanviz. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the StatesmanViz project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Thanks to Tadas Sasnauskas and the lovely people at Ruby Hacknight - London ❤️