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 = 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



84
85
86
87
88
89
90
91
# File 'lib/archimate/svg/entity/base_entity.rb', line 84

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

#entity_label(xml) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/archimate/svg/entity/base_entity.rb', line 61

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



118
119
120
121
122
123
124
# File 'lib/archimate/svg/entity/base_entity.rb', line 118

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

#layer_background_classObject



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

def layer_background_class
  case child.element&.layer
  when "Strategy"
    "archimate-strategy-background"
  when "Business"
    "archimate-business-background"
  when "Application"
    "archimate-application-background"
  when "Technology"
    "archimate-infrastructure-background"
  when "Physical"
    "archimate-physical-background"
  when "Motivation"
    "archimate-motivation-background"
  when "Implementation and Migration"
    "archimate-implementation-background"
  end
end


57
58
59
# File 'lib/archimate/svg/entity/base_entity.rb', line 57

def optional_link(xml, &block)
  block.call
end

#shape_styleObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/archimate/svg/entity/base_entity.rb', line 93

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



80
81
82
# File 'lib/archimate/svg/entity/base_entity.rb', line 80

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

#text_styleObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/archimate/svg/entity/base_entity.rb', line 105

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



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

def to_svg(xml)
  optional_link(xml) {
    xml.g(group_attrs) do
      xml.title { xml.text @entity.name } unless @entity.name.nil? || @entity.name.empty?
      xml.desc { xml.text(@entity.documentation.map(&:text).join("\n\n")) } 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