Class: DrawioDsl::Schema::Shape

Inherits:
Node
  • Object
show all
Includes:
Formatters::Factory
Defined in:
lib/drawio_dsl/schema/shape.rb

Overview

Shape is a graphical element, it can be a shape, a text, or a group (todo)

Direct Known Subclasses

Element, Line, Text

Constant Summary

Constants included from Formatters::Factory

Formatters::Factory::FORMATTERS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Node

#classification, #id, #key, #nodes, #page, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Formatters::Factory

#format_instance, #formatter

Methods inherited from Node

#add_node, #debug_detail, #debug_row, #root?

Constructor Details

#initialize(page, **args, &block) ⇒ Shape

Returns a new instance of Shape.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/drawio_dsl/schema/shape.rb', line 51

def initialize(page, **args, &block)
  args[:classification] = :shape
  super(page, **args)

  @style_builder = DrawioDsl::Schema::StyleBuilder.new

  apply_defaults(args)

  instance_eval(&block) if block_given?

  @value = formatter.empty? ? title : formatter.as_html
  @meta_data = formatter.empty? ? { items: [{ type: :title, content: title }] } : formatter.to_h
end

Class Attribute Details

.shape_categoryObject (readonly)

Returns the value of attribute shape_category.



11
12
13
# File 'lib/drawio_dsl/schema/shape.rb', line 11

def shape_category
  @shape_category
end

.shape_defaultsObject (readonly)

Returns the value of attribute shape_defaults.



12
13
14
# File 'lib/drawio_dsl/schema/shape.rb', line 12

def shape_defaults
  @shape_defaults
end

.shape_keyObject (readonly)

Returns the value of attribute shape_key.



10
11
12
# File 'lib/drawio_dsl/schema/shape.rb', line 10

def shape_key
  @shape_key
end

Instance Attribute Details

#fill_colorObject

Returns the value of attribute fill_color.



40
41
42
# File 'lib/drawio_dsl/schema/shape.rb', line 40

def fill_color
  @fill_color
end

#font_colorObject

Returns the value of attribute font_color.



42
43
44
# File 'lib/drawio_dsl/schema/shape.rb', line 42

def font_color
  @font_color
end

#glassObject

Returns the value of attribute glass.



37
38
39
# File 'lib/drawio_dsl/schema/shape.rb', line 37

def glass
  @glass
end

#gradientObject

Returns the value of attribute gradient.



43
44
45
# File 'lib/drawio_dsl/schema/shape.rb', line 43

def gradient
  @gradient
end

#hObject

Returns the value of attribute h.



48
49
50
# File 'lib/drawio_dsl/schema/shape.rb', line 48

def h
  @h
end

#htmlObject

Returns the value of attribute html.



34
35
36
# File 'lib/drawio_dsl/schema/shape.rb', line 34

def html
  @html
end

#roundedObject

Returns the value of attribute rounded.



35
36
37
# File 'lib/drawio_dsl/schema/shape.rb', line 35

def rounded
  @rounded
end

#shadowObject

Returns the value of attribute shadow.



36
37
38
# File 'lib/drawio_dsl/schema/shape.rb', line 36

def shadow
  @shadow
end

#sketchObject

Returns the value of attribute sketch.



38
39
40
# File 'lib/drawio_dsl/schema/shape.rb', line 38

def sketch
  @sketch
end

#stroke_colorObject

Returns the value of attribute stroke_color.



41
42
43
# File 'lib/drawio_dsl/schema/shape.rb', line 41

def stroke_color
  @stroke_color
end

#style_modifiersObject

Returns the value of attribute style_modifiers.



49
50
51
# File 'lib/drawio_dsl/schema/shape.rb', line 49

def style_modifiers
  @style_modifiers
end

#themeObject

Returns the value of attribute theme.



28
29
30
# File 'lib/drawio_dsl/schema/shape.rb', line 28

def theme
  @theme
end

#titleObject

Returns the value of attribute title.



29
30
31
# File 'lib/drawio_dsl/schema/shape.rb', line 29

def title
  @title
end

#valueObject

Returns the value of attribute value.



30
31
32
# File 'lib/drawio_dsl/schema/shape.rb', line 30

def value
  @value
end

#wObject

Returns the value of attribute w.



47
48
49
# File 'lib/drawio_dsl/schema/shape.rb', line 47

def w
  @w
end

#white_spaceObject

The style of the element, these will derive from the page style if not provided



33
34
35
# File 'lib/drawio_dsl/schema/shape.rb', line 33

def white_space
  @white_space
end

#xObject

Returns the value of attribute x.



45
46
47
# File 'lib/drawio_dsl/schema/shape.rb', line 45

def x
  @x
end

#yObject

Returns the value of attribute y.



46
47
48
# File 'lib/drawio_dsl/schema/shape.rb', line 46

def y
  @y
end

Class Method Details

.configure_shape(key, category) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/drawio_dsl/schema/shape.rb', line 14

def configure_shape(key, category)
  item_config = KConfig.configuration.drawio.get_item_by_category(key, category)

  if item_config.nil?
    puts "#{key} not found in configuration for category: #{category}"
    return
  end

  @shape_key = key
  @shape_category = category
  @shape_defaults = item_config.clone
end

Instance Method Details

#add_base_modifiersObject



150
# File 'lib/drawio_dsl/schema/shape.rb', line 150

def add_base_modifiers; end

#apply_defaults(args) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/drawio_dsl/schema/shape.rb', line 70

def apply_defaults(args)
  @theme            = args[:theme] || page.theme # KConfig.configuration.drawio.themes.sample
  @title            = args[:title]            || ''

  @white_space      = args[:white_space]      || page.style.white_space # wrap or nil
  @html             = args[:html]             || page.style.html
  @rounded          = args[:rounded]          || page.style.rounded
  @shadow           = args[:shadow]           || page.style.shadow
  @sketch           = args[:sketch]           || page.style.sketch
  @glass            = args[:glass]            || page.style.glass

  @key              = args[:key]              || shape_defaults.key
  @x                = args[:x]                || shape_defaults.x
  @y                = args[:y]                || shape_defaults.y
  @w                = args[:w]                || shape_defaults.w
  @h                = args[:h]                || shape_defaults.h
  @style_modifiers  = args[:style_modifiers]  || shape_defaults.style_modifiers || ''
end

#as_xml(xml) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



115
116
117
118
# File 'lib/drawio_dsl/schema/shape.rb', line 115

def as_xml(xml)
  draw_element(xml) if is_a?(Element) || is_a?(Text)
  draw_line(xml)    if is_a?(Line)
end

#bg_theme_paletteObject

TODO: test



157
158
159
# File 'lib/drawio_dsl/schema/shape.rb', line 157

def bg_theme_palette
  @bg_theme_palette ||= KConfig.configuration.drawio.theme.background(bg_theme)
end

#debug(format: :detail) ⇒ Object

:nocov:



162
163
164
165
166
167
168
# File 'lib/drawio_dsl/schema/shape.rb', line 162

def debug(format: :detail)
  if format == :detail
    debug_detail({ id: id, classification: classification, key: key })
  else
    debug_row(classification, id, key, x, y, w, h)
  end
end

#draw_element(xml) ⇒ Object



120
121
122
123
124
# File 'lib/drawio_dsl/schema/shape.rb', line 120

def draw_element(xml)
  xml.mxCell(id: id, value: value, style: style, vertex: 1, parent: parent&.id) do
    xml.mxGeometry(x: x, y: y, width: w, height: h, as: 'geometry')
  end
end

#draw_line(xml) ⇒ Object



126
127
128
129
130
# File 'lib/drawio_dsl/schema/shape.rb', line 126

def draw_line(xml)
  xml.mxCell(id: id, value: value, style: style, parent: parent&.id, source: source, target: target, edge: 1) do
    xml.mxGeometry(relative: 1, as: 'geometry')
  end
end

#format(key = nil) ⇒ Object



89
90
91
92
# File 'lib/drawio_dsl/schema/shape.rb', line 89

def format(key = nil)
  key ||= self.class.shape_key
  format_instance(key)
end

#shape_defaultsObject



65
66
67
# File 'lib/drawio_dsl/schema/shape.rb', line 65

def shape_defaults
  @shape_defaults ||= self.class.shape_defaults || default_configuration
end

#styleObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/drawio_dsl/schema/shape.rb', line 94

def style
  return @style if defined? @style

  add_base_modifiers

  @style_builder.add_kv('whiteSpace', white_space)      if white_space
  @style_builder.add_kv('html', html)                   if html
  @style_builder.add_kv('rounded', rounded)             if rounded
  @style_builder.add_kv('shadow', shadow)               if shadow
  @style_builder.add_kv('sketch', sketch)               if sketch
  @style_builder.add_kv('glass', glass)                 if glass
  @style_builder.add_kv('fillColor', fill_color)        if fill_color
  @style_builder.add_kv('strokeColor', stroke_color)    if stroke_color
  @style_builder.add_kv('fontColor', font_color)        if font_color
  @style_builder.add_kv('gradient', gradient)           if gradient
  @style_builder.add(style_modifiers)

  @style = @style_builder.style
end

#theme_paletteObject



152
153
154
# File 'lib/drawio_dsl/schema/shape.rb', line 152

def theme_palette
  @theme_palette ||= KConfig.configuration.drawio.theme.element(theme)
end

#to_hObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/drawio_dsl/schema/shape.rb', line 132

def to_h
  result = {
    id: id,
    parent_id: parent&.id,
    classification: classification,
    key: key,
    x: x,
    y: y,
    w: w,
    h: h,
    style: style,
    value: value,
    meta_data: @meta_data
  }
  result[:nodes] = nodes.to_h if nodes.any?
  result
end