Class: Ea::Svg::EaEmitter::Label::MidpointLabel

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/svg/ea_emitter/label/midpoint_label.rb

Overview

Renders the midpoint label for a connector. EA emits one of two things at the midpoint, but not both:

1. «stereotype» when the relationship has an applied
 stereotype (e.g. «import», «realize»).
2. The relationship's Name when no stereotype is applied
 and Name is non-empty (e.g. "Association A").

Stereotype wins when both are present.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas:, model_index:, font_family:, font_size:, font_unit:) ⇒ MidpointLabel

Returns a new instance of MidpointLabel.



20
21
22
23
24
25
26
27
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 20

def initialize(canvas:, model_index:, font_family:, font_size:,
               font_unit:)
  @canvas = canvas
  @model_index = model_index
  @font_family = font_family
  @font_size = font_size
  @font_unit = font_unit
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



17
18
19
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 17

def canvas
  @canvas
end

#font_familyObject (readonly)

Returns the value of attribute font_family.



17
18
19
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 17

def font_family
  @font_family
end

#font_sizeObject (readonly)

Returns the value of attribute font_size.



17
18
19
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 17

def font_size
  @font_size
end

#font_unitObject (readonly)

Returns the value of attribute font_unit.



17
18
19
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 17

def font_unit
  @font_unit
end

#model_indexObject (readonly)

Returns the value of attribute model_index.



17
18
19
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 17

def model_index
  @model_index
end

Instance Method Details

#midpoint_content(connector) ⇒ Object

Returns the midpoint label content: stereotype if present, else the relationship Name.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 53

def midpoint_content(connector)
  stereotype = stereotype_label(connector)
  return stereotype if stereotype

  rel = relationship_for(connector)
  return nil unless rel

  name = rel.name
  return nil if name.nil? || name.to_s.empty?

  name.to_s
end

#relationship_for(connector) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 66

def relationship_for(connector)
  return nil unless model_index

  rel = model_index[connector.relationship_ref]
  return nil unless rel.is_a?(Ea::Model::Relationship)

  rel
end

#stereotype_label(connector) ⇒ Object

Returns the formatted «stereotype» string or nil. Used by Label::Registry to dispatch end-label vs midpoint.



41
42
43
44
45
46
47
48
49
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 41

def stereotype_label(connector)
  rel = relationship_for(connector)
  return nil unless rel

  stereotype = rel.stereotype
  return nil if stereotype.nil? || stereotype.to_s.empty?

  "«#{stereotype}»"
end

#text_for(connector, points) ⇒ Object

Returns the SVG <text> for the connector's midpoint label, or nil if there is no label to render.



31
32
33
34
35
36
37
# File 'lib/ea/svg/ea_emitter/label/midpoint_label.rb', line 31

def text_for(connector, points)
  content = midpoint_content(connector)
  return nil if content.nil?

  mx, my = midpoint(points)
  render_text(mx, my, content)
end