Class: Ea::Svg::ElementBox

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/svg/element_box.rb

Overview

Renders one DiagramElement as an SVG containing the rect and a name label. Class-shape rendering (with attribute / operation compartments) is future work; for now we emit a simple labeled box that mirrors EA's logical placement.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, model_index:) ⇒ ElementBox

Returns a new instance of ElementBox.



14
15
16
17
# File 'lib/ea/svg/element_box.rb', line 14

def initialize(element, model_index:)
  @element = element
  @model_index = model_index
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



12
13
14
# File 'lib/ea/svg/element_box.rb', line 12

def element
  @element
end

#model_indexObject (readonly)

Returns the value of attribute model_index.



12
13
14
# File 'lib/ea/svg/element_box.rb', line 12

def model_index
  @model_index
end

Instance Method Details

#renderObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ea/svg/element_box.rb', line 19

def render
  return "" unless element.bounds

  b = normalize_bounds(element.bounds)
  style = StyleResolver.new(element.style)
  label_text = model_element_name

  <<~SVG.chomp
    <g class="element" data-element-id="#{escape(element.id)}">
      <rect x="#{b.x}" y="#{b.y}" width="#{b.width}" height="#{b.height}"
            fill="#{style.fill_color}" stroke="#{style.stroke_color}"
            stroke-width="#{style.stroke_width}"/>
      <text x="#{b.x + (b.width / 2.0)}"
            y="#{b.y + (b.height / 2.0)}"
            text-anchor="middle" dominant-baseline="middle"
            fill="#{style.font_color}"
            font-family="sans-serif" font-size="14">#{escape(label_text)}</text>
    </g>
  SVG
end