Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
doc/*
!doc/switch.svg
lib/bundler/man
pkg
rdoc
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ in the class. The generated files will use an output filename of the format
For examples of actual images generated using this task, see those under the
examples folder.

#### Example output

Generate the sample diagram used in this README:

```bash
bundle exec ruby script/generate_example.rb
```

![Switch state machine](doc/switch.svg)

### Interactive graphs

Jean Bovet's [Visual Automata Simulator](https://github.com/NimaGhaedsharafi/VAS)
Expand Down
84 changes: 84 additions & 0 deletions doc/switch.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions lib/state_machines-graphviz.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# frozen_string_literal: true

require 'state_machines-diagram'
require 'state_machines/graphviz'
require 'state_machines/graphviz/renderer'

# Set the renderer to use graphviz
StateMachines::Machine.renderer = StateMachines::Graphviz::Renderer
32 changes: 31 additions & 1 deletion lib/state_machines/graphviz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,38 @@

require 'state_machines'
require 'graphviz'
require 'state_machines/graphviz/monkeypatch'
require 'state_machines/graphviz/graph'
require 'state_machines/graphviz/renderer'
require 'state_machines/graphviz/version'

module StateMachines
module Graphviz
module_function

def draw(class_names, options = {})
if class_names.nil? || class_names.to_s.split(',').empty?
raise ArgumentError, 'At least one class must be specified'
end

# Load any files
if (files = options.delete(:file))
files.split(',').each { |file| require file }
end

class_names.to_s.split(',').each do |class_name|
# Navigate through the namespace structure to get to the class
klass = Object
class_name.split('::').each do |name|
klass = klass.const_defined?(name) ? klass.const_get(name) : klass.const_missing(name)
end

# Draw each of the class's state machines
klass.state_machines.each_value do |machine|
machine.draw(**options)
end
end
end
end
end

require 'state_machines/tasks/railtie' if defined?(Rails)
184 changes: 0 additions & 184 deletions lib/state_machines/graphviz/monkeypatch.rb

This file was deleted.

Loading