Class: MermaidErdMarkdown::MarkdownDocument

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMarkdownDocument

Returns a new instance of MarkdownDocument.



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

def initialize
  @is_show_key = true
  @is_show_comment = true
end

Instance Attribute Details

#is_show_commentObject

Returns the value of attribute is_show_comment.



5
6
7
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 5

def is_show_comment
  @is_show_comment
end

#is_show_keyObject

Returns the value of attribute is_show_key.



5
6
7
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 5

def is_show_key
  @is_show_key
end

Class Method Details

.create(&block) ⇒ Object



7
8
9
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 7

def self.create(&block)
  new.generate(&block)
end

Instance Method Details

#add(element) ⇒ Object



16
17
18
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 16

def add(element)
  document << element
end

#documentObject



20
21
22
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 20

def document
  @document ||= []
end

#erd(&block) ⇒ Object



24
25
26
27
28
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 24

def erd(&block)
  add(erd_header)
  block.arity == 1 ? block[self] : instance_eval(&block)
  add(erd_footer)
end


30
31
32
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 30

def erd_footer
  "```"
end

#erd_headerObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 34

def erd_header
  [
    "```mermaid",
    "erDiagram",
    "    %% --------------------------------------------------------",
    "    %% Entity-Relationship Diagram",
    "    %% --------------------------------------------------------",
    ""
  ].join("\n")
end

#erd_relation(relation) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 45

def erd_relation(relation)
  left_model_name = relation[:LeftModelName].tr(":", "-")
  right_model_name = relation[:RightModelName].tr(":", "-")
  comment = is_show_comment ? ": \"#{relation[:Comment]}\"" : ": \"\""

  "    #{left_model_name} #{relation[:LeftValue]}" \
    "#{relation[:Line]}#{relation[:RightValue]} " \
    "#{right_model_name} #{comment}"
end

#erd_table(table_name, model_name) ⇒ Object



55
56
57
58
59
60
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 55

def erd_table(table_name, model_name)
  lines = [erd_table_header(table_name, model_name)]
  lines << yield
  lines << erd_table_footer
  lines.join("\n")
end

#erd_table_column(column) ⇒ Object



62
63
64
65
66
67
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 62

def erd_table_column(column)
  key = is_show_key ? column[:key] : ""
  line = "        #{column[:type]} #{column[:name]}"
  line << " #{key}" unless key.empty?
  line
end


76
77
78
79
80
81
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 76

def erd_table_footer
  [
    "    }",
    ""
  ].join("\n")
end

#erd_table_header(table_name, model_name) ⇒ Object



69
70
71
72
73
74
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 69

def erd_table_header(table_name, model_name)
  [
    "    %% table name: #{table_name}",
    "    #{model_name.tr(":", "-")}{"
  ].join("\n")
end

#generate(&block) ⇒ Object



87
88
89
90
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 87

def generate(&block)
  block.arity == 1 ? block[self] : instance_eval(&block)
  document.join("\n")
end

#header(text) ⇒ Object



83
84
85
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 83

def header(text)
  "# #{text}\n"
end


92
93
94
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 92

def link(text, url)
  "[#{text}](#{url})"
end

#list_item(text) ⇒ Object



96
97
98
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 96

def list_item(text)
  "- #{text}"
end

#subheader(text) ⇒ Object



100
101
102
# File 'lib/rails-mermaid_erd_markdown/markdown_document.rb', line 100

def subheader(text)
  "## #{text}\n"
end