Class: Yml2erd::Diagram

Inherits:
Object
  • Object
show all
Defined in:
lib/yml2erd/diagram.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ output_path: './output.png' }.freeze

Class Method Summary collapse

Class Method Details

.build_column(name, type) ⇒ Object



51
52
53
# File 'lib/yml2erd/diagram.rb', line 51

def build_column(name, type)
  "#{name}: <FONT color='gray'>#{type}</FONT><BR/>"
end

.build_label(table_name, columns, index) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/yml2erd/diagram.rb', line 43

def build_label(table_name, columns, index)
  if index
    "<{<FONT POINT-SIZE='15'>#{table_name}</FONT> | #{columns} | indexed: #{index.to_s}}>"
  else
    "<{<FONT POINT-SIZE='15'>#{table_name}</FONT> | #{columns}}>"
  end
end

.create(schema_structure, opts = {}) ⇒ Object



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
# File 'lib/yml2erd/diagram.rb', line 8

def create(schema_structure, opts = {})
  opts = opts.merge(DEFAULT_OPTIONS)
  GraphViz::options(use: 'dot')
  g = GraphViz::new('structs', label: opts[:project_name])

  table_names = schema_structure.table_names

  table_names.each do |table_name|
    columns = ''
    schema_structure.shared_columns.each { |key, value| columns += build_column(key, value) } unless schema_structure.shared_columns.nil?
    schema_structure.columns(table_name).each do |column|
      column.each { |key, value| columns += build_column(key, value) }
    end

    index = schema_structure.index(table_name)
    label = build_label(table_name, columns, index)
    g.add_nodes(table_name, shape: "record", label: label, style: "rounded")
  end

  table_names.each do |table_name|
    if !schema_structure.relation(table_name).nil? && \
      !schema_structure.belongs(table_name).nil?
      schema_structure.belongs(table_name).each do |belongs_to|
        g.add_edges(belongs_to, table_name)
      end
    end
  end

  if opts[:output_style] == 'svg'
    g.output(:svg=> opts[:output_path] + '.svg')
  else
    g.output(:png => opts[:output_path])
  end
end