ruby-erd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ruby-erd.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'graphviz'
2
+
3
+ require 'ruby-erd/erd_builder.rb'
4
+ require 'ruby-erd/erd_entity.rb'
5
+
6
+ class RubyERD < ERDBuilder
7
+
8
+ end
@@ -0,0 +1,115 @@
1
+ class ERDBuilder
2
+
3
+ attr_accessor :default_node_background_color
4
+ attr_reader :name
5
+
6
+ def add_entity(name)
7
+ ERDEntity.new.tap do |entity|
8
+ entity.build(graph: graph, name: name)
9
+ entity.background_color = node_background_color
10
+ end
11
+ end
12
+
13
+ def associate(entity, other_entity, attributes)
14
+ case other_entity
15
+ when Array
16
+ diagram.add_edges(entity.node, other_entity.collect(&:node), edge_attributes_for(attributes))
17
+ else
18
+ diagram.add_edges(entity.node, other_entity.node, edge_attributes_for(attributes))
19
+ end
20
+ end
21
+
22
+ def begin_system(name)
23
+ @name = name
24
+
25
+ # Initialize these objects
26
+ graph
27
+ end
28
+
29
+ # Public: Returns the current diagram, or a new instance if there is none.
30
+ #
31
+ # For a list of available attributes, see:
32
+ # * https://github.com/glejeune/Ruby-Graphviz/blob/master/lib/graphviz/constants.rb
33
+ # * http://www.graphviz.org/content/attrs
34
+ #
35
+ # Ruby-Graphviz overrides []= on GraphViz objects so to set an attribute, treat
36
+ # the GraphViz object like a hash.
37
+ #
38
+ # Example: diagram[attribute] = value
39
+ #
40
+ # Returns a GraphViz instance
41
+ def diagram
42
+ @diagram ||= GraphViz.digraph(name) do |diagram|
43
+ # Prevent node overlap
44
+ # http://www.graphviz.org/content/attrs#doverlap
45
+ diagram[:overlap] = 'false'
46
+
47
+ # Draw straight lines between nodes
48
+ # http://www.graphviz.org/content/attrs#dsplines
49
+ diagram[:splines] = 'true'
50
+
51
+ # Contents of the diagram's label
52
+ # http://www.graphviz.org/content/attrs#dlabel
53
+ diagram[:label] = name
54
+
55
+ # Label location: Display the title at the top 't' of the diagram
56
+ # http://www.graphviz.org/content/attrs#dlabelloc
57
+ diagram[:labelloc] = "t"
58
+ end
59
+ end
60
+
61
+ def export_as_png
62
+ diagram.output(png: "#{name}.png")
63
+ end
64
+
65
+ def graph
66
+ @graph ||= diagram.add_graph(name).tap do |graph|
67
+ graph[:overlap] = 'false'
68
+ graph[:splines] = 'true'
69
+ end
70
+ end
71
+
72
+ def join(entity, other_entity, label="joins")
73
+ join_table = add_entity("#{entity.name}#{other_entity.name}")
74
+ join_table.background_color = '#5182FF'
75
+
76
+ associate(entity, join_table, {association: :one_to_many, name: label})
77
+ associate(join_table, other_entity, {association: :many_to_one, name: ""})
78
+ end
79
+
80
+ private
81
+
82
+ def edge_attributes_for(attributes)
83
+ case attributes[:association]
84
+ when :one_to_one
85
+ arrowhead = 'odot'
86
+ arrowtail = 'odot'
87
+ when :one_to_many
88
+ arrowhead = 'crow'
89
+ arrowtail = 'odot'
90
+ when :many_to_one
91
+ arrowhead = 'odot'
92
+ arrowtail = 'crow'
93
+ when :many_to_many
94
+ arrowhead = 'crow'
95
+ arrowtail = 'crow'
96
+ else
97
+ arrowhead = 'odot'
98
+ arrowtail = 'odot'
99
+ end
100
+
101
+ label = attributes[:name] || "Label"
102
+
103
+ {
104
+ arrowhead: arrowhead,
105
+ arrowtail: arrowtail,
106
+ dir: 'both',
107
+ label: label
108
+ }
109
+ end
110
+
111
+ def node_background_color
112
+ default_node_background_color || '#51CEFF'
113
+ end
114
+
115
+ end
@@ -0,0 +1,25 @@
1
+ class ERDEntity
2
+
3
+ attr_reader :name, :node
4
+
5
+ def attributes=(attributes)
6
+ attributes_as_string = attributes.collect do |key, value|
7
+ "#{key} :#{value}"
8
+ end.join('\l')
9
+
10
+ @node.label = "{#{name}| #{attributes_as_string}}"
11
+ end
12
+
13
+ def background_color=(color)
14
+ @node.color = color
15
+ @node.style = :filled
16
+ end
17
+
18
+ def build(attributes)
19
+ @graph = attributes[:graph]
20
+ @name = attributes[:name]
21
+ # Returns a GraphViz::Node
22
+ @node = @graph.add_nodes(name, shape: 'record')
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-erd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jordan Maguire
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-graphviz
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: description
31
+ email:
32
+ - jmaguire@thefrontiergroup.com.au
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/ruby-erd/erd_builder.rb
38
+ - lib/ruby-erd/erd_entity.rb
39
+ - lib/ruby-erd.rb
40
+ homepage: https://github.com/jordanmaguire/ruby-erd
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.25
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: summary
64
+ test_files: []