Class: RailsFlowMap::SequenceFormatter

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

Overview

Generates sequence diagrams showing request flow through the application

This formatter creates Mermaid sequence diagrams that visualize how requests flow through controllers, services, models, and the database. It can optionally include middleware, callbacks, and validation steps.

Examples:

Basic usage

formatter = SequenceFormatter.new(graph, endpoint: '/api/users')
diagram = formatter.format

With all features enabled

formatter = SequenceFormatter.new(graph, {
  endpoint: '/api/users',
  include_middleware: true,
  include_callbacks: true,
  include_validations: true,
  include_database: true
})

Instance Method Summary collapse

Constructor Details

#initialize(graph, options = {}) ⇒ SequenceFormatter

Creates a new sequence diagram formatter

Parameters:

  • graph (FlowGraph)

    The graph to analyze

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :endpoint (String)

    The endpoint to analyze (analyzes all if nil)

  • :include_middleware (Boolean)

    Include middleware in diagram (default: false)

  • :include_callbacks (Boolean)

    Include callbacks in diagram (default: false)

  • :include_validations (Boolean)

    Include validations in diagram (default: false)

  • :include_database (Boolean)

    Include database queries in diagram (default: true)



31
32
33
34
35
36
37
38
# File 'lib/rails_flow_map/formatters/sequence_formatter.rb', line 31

def initialize(graph, options = {})
  @graph = graph
  @endpoint = options[:endpoint]
  @include_middleware = options[:include_middleware] || false
  @include_callbacks = options[:include_callbacks] || false
  @include_validations = options[:include_validations] || false
  @include_database = options[:include_database] || true
end

Instance Method Details

#format(graph = @graph) ⇒ String

Generates the sequence diagram

Parameters:

  • graph (FlowGraph) (defaults to: @graph)

    Optional graph to format (uses instance graph by default)

Returns:

  • (String)

    Mermaid sequence diagram markup



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_flow_map/formatters/sequence_formatter.rb', line 44

def format(graph = @graph)
  output = []
  output << "```mermaid"
  output << "sequenceDiagram"
  output << "    autonumber"
  
  # 参加者の定義
  output.concat(define_participants(graph))
  
  # エンドポイントに関連するノードを取得
  route_nodes = filter_route_nodes(graph)
  
  if route_nodes.empty?
    output << "    Note over Client: No routes found for endpoint: #{@endpoint}"
  else
    route_nodes.each do |route|
      output << ""
      output << "    Note over Client: === #{route.attributes[:verb]} #{route.attributes[:path]} ==="
      output.concat(generate_sequence_for_route(route, graph))
    end
  end
  
  output << "```"
  output.join("\n")
end