stateoscope 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a78509d1a8ca0900d81789aa74bcee3fb47dab8
4
+ data.tar.gz: a12f172369063cd9cb94afbd5f117fc4b52f1d85
5
+ SHA512:
6
+ metadata.gz: 1d47e9dda6a1525d5d408be3a2244ec5c365be3e3666e848acda6c3ba6fb3b1661602f8366fbbbd9ff7dc5a7707760afa028f79b1be866593b02d858110035d4
7
+ data.tar.gz: 7f724a7ce3b2288f08d55b5d16795110fa1e080faf238b663ac06c115467e83b1c1d96a25009e325631533c17e68426c15b78941d0158eb35b04b2114099d628
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stateoscope.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Patrick Oscity
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Stateoscope
2
+
3
+ Visualize State Machines using GraphViz
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'stateoscope'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ To generate a state machine visualization for your `Model`, run
20
+
21
+ ```ruby
22
+ rake 'stateoscope:visualize[Model]'
23
+ ```
24
+
25
+ If you have multiple state machines defined on your model, you can pass the name
26
+ of the state machine as second parameter
27
+
28
+ ```ruby
29
+ rake 'stateoscope:visualize[Model,specific_state_machine]'
30
+ ```
31
+
32
+ In both cases, a PDF file containing the graph visualization will be saved to
33
+ the current directory.
34
+
35
+ ## Integrations
36
+
37
+ Stateoscope is currently integrated with the following state machine gems:
38
+
39
+ * [AASM](https://github.com/aasm/aasm)
40
+
41
+ ## Development
42
+
43
+ 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.
44
+
45
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stateoscope.
50
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "stateoscope"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ require 'stateoscope/version'
2
+ require 'stateoscope/integrations'
3
+ require 'stateoscope/visualizer'
4
+
5
+ require 'stateoscope/railtie' if defined?(Rails)
6
+
7
+ module Stateoscope
8
+ def self.visualize(klass, options = {})
9
+ state_machine_name = options.fetch(:state_machine_name, nil)
10
+ integration = Integrations.new_for(klass, state_machine_name)
11
+ filename = options.fetch(:filename, filename_for(integration))
12
+ Visualizer.new(integration.graph).output(filename)
13
+ end
14
+
15
+ def self.filename_for(integration)
16
+ "#{integration.full_state_machine_name}-#{Time.now.utc.strftime('%Y%m%d%H%M%s')}"
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ require 'ostruct'
2
+
3
+ module Stateoscope
4
+ class Graph
5
+ attr_accessor :initial_state, :states, :final_states, :transitions
6
+
7
+ def initialize
8
+ @states = []
9
+ @final_states = []
10
+ @transitions = []
11
+ end
12
+
13
+ def add_state(state)
14
+ @states << state
15
+ end
16
+
17
+ def add_transition(from, to, event)
18
+ @transitions << OpenStruct.new(from: from, to: to, event: event)
19
+ end
20
+
21
+ def detect_final_states!
22
+ @final_states = states - transitions.map(&:from)
23
+ end
24
+
25
+ def final_state?(state)
26
+ @final_states.include?(state)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ require 'stateoscope/graph'
2
+ require 'stateoscope/integrations/aasm'
3
+
4
+ module Stateoscope
5
+ module Integrations
6
+ def self.new_for(klass, state_machine_name)
7
+ if klass.ancestors.include?(::AASM)
8
+ ::Stateoscope::Integrations::AASM.new(klass, state_machine_name)
9
+ else
10
+ fail NotImplementedError, "unsupported state machine implementation"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,39 @@
1
+ module Stateoscope
2
+ module Integrations
3
+ class AASM < Struct.new(:klass, :state_machine_name)
4
+ def graph
5
+ graph = Graph.new
6
+
7
+ graph.initial_state = state_machine.initial_state.to_s
8
+
9
+ state_machine.states.each do |state|
10
+ graph.add_state(state.name.to_s)
11
+ end
12
+
13
+ state_machine.events.each do |event|
14
+ event.transitions.each do |transition|
15
+ graph.add_transition(transition.from.to_s, transition.to.to_s, event.name.to_s)
16
+ end
17
+ end
18
+
19
+ graph.detect_final_states!
20
+
21
+ graph
22
+ end
23
+
24
+ def full_state_machine_name
25
+ [
26
+ "aasm",
27
+ klass.name,
28
+ state_machine_name
29
+ ].compact.join('-').dasherize
30
+ end
31
+
32
+ private
33
+
34
+ def state_machine
35
+ klass.public_send(:aasm, state_machine_name.presence)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ module Stateoscope
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ Dir.glob File.expand_path('../../tasks/*.rake', __FILE__) do |rake_task|
5
+ load rake_task
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Stateoscope
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,69 @@
1
+ require 'ruby-graphviz'
2
+
3
+ module Stateoscope
4
+ class Visualizer < Struct.new(:graph)
5
+ def initialize(graph)
6
+ super
7
+ parse_graph!
8
+ end
9
+
10
+ def parse_graph!
11
+ @viz = GraphViz.new(:G, type: 'digraph')
12
+ add_entry_point
13
+ add_states
14
+ add_entry_point_transition
15
+ add_state_transitions
16
+ end
17
+
18
+ def output(filename)
19
+ @viz.output(pdf: "#{filename}.pdf")
20
+ end
21
+
22
+ private
23
+
24
+ ENTRY_POINT = '__ENTRY_POINT__'
25
+
26
+ def filename
27
+ "#{integration.name}-#{state_machine_name}-#{Time.now.utc.strftime('%Y%m%d%H%M%s')}"
28
+ end
29
+
30
+ def add_entry_point
31
+ return unless graph.initial_state
32
+ add_node(ENTRY_POINT, shape: 'circle', label: '', style: 'filled', color: 'black', fixedsize: true, width: 0.3)
33
+ end
34
+
35
+ def add_states
36
+ graph.states.each do |state|
37
+ add_node(state, peripheries: graph.final_state?(state) ? 2 : 1)
38
+ end
39
+ end
40
+
41
+ def add_entry_point_transition
42
+ return unless graph.initial_state
43
+ add_edge(ENTRY_POINT, graph.initial_state)
44
+ end
45
+
46
+ def add_state_transitions
47
+ graph.transitions.each do |transition|
48
+ add_edge(transition.from, transition.to, transition.event)
49
+ end
50
+ end
51
+
52
+ def add_node(label, options = {})
53
+ options.reverse_merge!(shape: 'ellipse')
54
+ options.reverse_merge!(global_options)
55
+ @viz.add_nodes(label, options)
56
+ end
57
+
58
+ def add_edge(from, to, label = nil, options = {})
59
+ options.reverse_merge!(label: label) if label
60
+ options.reverse_merge!(fontsize: 9, fontcolor: '#888888', labelangle: 45)
61
+ options.reverse_merge!(global_options)
62
+ @viz.add_edges(from, to, options)
63
+ end
64
+
65
+ def global_options
66
+ {fontname: 'Helvetica', fontsize: 10}
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+ namespace :stateoscope do
2
+ desc 'visualize state machine for a given class'
3
+ task :visualize, [:class, :state_machine_name] => :environment do |t, args|
4
+ fail ArgumentError 'missing required argument <class>' unless args[:class].present?
5
+ klass = args[:class].classify.constantize
6
+ Stateoscope.visualize(klass, state_machine_name: args[:state_machine_name])
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stateoscope/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stateoscope"
8
+ spec.version = Stateoscope::VERSION
9
+ spec.authors = ["Patrick Oscity"]
10
+ spec.email = ["patrick.oscity@gmail.com"]
11
+
12
+ spec.summary = %q{State Machine Visualizer}
13
+ spec.description = %q{Visualize State Machines using GraphViz}
14
+ spec.homepage = "https://github.com/padde/stateoscope"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "ruby-graphviz"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stateoscope
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Oscity
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-graphviz
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Visualize State Machines using GraphViz
70
+ email:
71
+ - patrick.oscity@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/stateoscope.rb
86
+ - lib/stateoscope/graph.rb
87
+ - lib/stateoscope/integrations.rb
88
+ - lib/stateoscope/integrations/aasm.rb
89
+ - lib/stateoscope/railtie.rb
90
+ - lib/stateoscope/version.rb
91
+ - lib/stateoscope/visualizer.rb
92
+ - lib/tasks/stateoscope.rake
93
+ - stateoscope.gemspec
94
+ homepage: https://github.com/padde/stateoscope
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: State Machine Visualizer
118
+ test_files: []