cgraph 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +35 -0
- data/Guardfile +11 -0
- data/Rakefile +2 -0
- data/cgraph.gemspec +20 -0
- data/lib/cgraph.rb +85 -0
- data/spec/cgraph_spec.rb +67 -0
- data/spec/spec_data.rb +12 -0
- data/spec/spec_helper.rb +3 -0
- metadata +91 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.png
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cgraph (0.0.1)
|
5
|
+
ruby-graphviz
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ffi (1.0.11)
|
11
|
+
guard (1.2.3)
|
12
|
+
listen (>= 0.4.2)
|
13
|
+
thor (>= 0.14.6)
|
14
|
+
guard-minitest (0.5.0)
|
15
|
+
guard (>= 0.4)
|
16
|
+
listen (0.4.7)
|
17
|
+
rb-fchange (~> 0.0.5)
|
18
|
+
rb-fsevent (~> 0.9.1)
|
19
|
+
rb-inotify (~> 0.8.8)
|
20
|
+
minitest (3.3.0)
|
21
|
+
rb-fchange (0.0.5)
|
22
|
+
ffi
|
23
|
+
rb-fsevent (0.9.1)
|
24
|
+
rb-inotify (0.8.8)
|
25
|
+
ffi (>= 0.5.0)
|
26
|
+
ruby-graphviz (1.0.8)
|
27
|
+
thor (0.15.4)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
cgraph!
|
34
|
+
guard-minitest
|
35
|
+
minitest
|
data/Guardfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
guard 'minitest', :colour => true do
|
2
|
+
# with Minitest::Unit
|
3
|
+
#watch(%r|^test/test_(.*)\.rb|)
|
4
|
+
#watch(%r|^lib/(.*)\.rb|) { |m| "test/test_#{m[1]}.rb" }
|
5
|
+
#watch(%r|^test/test_helper\.rb|) { "test" }
|
6
|
+
|
7
|
+
# with Minitest::Spec
|
8
|
+
watch(%r|^spec/(.*)_spec\.rb|)
|
9
|
+
watch(%r|^lib/(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
|
10
|
+
watch(%r|^spec/spec_helper\.rb|) { "spec" }
|
11
|
+
end
|
data/Rakefile
ADDED
data/cgraph.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Martin Schneider"]
|
5
|
+
gem.email = ["martin.schneider@pludoni.de"]
|
6
|
+
gem.description = %q{}
|
7
|
+
gem.summary = %q{}
|
8
|
+
gem.homepage = ""
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "cgraph"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = "0.0.1"
|
16
|
+
gem.add_development_dependency "guard-minitest"
|
17
|
+
gem.add_development_dependency "minitest"
|
18
|
+
|
19
|
+
gem.add_dependency "ruby-graphviz"
|
20
|
+
end
|
data/lib/cgraph.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'graphviz'
|
2
|
+
module Cgraph
|
3
|
+
class Entity
|
4
|
+
@@count = 0
|
5
|
+
@@names = []
|
6
|
+
attr_accessor :picture, :outgoing, :label, :group
|
7
|
+
attr_reader :name, :id
|
8
|
+
def initialize name
|
9
|
+
@name = Entity.check_name name
|
10
|
+
@id = Entity.get_id
|
11
|
+
@picture = ""
|
12
|
+
@group = ""
|
13
|
+
@outgoing = []
|
14
|
+
end
|
15
|
+
def self.clear_namelist
|
16
|
+
@@names = []
|
17
|
+
end
|
18
|
+
def label
|
19
|
+
return @name if @label.nil?
|
20
|
+
@label
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def self.get_id
|
25
|
+
@@count += 1
|
26
|
+
end
|
27
|
+
def self.check_name name
|
28
|
+
throw :same_name_error if @@names.include? name
|
29
|
+
@@names.push name
|
30
|
+
name
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class Graph
|
35
|
+
attr_reader :entities, :connections
|
36
|
+
def initialize data, info
|
37
|
+
Entity.clear_namelist
|
38
|
+
@connections = []
|
39
|
+
@entities = []
|
40
|
+
generate_entities data, info
|
41
|
+
generate_connections data
|
42
|
+
end
|
43
|
+
def generate_entities data, infos = nil
|
44
|
+
data.flatten.uniq.each do |name|
|
45
|
+
tmp = Entity.new name
|
46
|
+
if !infos.nil? && !infos[name].nil?
|
47
|
+
info = infos[name]
|
48
|
+
tmp.label = info[:label] || ""
|
49
|
+
tmp.picture = info[:picture] || ""
|
50
|
+
tmp.group = info[:group] || ""
|
51
|
+
end
|
52
|
+
@entities.push tmp
|
53
|
+
end
|
54
|
+
end
|
55
|
+
def generate_connections data
|
56
|
+
@connections = data.group_by do |a,b|
|
57
|
+
a+b; [a,b]
|
58
|
+
end.map do |a,b|
|
59
|
+
[a ,{count: b.count}]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
def find_entity name
|
63
|
+
@entities.each do |e|
|
64
|
+
return e if e.name == name
|
65
|
+
end
|
66
|
+
end
|
67
|
+
def generate_graph
|
68
|
+
graph = GraphViz.new(:G, :type => :digraph)
|
69
|
+
graph = add_connections_to_graph graph
|
70
|
+
graph.output(:png => "test.png")
|
71
|
+
graph
|
72
|
+
end
|
73
|
+
def add_connections_to_graph graph
|
74
|
+
@connections.each do |conn|
|
75
|
+
p conn
|
76
|
+
tupel = conn.first
|
77
|
+
|
78
|
+
options = {penwidth: conn[1][:count]}
|
79
|
+
graph.add_edges tupel[0],tupel[1],options
|
80
|
+
end
|
81
|
+
graph
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
data/spec/cgraph_spec.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
require_relative 'spec_data'
|
3
|
+
require 'cgraph'
|
4
|
+
include Cgraph
|
5
|
+
describe Entity do
|
6
|
+
after do
|
7
|
+
Entity.clear_namelist
|
8
|
+
end
|
9
|
+
let(:e) {Entity.new "abc" }
|
10
|
+
let(:f) {Entity.new "blub" }
|
11
|
+
it "has automagic id" do
|
12
|
+
e.id.must_be_instance_of Fixnum
|
13
|
+
e.id.wont_equal f.id
|
14
|
+
end
|
15
|
+
it "must have a name" do
|
16
|
+
e.name.must_equal "abc"
|
17
|
+
end
|
18
|
+
it "has a label" do
|
19
|
+
e.label.must_equal "abc"
|
20
|
+
e.label = "xyz"
|
21
|
+
e.label.must_equal "xyz"
|
22
|
+
e.name.must_equal "abc"
|
23
|
+
end
|
24
|
+
it "can have a group" do
|
25
|
+
e.group.must_equal ""
|
26
|
+
e.group = "woo"
|
27
|
+
e.group.must_equal "woo"
|
28
|
+
end
|
29
|
+
it "should not allow same names" do
|
30
|
+
Entity.new("blub")
|
31
|
+
proc {Entity.new("blub")}.must_throw :same_name_error
|
32
|
+
end
|
33
|
+
it "can have a picture" do
|
34
|
+
e.picture.must_equal ""
|
35
|
+
e.picture = "link"
|
36
|
+
e.picture.must_equal "link"
|
37
|
+
end
|
38
|
+
it "knows the outgoing connections" do
|
39
|
+
e.outgoing.must_be_instance_of Array
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe Graph do
|
44
|
+
let(:g) {Graph.new TEST_CONNECTIONS, TEST_ENTITIES}
|
45
|
+
|
46
|
+
it "should take an array, and a hash as parameter" do
|
47
|
+
Graph.new [], {}
|
48
|
+
end
|
49
|
+
it "should create 3 Entities with the given data" do
|
50
|
+
g.entities.count.must_equal 3
|
51
|
+
end
|
52
|
+
it "should find entity by name" do
|
53
|
+
g.find_entity("peter").name.must_equal "peter"
|
54
|
+
end
|
55
|
+
it "should create connections between the entities" do
|
56
|
+
g.connections.must_be_kind_of Array
|
57
|
+
g.connections.count.must_be :>, 0
|
58
|
+
end
|
59
|
+
it "should generate an basic graphviz graph" do
|
60
|
+
g.generate_graph.to_s.must_match /digraph/
|
61
|
+
end
|
62
|
+
it "should use the info data to enhance the entities" do
|
63
|
+
g.find_entity("peter").label.must_equal "Peter Pan"
|
64
|
+
g.find_entity("paul").picture.must_equal "logo_url"
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
data/spec/spec_data.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cgraph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Martin Schneider
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: guard-minitest
|
16
|
+
requirement: &20170240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *20170240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: minitest
|
27
|
+
requirement: &20186200 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *20186200
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: ruby-graphviz
|
38
|
+
requirement: &20185780 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *20185780
|
47
|
+
description: ''
|
48
|
+
email:
|
49
|
+
- martin.schneider@pludoni.de
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- Gemfile.lock
|
57
|
+
- Guardfile
|
58
|
+
- Rakefile
|
59
|
+
- cgraph.gemspec
|
60
|
+
- lib/cgraph.rb
|
61
|
+
- spec/cgraph_spec.rb
|
62
|
+
- spec/spec_data.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
homepage: ''
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.10
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: ''
|
88
|
+
test_files:
|
89
|
+
- spec/cgraph_spec.rb
|
90
|
+
- spec/spec_data.rb
|
91
|
+
- spec/spec_helper.rb
|