Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
padde committed Oct 7, 2015
0 parents commit 6267149
Show file tree
Hide file tree
Showing 20 changed files with 344 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: ruby
rvm:
- 2.2.2
before_install: gem install bundler -v 1.10.6
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in stateoscope.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Patrick Oscity

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Stateoscope

Visualize State Machines using GraphViz

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'stateoscope'
```

And then execute:

$ bundle

## Usage

To generate a state machine visualization for your `Model`, run

```ruby
rake 'stateoscope:visualize[Model]'
```

If you have multiple state machines defined on your model, you can pass the name
of the state machine as second parameter

```ruby
rake 'stateoscope:visualize[Model,specific_state_machine]'
```

In both cases, a PDF file containing the graph visualization will be saved to
the current directory.

## Integrations

Stateoscope is currently integrated with the following state machine gems:

* [AASM](https://github.com/aasm/aasm)

## Development

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.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/stateoscope.

6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "stateoscope"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start
7 changes: 7 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'

bundle install

# Do any other automated setup that you need to do here
18 changes: 18 additions & 0 deletions lib/stateoscope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'stateoscope/version'
require 'stateoscope/integrations'
require 'stateoscope/visualizer'

require 'stateoscope/railtie' if defined?(Rails)

module Stateoscope
def self.visualize(klass, options = {})
state_machine_name = options.fetch(:state_machine_name, nil)
integration = Integrations.new_for(klass, state_machine_name)
filename = options.fetch(:filename, filename_for(integration))
Visualizer.new(integration.graph).output(filename)
end

def self.filename_for(integration)
"#{integration.full_state_machine_name}-#{Time.now.utc.strftime('%Y%m%d%H%M%s')}"
end
end
29 changes: 29 additions & 0 deletions lib/stateoscope/graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'ostruct'

module Stateoscope
class Graph
attr_accessor :initial_state, :states, :final_states, :transitions

def initialize
@states = []
@final_states = []
@transitions = []
end

def add_state(state)
@states << state
end

def add_transition(from, to, event)
@transitions << OpenStruct.new(from: from, to: to, event: event)
end

def detect_final_states!
@final_states = states - transitions.map(&:from)
end

def final_state?(state)
@final_states.include?(state)
end
end
end
14 changes: 14 additions & 0 deletions lib/stateoscope/integrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'stateoscope/graph'
require 'stateoscope/integrations/aasm'

module Stateoscope
module Integrations
def self.new_for(klass, state_machine_name)
if klass.ancestors.include?(::AASM)
::Stateoscope::Integrations::AASM.new(klass, state_machine_name)
else
fail NotImplementedError, "unsupported state machine implementation"
end
end
end
end
39 changes: 39 additions & 0 deletions lib/stateoscope/integrations/aasm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Stateoscope
module Integrations
class AASM < Struct.new(:klass, :state_machine_name)
def graph
graph = Graph.new

graph.initial_state = state_machine.initial_state.to_s

state_machine.states.each do |state|
graph.add_state(state.name.to_s)
end

state_machine.events.each do |event|
event.transitions.each do |transition|
graph.add_transition(transition.from.to_s, transition.to.to_s, event.name.to_s)
end
end

graph.detect_final_states!

graph
end

def full_state_machine_name
[
"aasm",
klass.name,
state_machine_name
].compact.join('-').dasherize
end

private

def state_machine
klass.public_send(:aasm, state_machine_name.presence)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/stateoscope/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Stateoscope
class Railtie < Rails::Railtie
rake_tasks do
Dir.glob File.expand_path('../../tasks/*.rake', __FILE__) do |rake_task|
load rake_task
end
end
end
end
3 changes: 3 additions & 0 deletions lib/stateoscope/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Stateoscope
VERSION = "0.1.0"
end
69 changes: 69 additions & 0 deletions lib/stateoscope/visualizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require 'ruby-graphviz'

module Stateoscope
class Visualizer < Struct.new(:graph)
def initialize(graph)
super
parse_graph!
end

def parse_graph!
@viz = GraphViz.new(:G, type: 'digraph')
add_entry_point
add_states
add_entry_point_transition
add_state_transitions
end

def output(filename)
@viz.output(pdf: "#{filename}.pdf")
end

private

ENTRY_POINT = '__ENTRY_POINT__'

def filename
"#{integration.name}-#{state_machine_name}-#{Time.now.utc.strftime('%Y%m%d%H%M%s')}"
end

def add_entry_point
return unless graph.initial_state
add_node(ENTRY_POINT, shape: 'circle', label: '', style: 'filled', color: 'black', fixedsize: true, width: 0.3)
end

def add_states
graph.states.each do |state|
add_node(state, peripheries: graph.final_state?(state) ? 2 : 1)
end
end

def add_entry_point_transition
return unless graph.initial_state
add_edge(ENTRY_POINT, graph.initial_state)
end

def add_state_transitions
graph.transitions.each do |transition|
add_edge(transition.from, transition.to, transition.event)
end
end

def add_node(label, options = {})
options.reverse_merge!(shape: 'ellipse')
options.reverse_merge!(global_options)
@viz.add_nodes(label, options)
end

def add_edge(from, to, label = nil, options = {})
options.reverse_merge!(label: label) if label
options.reverse_merge!(fontsize: 9, fontcolor: '#888888', labelangle: 45)
options.reverse_merge!(global_options)
@viz.add_edges(from, to, options)
end

def global_options
{fontname: 'Helvetica', fontsize: 10}
end
end
end
8 changes: 8 additions & 0 deletions lib/tasks/stateoscope.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace :stateoscope do
desc 'visualize state machine for a given class'
task :visualize, [:class, :state_machine_name] => :environment do |t, args|
fail ArgumentError 'missing required argument <class>' unless args[:class].present?
klass = args[:class].classify.constantize
Stateoscope.visualize(klass, state_machine_name: args[:state_machine_name])
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'stateoscope'
9 changes: 9 additions & 0 deletions spec/stateoscope_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

describe Stateoscope do
it 'has a version number' do
expect(Stateoscope::VERSION).not_to be nil
end

pending 'does something useful'
end
27 changes: 27 additions & 0 deletions stateoscope.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'stateoscope/version'

Gem::Specification.new do |spec|
spec.name = "stateoscope"
spec.version = Stateoscope::VERSION
spec.authors = ["Patrick Oscity"]
spec.email = ["[email protected]"]

spec.summary = %q{State Machine Visualizer}
spec.description = %q{Visualize State Machines using GraphViz}
spec.homepage = "https://github.com/padde/stateoscope"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "ruby-graphviz"

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
end

0 comments on commit 6267149

Please sign in to comment.