Skip to content

Commit 3be9f14

Browse files
committed
init
0 parents  commit 3be9f14

File tree

10 files changed

+412
-0
lines changed

10 files changed

+412
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in yagviz.gemspec
4+
gemspec

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2012 kyoendo
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Yagviz
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'yagviz'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install yagviz
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "bundler/gem_tasks"

lib/yagviz.rb

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
class Yagviz
2+
class Node < Struct.new(:id, :attrs)
3+
def initialize(id, attrs={})
4+
raise ArgumentError, "`id` must not include underscores" if id.match(/_/)
5+
super
6+
end
7+
8+
def to_s
9+
"#{id}"
10+
end
11+
end
12+
13+
class Edge < Struct.new(:id, :attrs)
14+
attr_reader :st, :ed, :seq
15+
def initialize(id, attrs={})
16+
raise ArgumentError, "`attrs` must a hash" unless attrs.is_a?(Hash)
17+
st, ed, seq = "#{id}".split('_')
18+
@st, @ed = [st, ed].map(&:intern)
19+
@seq = seq.to_i
20+
super
21+
end
22+
23+
def to_s
24+
"#{st} -> #{ed}"
25+
end
26+
27+
def nodes
28+
[st, ed]
29+
end
30+
end
31+
32+
attr_reader :edges, :nodes
33+
def initialize
34+
@edges = {}
35+
@nodes = {}
36+
end
37+
38+
def node(id, attrs={})
39+
raise ArgumentError, '`id` must a symbol' unless id.is_a?(Symbol)
40+
Node[id, attrs].tap { |node| @nodes.update(id => node) }
41+
end
42+
43+
def edge(id, attrs={})
44+
unless id.is_a?(Symbol) && id.match(/._./)
45+
raise ArgumentError, '`id` must a symbol in which node names joined with "_"'
46+
end
47+
Edge[id, attrs].tap do |edge|
48+
@edges.update(id => edge)
49+
create_nodes
50+
end
51+
end
52+
53+
def graph(&blk)
54+
instance_eval(&blk)
55+
end
56+
57+
def add(*nodes_or_routes)
58+
nodes_or_routes.each do |unit|
59+
case unit
60+
when Hash
61+
unit.each do |sts, eds|
62+
Array(sts).product(Array(eds))
63+
.each { |st, ed| edge([st,ed].join('_').intern) }
64+
end
65+
when Symbol
66+
node(unit)
67+
else
68+
raise ArgumentError, 'pass nodes in symbol or edges in hash'
69+
end
70+
end
71+
self
72+
end
73+
74+
def to_s
75+
result = []
76+
result << "digraph {"
77+
78+
nodes.values.each do |node|
79+
attrs = build_attrs(node.attrs)
80+
result << " #{node.id}#{attrs};"
81+
end
82+
edges.values.each do |edge|
83+
attrs = build_attrs(edge.attrs)
84+
result << " #{edge}#{attrs};"
85+
end
86+
87+
result << "}\n"
88+
result.join("\n")
89+
end
90+
91+
private
92+
def create_nodes
93+
# only create unregistered nodes
94+
ids = edges.values.flat_map(&:nodes).uniq - nodes.keys
95+
ids.each { |id| node(id) }
96+
nodes
97+
end
98+
99+
def build_attrs(attrs)
100+
return nil if attrs.empty?
101+
'[' + attrs.map { |attr| attr.join("=") }.join(',') + ']'
102+
end
103+
end
104+
105+
if __FILE__ == $0
106+
yag = Yagviz.new
107+
yag.add :main => [:init, :parse, :cleanup, :printf]
108+
puts yag
109+
end
110+

lib/yagviz/version.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Yagviz
2+
VERSION = "0.0.1"
3+
end

spec/spec_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "yagviz"
2+
require "rspec"
3+
4+
class String
5+
def ~
6+
margin = scan(/^ +/).map(&:size).min
7+
gsub(/^ {#{margin}}/, '')
8+
end
9+
end

0 commit comments

Comments
 (0)