Class: Marvi::Diagram::ClassRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/marvi/diagram.rb

Overview

Renders ‘classDiagram` as UML class boxes followed by their relationships.

Constant Summary collapse

RELATION_GLYPHS =

Maps a Mermaid relationship operator to a readable connector glyph. Ordered so multi-character markers are tested before their substrings.

[
  ["<|--", "◁──"], ["--|>", "──▷"],
  ["<|..", "◁╌╌"], ["..|>", "╌╌▷"],
  ["*--", "◆──"], ["--*", "──◆"],
  ["o--", "◇──"], ["--o", "──◇"],
  ["-->", "──▶"], ["<--", "◀──"],
  ["..>", "╌╌▶"], ["<..", "◀╌╌"],
  ["--", "───"], ["..", "╌╌╌"]
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(lines, max_width) ⇒ ClassRenderer

Returns a new instance of ClassRenderer.



490
491
492
493
494
495
496
# File 'lib/marvi/diagram.rb', line 490

def initialize(lines, max_width)
  @lines = lines
  @max_width = max_width
  @members = {}
  @order = []
  @relations = []
end

Instance Method Details

#renderObject



498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/marvi/diagram.rb', line 498

def render
  return nil unless parse
  return nil if @order.empty?

  spans = []
  @order.each_with_index do |name, i|
    spans << [Span.new(text: "", color: COLOR)] unless i.zero?
    spans.concat(class_box(name, @members[name]))
  end
  unless @relations.empty?
    spans << [Span.new(text: "", color: COLOR)]
    @relations.each { |line| spans << [Span.new(text: line, color: COLOR)] }
  end

  return nil if spans.any? { |line| Unicode::DisplayWidth.of(line.first.text) > @max_width }
  spans
end