Class: RailsFlowMap::ErdFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_flow_map/formatters/erd_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ ErdFormatter

Returns a new instance of ErdFormatter.



3
4
5
# File 'lib/rails_flow_map/formatters/erd_formatter.rb', line 3

def initialize(graph)
  @graph = graph
end

Instance Method Details

#format(graph = @graph) ⇒ Object



7
8
9
10
11
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
# File 'lib/rails_flow_map/formatters/erd_formatter.rb', line 7

def format(graph = @graph)
  output = []
  output << "# Entity Relationship Diagram\n"
  output << "```"
  
  # モデルのみを抽出
  models = graph.nodes_by_type(:model)
  
  models.each do |model|
    output << format_model_box(model)
    output << ""
  end
  
  # 関係性を表示
  output << "## Relationships\n"
  graph.edges_by_type(:belongs_to).each do |edge|
    from_model = graph.find_node(edge.from)
    to_model = graph.find_node(edge.to)
    output << "#{from_model.name} belongs_to #{to_model.name}"
  end
  
  graph.edges_by_type(:has_many).each do |edge|
    from_model = graph.find_node(edge.from)
    to_model = graph.find_node(edge.to)
    output << "#{from_model.name} has_many #{to_model.name}"
  end
  
  output << "```"
  output.join("\n")
end