Class: DrawioDsl::DomBuilder

Inherits:
Object
  • Object
show all
Includes:
DomBuilderShapes
Defined in:
lib/drawio_dsl/dom_builder.rb

Overview

Build Document object model to represent DrawioDsl output file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DomBuilderShapes

#add_actor, #add_actor2, #add_callout, #add_callout2, #add_callout3, #add_callout4, #add_circle, #add_cloud, #add_container, #add_container2, #add_container3, #add_container4, #add_cross, #add_dash, #add_dash24, #add_dash32, #add_dash44, #add_dash_dot, #add_dash_dot_dot, #add_dash_long_dash, #add_database, #add_db_json, #add_diamond, #add_document, #add_dot, #add_dot_dot_dot, #add_double, #add_double_dash, #add_double_dot, #add_ellipse, #add_embed_col200, #add_embed_col50, #add_embed_row, #add_envelop, #add_face, #add_group, #add_h1, #add_h2, #add_h3, #add_h4, #add_h5, #add_h6, #add_hexagon, #add_interface, #add_klass, #add_long_dash, #add_note, #add_p, #add_process, #add_rectangle, #add_rectangle2, #add_solid, #add_square, #add_step, #add_tick, #add_todo, #add_triangle

Constructor Details

#initializeDomBuilder

attr_reader :current_layout_rule attr_reader :current_shape



17
18
19
20
# File 'lib/drawio_dsl/dom_builder.rb', line 17

def initialize
  @actions = []
  @last_action = {}
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



8
9
10
# File 'lib/drawio_dsl/dom_builder.rb', line 8

def actions
  @actions
end

#current_pageObject (readonly)

Returns the value of attribute current_page.



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

def current_page
  @current_page
end

#focus_nodeObject (readonly)

Returns the value of attribute focus_node.



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

def focus_node
  @focus_node
end

#last_actionObject (readonly)

Returns the value of attribute last_action.



9
10
11
# File 'lib/drawio_dsl/dom_builder.rb', line 9

def last_action
  @last_action
end

Instance Method Details

#add_flex_layout(**opts) ⇒ Object



73
74
75
76
# File 'lib/drawio_dsl/dom_builder.rb', line 73

def add_flex_layout(**opts)
  rule = DrawioDsl::Schema::FlexLayout.new(current_page, **opts)
  add_layout(rule)
end

#add_grid_layout(**opts) ⇒ Object


Layout provides rules for positioning components




68
69
70
71
# File 'lib/drawio_dsl/dom_builder.rb', line 68

def add_grid_layout(**opts)
  rule = DrawioDsl::Schema::GridLayout.new(current_page, **opts)
  add_layout(rule)
end

#add_layout(rule) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/drawio_dsl/dom_builder.rb', line 78

def add_layout(rule)
  # @current_layout_rule = rule

  rule.id = "rule-#{current_page.nodes.length + 1}" unless rule.id

  # current_page.nodes.add(rule)
  focus_node.nodes.add(focus_node, rule)

  rule
end

#add_page(**opts) ⇒ Object

rubocop:disable Metrics/AbcSize



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/drawio_dsl/dom_builder.rb', line 45

def add_page(**opts)
  @current_page = DrawioDsl::Schema::Page.new(diagram, **opts)
  current_page.id = SecureRandom.alphanumeric(3) unless current_page.id

  # add anchor nodes
  page_anchor = DrawioDsl::Schema::Anchor.new(self, id: "page_root_#{current_page.id}", key: :page_root)
  node_anchor = DrawioDsl::Schema::Anchor.new(self, id: "node_root_#{current_page.id}", key: :node_root)
  page_anchor.add_node(node_anchor)

  @focus_node = node_anchor

  # page_anchor.nodes.add(node_anchor)
  current_page.add_node(page_anchor)

  diagram.pages << @current_page
  @current_page
end

#add_shape(shape) ⇒ Object


Shapes represent visual components




93
94
95
96
97
98
99
100
101
102
# File 'lib/drawio_dsl/dom_builder.rb', line 93

def add_shape(shape)
  # @current_shape = shape

  shape.id = "#{current_page.id}-#{focus_node.nodes.length + 1}" unless shape.id

  # current_page.nodes.add(shape)
  focus_node.nodes.add(focus_node, shape)

  shape
end

#debugObject

:nocov:



105
106
107
108
# File 'lib/drawio_dsl/dom_builder.rb', line 105

def debug
  puts JSON.pretty_generate(actions)
  puts JSON.pretty_generate(diagram.to_h)
end

#diagramObject



38
39
40
41
42
# File 'lib/drawio_dsl/dom_builder.rb', line 38

def diagram
  return @diagram if defined? @diagram

  set_diagram
end

#domObject

:nocov:



111
112
113
# File 'lib/drawio_dsl/dom_builder.rb', line 111

def dom
  diagram.to_h
end

#queue_action(action) ⇒ Object



29
30
31
32
# File 'lib/drawio_dsl/dom_builder.rb', line 29

def queue_action(action)
  @actions << action
  @last_action = action
end

#resetObject



22
23
24
25
26
27
# File 'lib/drawio_dsl/dom_builder.rb', line 22

def reset
  @actions = []
  @last_action = {}
  set_diagram
  # set_layout_engine
end

#set_diagram(**opts) ⇒ Object



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

def set_diagram(**opts)
  @diagram = DrawioDsl::Schema::Diagram.new(**opts)
end