-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplant_uml.rb
151 lines (114 loc) · 3.04 KB
/
plant_uml.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'singleton'
require 'active_support/core_ext/string'
class Column
FOREIGN_KEY_MATCHERS = %w(_id _type).freeze
attr_accessor :type, :name, :options
def initialize(type, name, **options)
@type = type.to_sym
@name = name
@options = options
end
def foreign_key?
FOREIGN_KEY_MATCHERS.any? do |relation_matcher|
name.to_s.end_with?(relation_matcher)
end
end
def to_plantuml
foreign_key_mark = foreign_key? ? '*' : ''
"#{foreign_key_mark}#{name} : #{type}"
end
end
class Table
SKIP_COLUMN_TYPES = %i[index]
attr_accessor :name, :options
attr_accessor :columns
def initialize(name, **options)
@name = name.to_sym
@options = options
@columns = []
end
def method_missing(column_type, *args, &block)
# exclude next methods on table definition
return if SKIP_COLUMN_TYPES.include?(column_type)
column_name = args.shift
columns << Column.new(column_type, column_name, *args)
end
def to_plantuml
<<-PLANTUML
entity "#{name}" as #{name} {
id : integer <<generated>>
--
#{columns.map(&:to_plantuml).join("\n ")}
}
PLANTUML
end
end
class Relation
attr_accessor :from_table, :to_table, :options
def initialize(from_table, to_table, **options)
@from_table = from_table.to_sym
@to_table = to_table.to_sym
@options = options
end
def foreign_key
options[:column].presence || "#{to_table.to_s.singularize}_id"
end
def to_plantuml
foreign_key_label = options[:column].present? ? " : #{foreign_key}" : ""
relation_direction = [:down, :up].sample
case relation_direction
when :up then "#{from_table} }|--#{'-' * rand(2)} #{to_table}#{foreign_key_label}"
when :down then "#{to_table} #{'-' * rand(2)}--|{ #{from_table}#{foreign_key_label}"
end
end
end
module ActiveRecord
class Schema
include Singleton
attr_accessor :tables, :relations
def self.define(info = {}, &block)
instance.define(info, &block)
end
def define(info, &block) # :nodoc:
instance_eval(&block)
end
def create_table(name, **options)
return unless include_table?(name)
table = Table.new(name, options)
yield(table)
tables << table
end
def add_foreign_key(from_table, to_table, **options)
return if !include_table?(from_table) && !include_table?(to_table)
relations << Relation.new(from_table, to_table, options)
end
def method_missing(m, *args, &block)
puts "WARN: Missing definition ##{m}(#{args})"
end
def include_table?(table_name)
return true if ARGV[1..-1].none?
ARGV[1..-1].include?(table_name.to_s)
end
def to_plantuml
<<-PLANTUML
@startuml
hide circle
skinparam linetype ortho
#{tables.map(&:to_plantuml).join("\n")}
#{relations.map(&:to_plantuml).join("\n")}
@enduml
PLANTUML
end
private
def initialize
@tables = []
@relations = []
end
end
end
if ARGV[0].nil?
puts "Usage: ruby plant_uml.rb /path/to/schema.rb [table1] [table2] ..."
else
load ARGV[0]
puts ActiveRecord::Schema.instance.to_plantuml
end