Module: Ea::Svg::EaEmitter::Compartment::InstanceSlots

Defined in:
lib/ea/svg/ea_emitter/compartment/instance_slots.rb

Overview

Instance slot compartment. Renders each slot on an InstanceSpecification as "name op value", then appends "(from PackageName)" in italics below the slots when the instance has a classifier. Skipped entirely when the model element is not an InstanceSpecification or has neither slots nor a classifier.

Constant Summary collapse

ROW_OFFSET =
13
SUBTITLE_EXTRA_OFFSET =
10

Class Method Summary collapse

Class Method Details

.from_package_subtitle(instance) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ea/svg/ea_emitter/compartment/instance_slots.rb', line 47

def from_package_subtitle(instance)
  pkg = instance.package_name
  return nil if pkg.nil? || pkg.empty?

  classifier = instance.classifier_name
  return nil if classifier.nil? || classifier.empty?

  "(from #{pkg})"
end

.instance?(context) ⇒ Object



42
43
44
# File 'lib/ea/svg/ea_emitter/compartment/instance_slots.rb', line 42

def instance?(context)
  context.model_element.is_a?(Ea::Model::InstanceSpecification)
end

.line_text(slot, bounds, y_pos, context, fill) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/ea/svg/ea_emitter/compartment/instance_slots.rb', line 58

def line_text(slot, bounds, y_pos, context, fill)
  content = "#{slot.name} #{slot.op} #{slot.value}"
  TextRenderer.new(
    content: content,
    x: bounds.x + (context.theme.attribute_spec.content_x_offset || 26),
    y: y_pos,
    family: context.family, size: context.size,
    size_unit: context.size_unit, fill: fill
  ).to_svg
end

.render(context) ⇒ Object



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

def render(context)
  return nil unless instance?(context)

  instance = context.model_element
  slots = instance.slots || []
  subtitle = from_package_subtitle(instance)
  return nil if slots.empty? && subtitle.nil?

  first_y = context.geometry.attr_first_y
  bounds = context.bounds
  fill = context.theme.attribute_text_color
  blocks = []
  slots.each_with_index do |slot, idx|
    y = first_y + (idx * ROW_OFFSET)
    blocks << line_text(slot, bounds, y, context, fill)
  end
  if subtitle
    y = first_y + (slots.size * ROW_OFFSET) + SUBTITLE_EXTRA_OFFSET
    blocks << subtitle_text(subtitle, bounds, y, context, fill)
  end
  wrap(blocks.join("\n"), fill)
end

.subtitle_text(text, bounds, y_pos, context, fill) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ea/svg/ea_emitter/compartment/instance_slots.rb', line 70

def subtitle_text(text, bounds, y_pos, context, fill)
  estimated_width = text.length * context.size * 0.5
  x_pos = bounds.x + (bounds.width - estimated_width) / 2.0
  TextRenderer.new(
    content: text,
    x: x_pos,
    y: y_pos,
    family: context.family, size: context.size,
    size_unit: context.size_unit, fill: fill,
    style: "italic"
  ).to_svg
end

.wrap(body, fill) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/ea/svg/ea_emitter/compartment/instance_slots.rb', line 84

def wrap(body, fill)
  group_style = "stroke-width:1;stroke-linecap:round;" \
                "stroke-linejoin:bevel; fill:#{fill};" \
                "fill-opacity:1.00; stroke:#000000;" \
                " stroke-opacity:0.00"
  %(<g style="#{group_style}">\n#{body}\n</g>)
end