Class: Archimate::Cli::Svger

Inherits:
Object
  • Object
show all
Defined in:
lib/archimate/cli/svger.rb

Overview

This class is used to export SVG diagrams as defined in the given model

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(diagrams, output_dir, options = {}) ⇒ Svger

Returns a new instance of Svger.



20
21
22
23
24
25
# File 'lib/archimate/cli/svger.rb', line 20

def initialize(diagrams, output_dir, options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @diagrams = diagrams
  @output_dir = output_dir
  @diagram_base_names = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/archimate/cli/svger.rb', line 14

def options
  @options
end

Class Method Details

.export_svgs(archi_file, output_dir, options = {}) ⇒ Object



16
17
18
# File 'lib/archimate/cli/svger.rb', line 16

def self.export_svgs(archi_file, output_dir, options = {})
  new(Archimate.read(archi_file).diagrams, output_dir, options).export_svgs
end

Instance Method Details

#diagram_base_name(diagram, name_option) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/archimate/cli/svger.rb', line 56

def diagram_base_name(diagram, name_option)
  case name_option
  when :name
    diagram.name
  # when :folder_name
  #   diagram.name # TODO: add the path
  else
    diagram.id
  end
end

#export(diagram, file_name = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/archimate/cli/svger.rb', line 38

def export(diagram, file_name = nil)
  file_name = Cli.process_svg_filename(file_name || diagram.id)
  File.open(File.join(@output_dir, file_name), "wb") do |svg_file|
    svg_file.write(Svg::Diagram.new(diagram, options).to_svg)
  end
end

#export_svgsObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/archimate/cli/svger.rb', line 27

def export_svgs
  @diagram_base_names = {}
  progress = ProgressIndicator.new(total: @diagrams.size, title: "Writing SVGs")
  @diagrams.each do |diagram|
    export(diagram, filename_for(diagram, options[:svg_name]))
    progress.increment
  end
ensure
  progress.finish
end

#filename_for(diagram, name_option) ⇒ Object

TODO: in case of duplicate name in a model, we need to extend the filename with a numeric or something.



46
47
48
49
50
51
52
53
54
# File 'lib/archimate/cli/svger.rb', line 46

def filename_for(diagram, name_option)
  base_name = diagram_base_name(diagram, name_option)
  if @diagram_base_names.include?(base_name)
    idx = 2
    idx += 1 while @diagram_base_names.include?(base_name + " - #{idx}")
    base_name += " - #{idx}"
  end
  base_name
end