nxt_state_machine 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 37f01d32a03c0c09b47018719f8b05b2439497034807ad0e6fa098a6d7d832ed
4
- data.tar.gz: '02682339637194f92d32240b759e51cab84cd590a789af74c66109e27d5ce912'
3
+ metadata.gz: 89b1ad3a07fcc60e4b3541b770f1e1f287713e973d3a038f3496be0a145006c5
4
+ data.tar.gz: f213f914ffe2ff39a0d90d8c61f574857b312c9065e1241e9e09dce16cf6cc3a
5
5
  SHA512:
6
- metadata.gz: e777bbfeffb013b7591d0d2e18169a7311d324aefe7ad2a97f181b53926f6d6aa0f2a3cd08728ca1e544ac21b5ca5893ae4479c39ff93e54ac083a0b8399cdb5
7
- data.tar.gz: 53ff5dd521de00050a1fe553c7dfd560f1cc8fde10e8cfc1cdea571873b22f4ac3be103ec1863314b64da0a04d95244a19f655e41794395dbcc4b8708f720a8e
6
+ metadata.gz: 48b17f521db0a19ccc500fff321b459f4e962875792e047923185a45691c20b81bfec5bbab03b77cf6eaeb10be2b8191ec73bfbc4d4b05c9e518389b1d9cc497
7
+ data.tar.gz: 286382b6dd9512abc0a0b1c1f177196edab5aced50bc8888d0febf4a58a86697ac152b8a7617c238f3268cbbada15f9767248b333264cf1608421d6a9b9c2b93
@@ -16,11 +16,17 @@ jobs:
16
16
  steps:
17
17
  - checkout
18
18
 
19
+ - run:
20
+ name: Install apt dependencies
21
+ command: |
22
+ sudo apt update -q \
23
+ && sudo apt upgrade -q \
24
+ && sudo apt install -qq graphviz
19
25
  # Download and cache dependencies
20
26
  - restore_cache:
21
27
  keys:
22
28
  - v1-dependencies-{{ checksum "Gemfile.lock" }}
23
-
29
+
24
30
  - run: gem install bundler --version $BUNDLER_VERSION
25
31
 
26
32
  - run:
@@ -53,4 +59,4 @@ jobs:
53
59
  path: /tmp/test-results
54
60
  - store_artifacts:
55
61
  path: /tmp/test-results
56
- destination: test-results
62
+ destination: test-results
@@ -0,0 +1,7 @@
1
+ # v0.1.8 2020-0-05
2
+
3
+ ### Added
4
+
5
+ - [internal] Allow to draw state machine graph with rake task
6
+
7
+ [Compare v0.1.7...v0.1.8](https://github.com/nxt-insurance/nxt_state_machine/compare/v0.1.7...v0.1.8)
@@ -1,37 +1,38 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_state_machine (0.1.7)
4
+ nxt_state_machine (0.1.8)
5
5
  activesupport
6
6
  nxt_registry (~> 0.1.3)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (6.0.2.2)
12
- activesupport (= 6.0.2.2)
13
- activerecord (6.0.2.2)
14
- activemodel (= 6.0.2.2)
15
- activesupport (= 6.0.2.2)
16
- activesupport (6.0.2.2)
11
+ activemodel (6.0.3.1)
12
+ activesupport (= 6.0.3.1)
13
+ activerecord (6.0.3.1)
14
+ activemodel (= 6.0.3.1)
15
+ activesupport (= 6.0.3.1)
16
+ activesupport (6.0.3.1)
17
17
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
18
  i18n (>= 0.7, < 2)
19
19
  minitest (~> 5.1)
20
20
  tzinfo (~> 1.1)
21
- zeitwerk (~> 2.2)
21
+ zeitwerk (~> 2.2, >= 2.2.2)
22
22
  coderay (1.1.2)
23
23
  concurrent-ruby (1.1.6)
24
24
  diff-lcs (1.3)
25
25
  i18n (1.8.2)
26
26
  concurrent-ruby (~> 1.0)
27
27
  method_source (1.0.0)
28
- minitest (5.14.0)
28
+ minitest (5.14.1)
29
29
  nxt_registry (0.1.5)
30
30
  activesupport
31
- pry (0.13.0)
31
+ pry (0.13.1)
32
32
  coderay (~> 1.1)
33
33
  method_source (~> 1.0)
34
34
  rake (12.3.3)
35
+ rexml (3.2.4)
35
36
  rspec (3.9.0)
36
37
  rspec-core (~> 3.9.0)
37
38
  rspec-expectations (~> 3.9.0)
@@ -47,9 +48,11 @@ GEM
47
48
  rspec-support (3.9.0)
48
49
  rspec_junit_formatter (0.4.1)
49
50
  rspec-core (>= 2, < 4, != 2.12.0)
51
+ ruby-graphviz (1.2.5)
52
+ rexml
50
53
  sqlite3 (1.4.2)
51
54
  thread_safe (0.3.6)
52
- tzinfo (1.2.6)
55
+ tzinfo (1.2.7)
53
56
  thread_safe (~> 0.1)
54
57
  zeitwerk (2.3.0)
55
58
 
@@ -64,6 +67,7 @@ DEPENDENCIES
64
67
  rake (~> 12.0)
65
68
  rspec (~> 3.0)
66
69
  rspec_junit_formatter
70
+ ruby-graphviz
67
71
  sqlite3
68
72
 
69
73
  BUNDLED WITH
data/README.md CHANGED
@@ -393,14 +393,6 @@ class Article < ApplicationRecord
393
393
  end
394
394
  ```
395
395
 
396
-
397
- ## TODO
398
- - Decide on how to include state methods in model classes
399
- - Should we clone machines for each context?
400
- - What about inheritance? => What would be the expected behaviour? (dup vs. no dup)
401
- => Might also make sense to walk the ancestors chain and collect configure blocks
402
-
403
-
404
396
  ## Development
405
397
 
406
398
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -31,6 +31,7 @@ require "nxt_state_machine/state_machine"
31
31
  require "nxt_state_machine/integrations/active_record"
32
32
  require "nxt_state_machine/integrations/attr_accessor"
33
33
  require "nxt_state_machine/integrations/hash"
34
+ require "nxt_state_machine/graph"
34
35
 
35
36
  module NxtStateMachine
36
37
  module ClassMethods
@@ -41,6 +41,10 @@ module NxtStateMachine
41
41
  event_transitions.resolve!(state).present?
42
42
  end
43
43
 
44
+ def to_s
45
+ "#{self.class.name}[:#{name}]"
46
+ end
47
+
44
48
  private
45
49
 
46
50
  def configure(&block)
@@ -0,0 +1,77 @@
1
+ module NxtStateMachine
2
+ class Graph
3
+ def initialize(state_machines, **options)
4
+ @state_machines = state_machines
5
+ @options = default_options.merge(**options)
6
+ end
7
+
8
+ def draw
9
+ require 'ruby-graphviz'
10
+
11
+ state_machines.each do |_, state_machine|
12
+ add_nodes(state_machine)
13
+ add_edges(state_machine)
14
+ end
15
+
16
+ filename = File.join(options[:path], "#{options[:name]}.#{options[:format]}")
17
+
18
+ graph.output options[:format] => filename
19
+
20
+ puts '----------------------------------------------'
21
+ puts 'Please run the following to open the generated file:'
22
+ puts "open '#{filename}'"
23
+ puts '----------------------------------------------'
24
+
25
+ graph
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :options, :state_machines
31
+
32
+ def graph
33
+ @graph ||= ::GraphViz.new(
34
+ 'G',
35
+ rankdir: options[:orientation] == 'landscape' ? 'LR' : 'TB',
36
+ ratio: options[:ratio]
37
+ )
38
+ end
39
+
40
+ def add_nodes(state_machine)
41
+ binding.pry
42
+ state_machine.states.values.each do |state|
43
+ add_node(state)
44
+ end
45
+ end
46
+
47
+ def add_node(state)
48
+ node_options = {
49
+ label: state.to_s,
50
+ width: '1',
51
+ height: '1',
52
+ shape: 'ellipse'
53
+ }
54
+
55
+ graph.add_nodes(state.to_s, node_options)
56
+ end
57
+
58
+ def add_edges(state_machine)
59
+ state_machine.events.values.each do |event|
60
+ event.event_transitions.values.each do |transition|
61
+ graph.add_edges(transition.from.to_s, transition.to.to_s, label: event.name)
62
+ end
63
+ end
64
+ end
65
+
66
+ def default_options
67
+ {
68
+ name: 'state_machine',
69
+ path: '.',
70
+ orientation: 'landscape',
71
+ ratio: 'fill',
72
+ format: 'png',
73
+ font: 'Helvetica'
74
+ }
75
+ end
76
+ end
77
+ end
@@ -15,7 +15,7 @@ module NxtStateMachine
15
15
  @initial_state = nil
16
16
  end
17
17
 
18
- attr_reader :class_context, :transitions, :events, :options, :callbacks, :name, :error_callback_registry, :defuse_registry
18
+ attr_reader :class_context, :events, :options, :callbacks, :name, :error_callback_registry, :defuse_registry
19
19
  attr_accessor :initial_state
20
20
 
21
21
  def get_state_with(method = nil, &block)
@@ -90,6 +90,7 @@ module NxtStateMachine
90
90
  class_context.define_method event_name do |*args, **opts|
91
91
  event.state_machine.can_transition!(name, event.state_machine.current_state_name(self))
92
92
  transition = event.event_transitions.resolve(event.state_machine.current_state_name(self))
93
+ # Transition is build every time and thus should be thread safe!
93
94
  transition.build_transition(event_name, self, set_state_method, *args, **opts)
94
95
  end
95
96
  end
@@ -1,3 +1,3 @@
1
1
  module NxtStateMachine
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -0,0 +1,8 @@
1
+ class Railtie < Rails::Railtie
2
+ railtie_name :nxt_state_machine
3
+
4
+ rake_tasks do
5
+ path = File.expand_path(__dir__)
6
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ namespace :graph do
2
+ desc 'draw the graph of a state machine'
3
+ task :draw, [:state_machine_class] => [:environment] do |_, args|
4
+ state_machine_class = Object.const_get(args.fetch(:state_machine_class))
5
+ state_machine = state_machine_class.state_machine
6
+ NxtStateMachine::Graph.new(state_machine).draw
7
+ end
8
+ end
@@ -43,4 +43,5 @@ Gem::Specification.new do |spec|
43
43
  spec.add_development_dependency "pry"
44
44
  spec.add_development_dependency "activerecord"
45
45
  spec.add_development_dependency "sqlite3"
46
+ spec.add_development_dependency "ruby-graphviz"
46
47
  end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nxt_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Robecke
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2020-03-26 00:00:00.000000000 Z
14
+ date: 2020-06-08 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -125,6 +125,20 @@ dependencies:
125
125
  - - ">="
126
126
  - !ruby/object:Gem::Version
127
127
  version: '0'
128
+ - !ruby/object:Gem::Dependency
129
+ name: ruby-graphviz
130
+ requirement: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
128
142
  description: A state machine library that can be used with ActiveRecord or in plain
129
143
  ruby and should be easy to customize for other integrations
130
144
  email:
@@ -139,6 +153,7 @@ files:
139
153
  - ".rspec"
140
154
  - ".ruby-version"
141
155
  - ".travis.yml"
156
+ - CHANGELOG.md
142
157
  - Gemfile
143
158
  - Gemfile.lock
144
159
  - LICENSE.txt
@@ -166,6 +181,7 @@ files:
166
181
  - lib/nxt_state_machine/event.rb
167
182
  - lib/nxt_state_machine/event/names.rb
168
183
  - lib/nxt_state_machine/event_registry.rb
184
+ - lib/nxt_state_machine/graph.rb
169
185
  - lib/nxt_state_machine/integrations/active_record.rb
170
186
  - lib/nxt_state_machine/integrations/attr_accessor.rb
171
187
  - lib/nxt_state_machine/integrations/hash.rb
@@ -179,7 +195,10 @@ files:
179
195
  - lib/nxt_state_machine/transition/proxy.rb
180
196
  - lib/nxt_state_machine/transition/store.rb
181
197
  - lib/nxt_state_machine/version.rb
198
+ - lib/railtie.rb
199
+ - lib/tasks/draw_graph.rake
182
200
  - nxt_state_machine.gemspec
201
+ - state_machine.png
183
202
  homepage: https://github.com/nxt-insurance/nxt_state_machine
184
203
  licenses:
185
204
  - MIT