module Doterd

Constants

VERSION

Public Class Methods

config() click to toggle source

Configuration

table_renderer should respond to call(table_name, columns) relation_renderer should respond to call(relation, table_name_1, table_name_2)

# File lib/doterd.rb, line 27
def self.config
  @config ||= {
    table_renderer: Renderer::Table,
    relation_renderer: Renderer::Relation,
    dot_filename: "/tmp/test.dot",
    output_type: :png,
    column_description: true,

    graph_attributes: {
      concentrate: true,
      labelloc: :t,
      nodesep: 0.5,
      ratio: 1.0,
      fontsize: 13,
      pad: "0.4,0.4",
      rankdir: :LR,
      margin: "0,0",
    },
    node_attributes:{
      shape: "Mrecord",
      fontsize: 15,
      margin: "0.07,0.05",
      penwidth: 1.0,
    },
    edge_attributes: {
      fontsize: 8,
      dir: :both,
      arrowsize: 1.4,
      penwidth: 1.0,
      labelangle: 32,
      labeldistance: 1.8,
      fontsize: 7,
    },
  }
end
dot() click to toggle source

Generate dot source code

# File lib/doterd.rb, line 102
def self.dot
  gattr = Doterd.config[:graph_attributes].map {|k, v| "#{k}=\"#{v}\";\n" }.join
  nattr = Doterd.config[:node_attributes].map {|k, v| "#{k}=\"#{v}\"" }.join ','
  eattr = Doterd.config[:edge_attributes].map {|k, v| "#{k}=\"#{v}\"" }.join ','
  nodes = Doterd.tables.map {|t| Doterd.config[:table_renderer].call *t }
  edges = Doterd.relations.map {|r| Doterd.config[:relation_renderer].call *r }

  dot = %Q{
      digraph adlantis_sp {
        #{gattr}
        node [#{nattr}];
        edge [#{eattr}];
        #{(nodes << '').join(";\n")}
        #{(edges << '').join(";\n")}
      }
  }
end
relations() click to toggle source

A relation has the following structure

[relation_name, table_name_1, table_name_2]

Available relation_name:

[:_1_N, :_1_1, :_N_1, :_N_N]

table may or may not exist in self.tables

# File lib/doterd.rb, line 87
def self.relations
  @relations ||= []
end
tables() click to toggle source

A table has the following structure

[
  table_name,
  {
    col_name_1 => [type, description]
    col_name_2 => [type]
    col_name_3 => []
    ...
  }
]
# File lib/doterd.rb, line 74
def self.tables
  @tables ||= []
end
viz(dot_filename=nil) click to toggle source

Create erd image

generate a dot source code, save it to a file and create a erd image out of it.

# File lib/doterd.rb, line 95
def self.viz(dot_filename=nil)
  dot_filename ||= Doterd.config[:dot_filename]
  File.open(dot_filename, 'w') { |f| f.write dot }
  system("dot -O -T#{Doterd.config[:output_type]} #{dot_filename}")
end