Class: DrawioDsl::Drawio

Inherits:
KDirector::Directors::BaseDirector
  • Object
show all
Defined in:
lib/drawio_dsl/drawio.rb

Overview

DrawioDsl is a DSL for draw-io diagrams.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_save_file_nameObject (readonly)

Returns the value of attribute last_save_file_name.



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

def last_save_file_name
  @last_save_file_name
end

Instance Method Details

#diagram(**opts) ⇒ Object



10
11
12
13
14
# File 'lib/drawio_dsl/drawio.rb', line 10

def diagram(**opts)
  builder.set_diagram(**opts)

  self
end

#export_png(output_file_name, page: 1) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/drawio_dsl/drawio.rb', line 71

def export_png(output_file_name, page: 1)
  return unless last_save_file_name
  return unless File.exist?(last_save_file_name)

  page -= 1 # use zero based index

  output_file_name = "#{output_file_name}.png" unless output_file_name.end_with?('.png')

  command = "/Applications/draw.io.app/Contents/MacOS/draw.io '#{last_save_file_name}' -p #{page} -x -o #{output_file_name}"

  run_command(command)

  self
end

#export_svg(output_file_name, page: 1) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/drawio_dsl/drawio.rb', line 56

def export_svg(output_file_name, page: 1)
  return unless last_save_file_name
  return unless File.exist?(last_save_file_name)

  page -= 1 # use zero based index

  output_file_name = "#{output_file_name}.svg" unless output_file_name.end_with?('.svg')

  command = "/Applications/draw.io.app/Contents/MacOS/draw.io '#{last_save_file_name}' -p #{page} -x -o #{output_file_name}"

  run_command(command)

  self
end

#osave(file_name, **opts) ⇒ Object



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

def osave(file_name, **opts)
  save(file_name, **{ open: :write }.merge(opts))
end

#osave_json(file_name, **opts) ⇒ Object



52
53
54
# File 'lib/drawio_dsl/drawio.rb', line 52

def osave_json(file_name, **opts)
  save_json(file_name, **{ open: :write }.merge(opts))
end

#page(name = nil, **opts, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/drawio_dsl/drawio.rb', line 16

def page(name = nil, **opts, &block)
  page = DrawioDsl::DrawioPage.new(self, **opts.merge(name: name))
  page.instance_eval(&block) if block_given?

  layout = DrawioDsl::LayoutEngine.new(builder.current_page)
  layout.call

  self
end

#save(file_name, **opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/drawio_dsl/drawio.rb', line 26

def save(file_name, **opts)
  diagram = DrawioDsl::XmlBuilder.new(builder.diagram)

  add(file_name, content: diagram.build, **opts)

  # if last_save_file_name is nil then you are not in execute mode
  @last_save_file_name = k_builder.last_output_file

  self
end

#save_json(file_name, **opts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/drawio_dsl/drawio.rb', line 41

def save_json(file_name, **opts)
  return unless last_save_file_name
  return unless File.exist?(last_save_file_name)

  file_name = "#{file_name}.json" unless file_name.end_with?('.json')

  add(file_name, content: JSON.pretty_generate(builder.dom), **opts)

  self
end