Class: ERDBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-erd/erd_builder.rb

Direct Known Subclasses

RubyERD

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_node_background_colorObject

Returns the value of attribute default_node_background_color.



3
4
5
# File 'lib/ruby-erd/erd_builder.rb', line 3

def default_node_background_color
  @default_node_background_color
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/ruby-erd/erd_builder.rb', line 4

def name
  @name
end

Instance Method Details

#add_entity(name) ⇒ Object



6
7
8
9
10
11
# File 'lib/ruby-erd/erd_builder.rb', line 6

def add_entity(name)
  ERDEntity.new.tap do |entity|
    entity.build(graph: graph, name: name)
    entity.background_color = node_background_color
  end
end

#associate(entity, other_entity, attributes) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/ruby-erd/erd_builder.rb', line 13

def associate(entity, other_entity, attributes)
  case other_entity
  when Array
    diagram.add_edges(entity.node, other_entity.collect(&:node), edge_attributes_for(attributes))
  else
    diagram.add_edges(entity.node, other_entity.node, edge_attributes_for(attributes))
  end
end

#begin_system(name) ⇒ Object



22
23
24
25
26
27
# File 'lib/ruby-erd/erd_builder.rb', line 22

def begin_system(name)
  @name = name

  # Initialize these objects
  graph
end

#diagramObject

Public: Returns the current diagram, or a new instance if there is none.

For a list of available attributes, see:

* https://github.com/glejeune/Ruby-Graphviz/blob/master/lib/graphviz/constants.rb
* http://www.graphviz.org/content/attrs

Ruby-Graphviz overrides []= on GraphViz objects so to set an attribute, treat the GraphViz object like a hash.

Example: diagram = value

Returns a GraphViz instance



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby-erd/erd_builder.rb', line 41

def diagram
  @diagram ||= GraphViz.digraph(name) do |diagram|
    # Prevent node overlap
    # http://www.graphviz.org/content/attrs#doverlap
    diagram[:overlap] = 'false'

    # Draw straight lines between nodes
    # http://www.graphviz.org/content/attrs#dsplines
    diagram[:splines] = 'true'

    # Contents of the diagram's label
    # http://www.graphviz.org/content/attrs#dlabel
    diagram[:label] = name

    # Label location: Display the title at the top 't' of the diagram
    # http://www.graphviz.org/content/attrs#dlabelloc
    diagram[:labelloc] = "t"
  end
end

#export_as_pngObject



61
62
63
# File 'lib/ruby-erd/erd_builder.rb', line 61

def export_as_png
  diagram.output(png: "#{name}.png")
end

#graphObject



65
66
67
68
69
70
# File 'lib/ruby-erd/erd_builder.rb', line 65

def graph
  @graph ||= diagram.add_graph(name).tap do |graph|
    graph[:overlap] = 'false'
    graph[:splines] = 'true'
  end
end

#join(entity, other_entity, label = "joins") ⇒ Object



72
73
74
75
76
77
78
# File 'lib/ruby-erd/erd_builder.rb', line 72

def join(entity, other_entity, label="joins")
  join_table = add_entity("#{entity.name}#{other_entity.name}")
  join_table.background_color = '#5182FF'

  associate(entity, join_table, {association: :one_to_many, name: label})
  associate(join_table, other_entity, {association: :many_to_one, name: ""})
end