Class: Archimate::Svg::Entity::BaseEntity

Inherits:
Object
  • Object
show all
Defined in:
lib/archimate/svg/entity/base_entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(child, bounds_offset) ⇒ BaseEntity

Returns a new instance of BaseEntity.



15
16
17
18
19
20
21
22
23
# File 'lib/archimate/svg/entity/base_entity.rb', line 15

def initialize(child, bounds_offset)
  @child = child
  @text_bounds = child.bounds.reduced_by(2)
  @bounds_offset = bounds_offset
  @entity = @child.element || @child
  @background_class = @child&.element&.layer&.background_class
  @text_align = nil
  @badge = nil
end

Instance Attribute Details

#background_classObject (readonly)

Returns the value of attribute background_class.



13
14
15
# File 'lib/archimate/svg/entity/base_entity.rb', line 13

def background_class
  @background_class
end

#badgeObject (readonly)

Returns the value of attribute badge.



12
13
14
# File 'lib/archimate/svg/entity/base_entity.rb', line 12

def badge
  @badge
end

#badge_boundsObject (readonly)

Returns the value of attribute badge_bounds.



11
12
13
# File 'lib/archimate/svg/entity/base_entity.rb', line 11

def badge_bounds
  @badge_bounds
end

#bounds_offsetObject (readonly)

Returns the value of attribute bounds_offset.



9
10
11
# File 'lib/archimate/svg/entity/base_entity.rb', line 9

def bounds_offset
  @bounds_offset
end

#childObject (readonly)

Returns the value of attribute child.



7
8
9
# File 'lib/archimate/svg/entity/base_entity.rb', line 7

def child
  @child
end

#entityObject (readonly)

Returns the value of attribute entity.



8
9
10
# File 'lib/archimate/svg/entity/base_entity.rb', line 8

def entity
  @entity
end

#text_boundsObject (readonly)

Returns the value of attribute text_bounds.



10
11
12
# File 'lib/archimate/svg/entity/base_entity.rb', line 10

def text_bounds
  @text_bounds
end

Instance Method Details

#entity_badge(xml) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/archimate/svg/entity/base_entity.rb', line 65

def entity_badge(xml)
  return if badge.nil?
  xml.use(
    badge_bounds
      .to_h
      .merge("xlink:href" => badge)
  )
end

#entity_label(xml) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/archimate/svg/entity/base_entity.rb', line 42

def entity_label(xml)
  return if (entity.nil? || entity.name.nil? || entity.name.strip.empty?) && (child.content.nil? || child.content.strip.empty?)
  xml.foreignObject(text_bounds.to_h) do
    xml.table(xmlns: "http://www.w3.org/1999/xhtml", style: "height:#{text_bounds.height}px;width:#{text_bounds.width}px;") do
      xml.tr(style: "height:#{text_bounds.height}px;") do
        xml.td(class: "entity-name") do
          xml.div(class: "archimate-badge-spacer") unless badge.nil?
          xml.p(class: "entity-name", style: text_style) do
            text_lines(entity.name || child.content).each do |line|
              xml.text(line)
              xml.br
            end
          end
        end
      end
    end
  end
end

#group_attrsObject



99
100
101
102
103
104
105
# File 'lib/archimate/svg/entity/base_entity.rb', line 99

def group_attrs
  attrs = { id: @entity.id }
  # TODO: Transform only needed only for Archi file types
  attrs[:transform] = "translate(#{@bounds_offset.x || 0}, #{@bounds_offset.y || 0})" unless @bounds_offset.nil?
  attrs[:class] = "archimate-#{@entity.type}" if @entity.type || !@entity.type.empty?
  attrs
end


38
39
40
# File 'lib/archimate/svg/entity/base_entity.rb', line 38

def optional_link(_xml)
  yield
end

#shape_styleObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/archimate/svg/entity/base_entity.rb', line 74

def shape_style
  style = child.style
  return "" if style.nil?
  {
    "fill": style.fill_color&.to_rgba,
    "stroke": style.line_color&.to_rgba,
    "stroke-width": style.line_width
  }.delete_if { |_key, value| value.nil? }
    .map { |key, value| "#{key}:#{value};" }
    .join("")
end

#text_lines(text) ⇒ Object



61
62
63
# File 'lib/archimate/svg/entity/base_entity.rb', line 61

def text_lines(text)
  text.tr("\r\n", "\n").split(/[\r\n]/)
end

#text_styleObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/archimate/svg/entity/base_entity.rb', line 86

def text_style
  style = child.style || DataModel::Style.new
  {
    "fill": style.font_color&.to_rgba,
    "color": style.font_color&.to_rgba,
    "font-family": style.font&.name,
    "font-size": style.font&.size,
    "text-align": style.text_align || @text_align
  }.delete_if { |_key, value| value.nil? }
    .map { |key, value| "#{key}:#{value};" }
    .join("")
end

#to_svg(xml) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/archimate/svg/entity/base_entity.rb', line 25

def to_svg(xml)
  optional_link(xml) do
    xml.g(group_attrs) do
      xml.title { xml.text @entity.name } unless @entity.name.nil? || @entity.name.empty?
      xml.desc { xml.text(@entity.documentation.to_s) } unless @entity.documentation&.empty?
      entity_shape(xml, child.bounds)
      entity_badge(xml)
      entity_label(xml)
      child.nodes.each { |c| Svg::EntityFactory.make_entity(c, child.bounds).to_svg(xml) }
    end
  end
end