Class: Archimate::Cli::Mapper

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

Constant Summary collapse

HEADERS =
%w[id name viewpoint].freeze
COL_DIVIDER =
" | "

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, output_io) ⇒ Mapper

Returns a new instance of Mapper.



11
12
13
14
# File 'lib/archimate/cli/mapper.rb', line 11

def initialize(model, output_io)
  @model = model
  @output = output_io
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/archimate/cli/mapper.rb', line 9

def model
  @model
end

Instance Method Details

#build_organization_hash(organizations, parent = "", hash = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/archimate/cli/mapper.rb', line 58

def build_organization_hash(organizations, parent = "", hash = {})
  organizations.each_with_object(hash) do |i, a|
    organization_path = [parent, i.name].join("/")
    a[organization_path] = i
    build_organization_hash(i.organizations, organization_path, a)
  end
end

#compute_column_widths(diagrams, headers) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/archimate/cli/mapper.rb', line 40

def compute_column_widths(diagrams, headers)
  initial_widths = headers.map(&:size)
  diagrams.each_with_object(initial_widths) do |diagram, memo|
    diagram.slice(0, headers.size).each_with_index do |o, i|
      memo[i] = !o.nil? && Color.uncolor(o).size > memo[i] ? Color.uncolor(o).size : memo[i]
    end
    memo
  end
end

#header_row(widths, headers) ⇒ Object



16
17
18
19
20
21
# File 'lib/archimate/cli/mapper.rb', line 16

def header_row(widths, headers)
  titles = []
  widths.each_with_index { |w, i| titles << format("%-#{w}s", headers[i]) }
  @output.puts titles.map { |t| Color.color(t.capitalize, %i[bold blue]) }.join(Color.color(COL_DIVIDER, :light_black))
  @output.puts Color.color(widths.map { |w| "-" * w }.join("-+-"), :light_black)
end

#mapObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/archimate/cli/mapper.rb', line 66

def map
  widths = compute_column_widths(process_diagrams(model.diagrams), HEADERS)
  adjusted_widths = widths.inject(COL_DIVIDER.size * (HEADERS.size - 1), &:+)
  header_row(widths, HEADERS)
  organization_paths = build_organization_hash(model.organizations)
  organization_paths.keys.sort.each do |organization_name|
    diagrams = organization_paths[organization_name].items.map { |i| model.lookup(i) }.select { |i| i.is_a?(DataModel::Diagram) }
    next if diagrams.empty?
    @output.puts(Color.color(format("%-#{adjusted_widths}s", organization_name), %i[bold green on_light_black]))
    output_diagrams(process_diagrams(diagrams), widths)
  end

  @output.puts "\n#{model.diagrams.size} Diagrams"
end

#output_diagrams(diagrams, widths) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/archimate/cli/mapper.rb', line 50

def output_diagrams(diagrams, widths)
  diagrams.sort_by { |a| a[1] }.each do |m|
    row = []
    m.slice(0, widths.size).each_with_index { |c, i| row << format("%-#{widths[i]}s", c) }
    @output.puts row.join(Color.color(COL_DIVIDER, :light_black))
  end
end

#process_diagrams(diagrams) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/archimate/cli/mapper.rb', line 23

def process_diagrams(diagrams)
  diagrams.map { |e| [e.id, e.name, e.viewpoint, e.type] }.map do |row|
    row[2] = case row[3]
             when "canvas:CanvasModel"
               ["Canvas", row[4]].compact.join(": ")
             when "archimate:SketchModel"
               "Sketch"
             when "archimate:ArchimateDiagramModel"
               DataModel::ViewpointType.values[(row[2] || 0).to_i].to_s
             else
               row[3]
             end
    row[0] = Color.color("#{row[0]}.png", :underline)
    row
  end
end