Class: Dbwatcher::Services::MermaidSyntax::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/dbwatcher/services/mermaid_syntax/sanitizer.rb

Overview

Utility class for sanitizing names and labels for Mermaid syntax

Provides methods to sanitize various types of identifiers for use in Mermaid diagrams, ensuring valid syntax.

Class Method Summary collapse

Class Method Details

.attribute_type(type) ⇒ String

Sanitize attribute type for Mermaid ERD



99
100
101
102
103
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 99

def attribute_type(type)
  return "string" unless type.is_a?(String) && !type.empty?

  type.to_s.gsub(/[^a-zA-Z0-9_]/, "_")
end

.class_name(name) ⇒ String

Sanitize class name for Mermaid class diagrams



16
17
18
19
20
21
22
23
24
25
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 16

def class_name(name)
  return "UnknownClass" unless name.is_a?(String) && !name.empty?

  # For namespaced models, preserve the namespace structure
  # Convert :: to underscore for Mermaid syntax while maintaining readability
  sanitized = name.to_s.gsub("::", "__")

  # Only replace other special characters with underscores
  sanitized.gsub(/[^a-zA-Z0-9_]/, "_")
end

.display_name(name) ⇒ String

Get display name for class (preserves namespace format for labels)



88
89
90
91
92
93
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 88

def display_name(name)
  return "UnknownClass" unless name.is_a?(String) && !name.empty?

  # Return the original name for display purposes (preserves :: for namespaces)
  name.to_s
end

.label(label) ⇒ String

Sanitize relationship label for Mermaid diagrams



76
77
78
79
80
81
82
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 76

def label(label)
  return "" unless label.is_a?(String) && !label.empty?

  # For Mermaid, we need to escape quotes but not remove them completely
  # We'll escape backslashes and double quotes, and replace newlines with spaces
  label.to_s.gsub("\\", "\\\\").gsub('"', '\\"').gsub(/[\n\r]/, " ").strip
end

.method_name(name) ⇒ String

Sanitize method name for Mermaid class diagrams



63
64
65
66
67
68
69
70
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 63

def method_name(name)
  return "unknown_method()" unless name.is_a?(String) && !name.empty?

  # Ensure method name ends with parentheses
  method = name.to_s.gsub(/[^a-zA-Z0-9_()]/, "_")
  method += "()" unless method.include?("(")
  method
end

.node_id(name) ⇒ String

Sanitize node ID for Mermaid flowcharts



41
42
43
44
45
46
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 41

def node_id(name)
  return "unknown_node" unless name.is_a?(String) && !name.empty?

  # Node IDs in flowcharts must be valid identifiers
  name.to_s.gsub(/[^a-zA-Z0-9_]/, "_")
end

.node_name(name) ⇒ String

Sanitize node name for Mermaid flowcharts



31
32
33
34
35
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 31

def node_name(name)
  return "unknown_node" unless name.is_a?(String) && !name.empty?

  name.to_s.gsub(/[^a-zA-Z0-9_]/, "_")
end

.table_name(name) ⇒ String

Sanitize table name for Mermaid ERD



52
53
54
55
56
57
# File 'lib/dbwatcher/services/mermaid_syntax/sanitizer.rb', line 52

def table_name(name)
  return "UNKNOWN_TABLE" unless name.is_a?(String) && !name.empty?

  # Always preserve original case
  name.to_s.gsub(/[^a-zA-Z0-9_]/, "_")
end