Class: MermaidErdMarkdown::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-mermaid_erd_markdown/generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



12
13
14
# File 'lib/rails-mermaid_erd_markdown/generator.rb', line 12

def logger=(value)
  @logger = value
end

Class Method Details

.generateObject



14
15
16
# File 'lib/rails-mermaid_erd_markdown/generator.rb', line 14

def self.generate
  new.generate
end

Instance Method Details

#generateObject



18
19
20
21
22
23
24
25
# File 'lib/rails-mermaid_erd_markdown/generator.rb', line 18

def generate
  return unless find_or_create_output
  return unless erd_changed?

  update_erd

  update_split_erds if configuration.split_output
end

#index_markdown(files) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails-mermaid_erd_markdown/generator.rb', line 27

def index_markdown(files)
  MermaidErdMarkdown::MarkdownDocument.create do |doc|
    doc.add(doc.header("Entity Relationship Diagrams"))
    doc.add(
      "Each model has its own ERD diagram. The diagram shows the " \
      "selected model, plus #{configuration.relationship_depth} " \
      "associated model(s) deep. Click on the model name to view " \
      "the diagram.\n"
    )
    doc.add(doc.subheader("Models"))
    files.each do |model, path|
      doc.add(doc.list_item(doc.link(model, "../#{path}")))
    end
    doc.add("")
  end
end

#mermaid_markdown(source) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rails-mermaid_erd_markdown/generator.rb', line 62

def mermaid_markdown(source)
  MermaidErdMarkdown::MarkdownDocument.create do
    erd do
      add(
        source[:Models].map do |model|
          erd_table(model[:TableName], model[:ModelName]) do
            model[:Columns].map do |column|
              erd_table_column(column)
            end
          end
        end
      )
      add(
        source[:Relations].map do |relation|
          erd_relation(relation)
        end
      )
    end
  end
end

#model_markdown(output) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails-mermaid_erd_markdown/generator.rb', line 44

def model_markdown(output)
  MermaidErdMarkdown::MarkdownDocument.create do |doc|
    model_name = output[:Models].first[:ModelName]
    doc.add(doc.header("#{model_name} Entity-Relationship Diagram"))
    doc.add(doc.subheader("Associated Models"))
    output[:Models].each do |model|
      next if model[:ModelName] == model_name

      model_path = "../#{output_path(model[:ModelName])}"

      doc.add(doc.list_item(doc.link(model[:ModelName], model_path)))
    end
    doc.add("")
    doc.add(doc.subheader("Entity-Relationship Diagram"))
    doc.add(mermaid_markdown(output))
  end
end