Skip to content

Instantly share code, notes, and snippets.

@ikaruga777
Last active June 12, 2022 17:06
Show Gist options
  • Save ikaruga777/a8a295b235c01457eab3f74bb94b22cb to your computer and use it in GitHub Desktop.
Save ikaruga777/a8a295b235c01457eab3f74bb94b22cb to your computer and use it in GitHub Desktop.
statesman2mermaid
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
end
module Statesman
module Machine
def self.included(base)
base.extend(ClassMethods)
puts "stateDiagram"
end
module ClassMethods
def state(state, initial: false)
puts "[*] --> #{state.to_s}" if initial
end
def transition(from:, to:)
Array(to).each do |state|
puts "#{from.to_s} --> #{state.to_s}"
end
end
end
end
end
load File.join(__dir__, ARGV)
$ ruby 1_statesman2mermaid.rb 0_order_state_machine.rb 
stateDiagram
[*] --> pending
pending --> checking_out
pending --> cancelled
checking_out --> purchased
checking_out --> cancelled
purchased --> shipped
purchased --> failed
shipped --> refunded
stateDiagram
[*] --> pending
pending --> checking_out
pending --> cancelled
checking_out --> purchased
checking_out --> cancelled
purchased --> shipped
purchased --> failed
shipped --> refunded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment