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_label(table_name, columns, index) ⇒ Object



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

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



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

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.columns(table_name).each do |column|
      column.each { |key, value| columns += "#{key}: <FONT color='gray'>#{value}</FONT><BR/>" }
    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|
    unless schema_structure.relation(table_name)['belongs_to'].nil?
      schema_structure.relation(table_name)['belongs_to'].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

.table_relations(schema_structure) ⇒ Object



8
9
10
# File 'lib/yml2erd/diagram.rb', line 8

def table_relations(schema_structure)
  schema_structure.relations
end