doterd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjU4MTQ3NzhkMjA5NzJlMGE4ZGI2NWNmNmI2MGZiYjdjYzNhMTZmZA==
5
+ data.tar.gz: !binary |-
6
+ YjVmNDgxMjgzOWU3OGEyMGExODUyZDA5NDdjYTA3NzM1Yzc0OTk3MA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmU3Y2Q4YTA0NjIzMGUyYzcxMjJlMGQ2MjA5MmMwMTk5YzcyYTNkMmM4ODU4
10
+ YTViOGU3MzExYmJkNzZlY2E2ZmM2MDcyM2MwMDNhZWQ3NmJiZWU4NTIyYjll
11
+ ZTEzMTFmZjIyMGVmYmY2ZTY1NTM0ODc3ZWZiYzA3NmNmZTBkNjE=
12
+ data.tar.gz: !binary |-
13
+ NjIzMWI2ZDYzNjEwODVjYzYwMGQ3MmU3ZjdmMTYxNDkxZjE4NDNjZjcwYmI0
14
+ ZGNlZmExMWM3ZjdmNTUxZTk2ZDc5ZjQ2YTBlM2U2MjY2ZGExMDViZjUwMmU0
15
+ OGI4ZjY3YTllNGZhZGViYzQ0ODQyNjFiOTAxZmNmMWU2YTRhMTk=
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in doterd.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 wenjun.yan
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.
@@ -0,0 +1,29 @@
1
+ # Doterd
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'doterd'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install doterd
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/doterd/fork )
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 a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'doterd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "doterd"
8
+ spec.version = Doterd::VERSION
9
+ spec.authors = ["wenjun.yan"]
10
+ spec.email = ["mylastnameisyan@gmail.com"]
11
+ spec.summary = %q{ruby dsl for ERD }
12
+ spec.description = %q{ruby dsl for ERD }
13
+ spec.homepage = "https://github.com/v2e4lisp/doterd"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
File without changes
@@ -0,0 +1,214 @@
1
+ require "doterd/version"
2
+
3
+ module Doterd
4
+
5
+ # auto draw erd at the end of current script
6
+ #
7
+ # Exmaple:
8
+ #
9
+ # include Doterd::Autodraw
10
+ #
11
+ # this will also include the default erd dsl
12
+ module Autodraw
13
+ def self.included(base)
14
+ base.send(:include, Doterd::DSL)
15
+
16
+ at_exit {
17
+ next if $! and not ($!.kind_of? SystemExit and $!.success?)
18
+ Doterd.viz
19
+ }
20
+ end
21
+ end
22
+
23
+ # Configuration
24
+ #
25
+ # table_renderer should respond to call(table_name, columns)
26
+ # relation_renderer should respond to call(relation, table_name_1, table_name_2)
27
+ def self.config
28
+ @config ||= {
29
+ table_renderer: Renderer::Table,
30
+ relation_renderer: Renderer::Relation,
31
+ dot_filename: "/tmp/test.dot",
32
+ output_type: :png,
33
+ column_description: true,
34
+
35
+ graph_attributes: {
36
+ concentrate: true,
37
+ labelloc: :t,
38
+ nodesep: 0.5,
39
+ ratio: 1.0,
40
+ fontsize: 13,
41
+ pad: "0.4,0.4",
42
+ rankdir: :LR,
43
+ margin: "0,0",
44
+ },
45
+ node_attributes:{
46
+ shape: "Mrecord",
47
+ fontsize: 15,
48
+ margin: "0.07,0.05",
49
+ penwidth: 1.0,
50
+ },
51
+ edge_attributes: {
52
+ fontsize: 8,
53
+ dir: :both,
54
+ arrowsize: 1.4,
55
+ penwidth: 1.0,
56
+ labelangle: 32,
57
+ labeldistance: 1.8,
58
+ fontsize: 7,
59
+ },
60
+ }
61
+ end
62
+
63
+ # A table has the following structure
64
+ #
65
+ # [
66
+ # table_name,
67
+ # {
68
+ # col_name_1 => [type, description]
69
+ # col_name_2 => [type]
70
+ # col_name_3 => []
71
+ # ...
72
+ # }
73
+ # ]
74
+ def self.tables
75
+ @tables ||= []
76
+ end
77
+
78
+ # A relation has the following structure
79
+ #
80
+ # [relation_name, table_name_1, table_name_2]
81
+ #
82
+ # Available relation_name:
83
+ #
84
+ # [:_1_N, :_1_1, :_N_1, :_N_N]
85
+ #
86
+ # table may or may not exist in self.tables
87
+ def self.relations
88
+ @relations ||= []
89
+ end
90
+
91
+ # Create erd image
92
+ #
93
+ # generate a dot source code, save it to a file
94
+ # and create a erd image out of it.
95
+ def self.viz(dot_filename=nil)
96
+ dot_filename ||= Doterd.config[:dot_filename]
97
+ File.open(dot_filename, 'w') { |f| f.write dot }
98
+ system("dot -O -T#{Doterd.config[:output_type]} #{dot_filename}")
99
+ end
100
+
101
+ # Generate dot source code
102
+ def self.dot
103
+ gattr = Doterd.config[:graph_attributes].map {|k, v| "#{k}=\"#{v}\";\n" }.join
104
+ nattr = Doterd.config[:node_attributes].map {|k, v| "#{k}=\"#{v}\"" }.join ','
105
+ eattr = Doterd.config[:edge_attributes].map {|k, v| "#{k}=\"#{v}\"" }.join ','
106
+ nodes = Doterd.tables.map {|t| Doterd.config[:table_renderer].call *t }
107
+ edges = Doterd.relations.map {|r| Doterd.config[:relation_renderer].call *r }
108
+
109
+ dot = %Q{
110
+ digraph adlantis_sp {
111
+ #{gattr}
112
+ node [#{nattr}];
113
+ edge [#{eattr}];
114
+ #{(nodes << '').join(";\n")}
115
+ #{(edges << '').join(";\n")}
116
+ }
117
+ }
118
+ end
119
+
120
+ # Default renderers
121
+ module Renderer
122
+ class Table
123
+ def self.call(tbl_name, columns)
124
+ "#{tbl_name} [label=\"TABLE #{tbl_name}|#{dot_columns columns}\"]"
125
+ end
126
+
127
+ def self.dot_columns(columns)
128
+ labels = columns.map {|name, opts| "#{name} #{column_label opts}"}.join("|") + "| "
129
+ end
130
+
131
+ def self.column_label options
132
+ case options.size
133
+ when 0
134
+ ''
135
+ when 1
136
+ "[#{options.first}]"
137
+ else
138
+ desc = options.pop
139
+ desc = '' unless ::Doterd.config[:column_description]
140
+ "[#{options.join(', ')}]" + '\n' + desc
141
+ end
142
+ end
143
+ end
144
+
145
+ class Relation
146
+ def self.call(relation, from, to)
147
+ "#{from} -> #{to} #{label relation}"
148
+ end
149
+
150
+ def self.label(relation)
151
+ case relation
152
+ when :_1_1
153
+ "[arrowhead=odot, arrowtail=odot, dir=both]"
154
+ when :_1_N
155
+ "[arrowhead=inv, arrowtail=odot, dir=both]"
156
+ when :_N_1
157
+ "[arrowhead=odot, arrowtail=inv, dir=both]"
158
+ when :_N_N
159
+ "[arrowhead=inv, arrowtail=inv, dir=both]"
160
+ else
161
+ raise "Relation not found for #{relation}"
162
+ end
163
+ end
164
+ end
165
+ end
166
+
167
+ # Default erd dsl
168
+ #
169
+ # Examples:
170
+ #
171
+ # # creats tables
172
+ # table(:t1) {
173
+ # col_1 Integer, "some comments"
174
+ # col_2 String, "some comments"
175
+ # col_3 "String, Not Null", "some comments"
176
+ # }
177
+ #
178
+ # table(:t2) {
179
+ # id
180
+ # t1_id
181
+ # }
182
+ #
183
+ # # create table relations
184
+ # _1_1 :t1, :t2
185
+ #
186
+ module DSL
187
+
188
+ def config(&block)
189
+ yield Doterd.config
190
+ end
191
+
192
+ def table(name, &block)
193
+ Table.new(name, &block)
194
+ end
195
+
196
+ [:_1_N, :_N_1, :_N_N, :_1_1].each {|rel|
197
+ define_method(rel) {|from, to| Doterd.relations << [rel, from, to]}
198
+ }
199
+
200
+ class Table < ::BasicObject
201
+ def initialize(name, &block)
202
+ @name = name
203
+ @columns = {}
204
+ instance_eval &block
205
+ ::Doterd.tables << [@name, @columns]
206
+ end
207
+
208
+ def method_missing(col, *options)
209
+ @columns[col] = options
210
+ end
211
+ end
212
+ end
213
+
214
+ end
@@ -0,0 +1,3 @@
1
+ module Doterd
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doterd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - wenjun.yan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ! 'ruby dsl for ERD '
42
+ email:
43
+ - mylastnameisyan@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - doterd.gemspec
54
+ - examples/.gitkeep
55
+ - lib/doterd.rb
56
+ - lib/doterd/version.rb
57
+ homepage: https://github.com/v2e4lisp/doterd
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: ruby dsl for ERD
81
+ test_files: []