Skip to content

Commit bfae188

Browse files
authored
feat: depend on state_machines-diagram (#19)
1 parent 10f8916 commit bfae188

File tree

10 files changed

+317
-205
lines changed

10 files changed

+317
-205
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Gemfile.lock
77
InstalledFiles
88
_yardoc
99
coverage
10-
doc/
10+
doc/*
11+
!doc/switch.svg
1112
lib/bundler/man
1213
pkg
1314
rdoc

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ in the class. The generated files will use an output filename of the format
6969
For examples of actual images generated using this task, see those under the
7070
examples folder.
7171

72+
#### Example output
73+
74+
Generate the sample diagram used in this README:
75+
76+
```bash
77+
bundle exec ruby script/generate_example.rb
78+
```
79+
80+
![Switch state machine](doc/switch.svg)
81+
7282
### Interactive graphs
7383

7484
Jean Bovet's [Visual Automata Simulator](https://github.com/NimaGhaedsharafi/VAS)

doc/switch.svg

Lines changed: 84 additions & 0 deletions
Loading

lib/state_machines-graphviz.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
# frozen_string_literal: true
22

3+
require 'state_machines-diagram'
34
require 'state_machines/graphviz'
5+
require 'state_machines/graphviz/renderer'
6+
7+
# Set the renderer to use graphviz
8+
StateMachines::Machine.renderer = StateMachines::Graphviz::Renderer

lib/state_machines/graphviz.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,38 @@
22

33
require 'state_machines'
44
require 'graphviz'
5-
require 'state_machines/graphviz/monkeypatch'
65
require 'state_machines/graphviz/graph'
6+
require 'state_machines/graphviz/renderer'
77
require 'state_machines/graphviz/version'
88

9+
module StateMachines
10+
module Graphviz
11+
module_function
12+
13+
def draw(class_names, options = {})
14+
if class_names.nil? || class_names.to_s.split(',').empty?
15+
raise ArgumentError, 'At least one class must be specified'
16+
end
17+
18+
# Load any files
19+
if (files = options.delete(:file))
20+
files.split(',').each { |file| require file }
21+
end
22+
23+
class_names.to_s.split(',').each do |class_name|
24+
# Navigate through the namespace structure to get to the class
25+
klass = Object
26+
class_name.split('::').each do |name|
27+
klass = klass.const_defined?(name) ? klass.const_get(name) : klass.const_missing(name)
28+
end
29+
30+
# Draw each of the class's state machines
31+
klass.state_machines.each_value do |machine|
32+
machine.draw(**options)
33+
end
34+
end
35+
end
36+
end
37+
end
38+
939
require 'state_machines/tasks/railtie' if defined?(Rails)

lib/state_machines/graphviz/monkeypatch.rb

Lines changed: 0 additions & 184 deletions
This file was deleted.

0 commit comments

Comments
 (0)