-
Notifications
You must be signed in to change notification settings - Fork 0
/
erd_generator.rb
136 lines (123 loc) · 4.45 KB
/
erd_generator.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
require_relative 'shopify_client'
class ERDGenerator
def initialize(shop_url, access_token)
@client = ShopifyClient.new(shop_url, access_token)
@output = ""
@relationships = []
@existing_objects = []
end
def generate
fetch_definitions
process_metaobjects
process_metafields
append_missing_objects
append_relationships
write_output
end
private
def fetch_definitions
puts "\nFetching metaobjects..."
@metaobjects = @client.fetch_metaobjects
puts "\nFetching metafields..."
@metafields = @client.fetch_metafields
end
def to_camel(str)
str.split(/[-_\s]/).inject([]) { |buffer, e| buffer.push(buffer.empty? ? e.downcase : e.capitalize) }.join
end
def random_color
[
"#ADD8E6", "#B0E0E6", "#AFEEEE", "#87CEEB", "#87CEFA", "#00BFFF", "#1E90FF", "#6495ED", "#4682B4", "#5F9EA0",
"#B0C4DE", "#B0E0E6", "#ADD8E6", "#87CEEB", "#87CEFA", "#00CED1", "#48D1CC", "#40E0D0", "#00FFFF", "#E0FFFF"
].sample
end
def process_metaobjects
@metaobjects.each do |name, fields|
color = random_color
object_name = to_camel(name)
@existing_objects << object_name
@output += "\n\n\n[#{object_name}] {bgcolor: \"#{color}\", size: \"20\", font: \"Comic Sans MS\" }\n"
fields.each do |field|
required = field['required']
label = "#{field['type_name']}, #{required}"
key = to_camel(field['key'])
@output += " #{key == 'id' ? '*' : ''}#{key} {label: \"#{label}\"}\n"
# Identify relationships based on attribute labels
if field['type_name'].end_with?('_reference')
referenced_object = to_camel(field['type_name'].split('_')[0...-1].join('_'))
if referenced_object.include?('list')
if referenced_object.include?('metaobject')
@relationships << "#{object_name} 1--* #{key}"
else
@relationships << "#{object_name} 1--* #{referenced_object.split('.').last.to_s}"
end
else
if referenced_object.include?('metaobject')
@relationships << "#{object_name} *--1 #{key}"
else
@relationships << "#{object_name} *--1 #{referenced_object}"
end
end
end
end
end
end
def process_metafields
@metafields.each do |key, fields|
next if fields.empty?
color = random_color
@existing_objects << key.downcase
@output += "\n\n\n[#{key}] {bgcolor: \"#{color}\", size: \"20\", font: \"Comic Sans MS\" }\n"
fields.each do |field|
required = field['required']
label = "#{field['type_name']}, #{required}"
name = to_camel(field['name'])
@output += "#{name} {label: \"#{label}\"}\n"
# Identify relationships based on attribute labels
if field['type_name'].end_with?('_reference')
referenced_object = to_camel(field['type_name'].split('_')[0...-1].join('_'))
if referenced_object.include?('list')
if referenced_object.include?('metaobject')
@relationships << "#{key} 1--* #{name}"
else
@relationships << "#{key} 1--* #{referenced_object.split('.').last.to_s}"
end
else
if referenced_object.include?('metaobject')
@relationships << "#{key} *--1 #{name}"
else
@relationships << "#{key} *--1 #{referenced_object}"
end
end
end
end
end
end
def append_missing_objects
@relationships.each do |relationship|
if relationship.include?(' 1--* ')
_, referenced_object = relationship.split(' 1--* ')
elsif relationship.include?(' *--1 ')
_, referenced_object = relationship.split(' *--1 ')
end
unless @existing_objects.include?(referenced_object.strip)
color = random_color
@output += "\n\n\n[#{referenced_object.strip}] {bgcolor: \"#{color}\"}\n"
@existing_objects << referenced_object.strip
end
end
end
def append_relationships
@output += "\n# Relationships\n"
@relationships.each do |relationship|
@output += "#{relationship}\n"
end
end
def write_output
header = "title {label: \"Entity-Relationship diagram\", size: \"20\"}\n\n# Entities\n\n"
@output = header + @output
File.open('erd-files/metafields.er', 'w') do |file|
file.write(@output)
end
puts "ERD generation completed. Output written to erd-files/metafields.er"
end
end