Module: Mermaid::ElementRenderers::ERDEntityRenderer

Defined in:
lib/mermaid/element_renderers/erd_entity_renderer.rb

Overview

Module containing Mermaid rendering logic for Diagrams::ElementRenderers::ERDEntity

Instance Method Summary collapse

Instance Method Details

#to_mermaid_fragmentString

Generates the Mermaid fragment for this entity.

Returns:

  • (String)

    The Mermaid syntax fragment for the entity.



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
# File 'lib/mermaid/element_renderers/erd_entity_renderer.rb', line 9

def to_mermaid_fragment
  fragment = "\"#{name}\" {\n"
  entity_attributes.each do |attr|
    fragment << "  #{attr.type} #{attr.name}"

    # Separate Mermaid-native constraints from comment-only constraints
    unless attr.keys.empty?
      native_keys = attr.keys.select { |key| %i[PK FK].include?(key) }
      comment_keys = attr.keys.select { |key| [:UK].include?(key) }

      # Add native keys first
      fragment << " #{native_keys.join(' ')}" unless native_keys.empty?

      # Quote UK as comment to mark it as comment
      comment_keys.each do |key|
        fragment << " \"#{key}\""
      end
    end

    # Add explicit comment if present
    fragment << " \"#{attr.comment}\"" if attr.comment && !attr.comment.empty?
    fragment << "\n"
  end
  fragment << "}\n"
  fragment
end