Class: Archimate::Diff::Cli::DiffSummary

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

Constant Summary collapse

DIFF_KINDS =
%w(Delete Change Insert).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local, remote) ⇒ DiffSummary

Returns a new instance of DiffSummary.



25
26
27
28
29
# File 'lib/archimate/diff/cli/diff_summary.rb', line 25

def initialize(local, remote)
  @local = local
  @remote = remote
  @summary = Hash.new { |hash, key| hash[key] = Hash.new { |k_hash, k_key| k_hash[k_key] = 0 } }
end

Instance Attribute Details

#localObject (readonly)

Returns the value of attribute local.



13
14
15
# File 'lib/archimate/diff/cli/diff_summary.rb', line 13

def local
  @local
end

#remoteObject (readonly)

Returns the value of attribute remote.



13
14
15
# File 'lib/archimate/diff/cli/diff_summary.rb', line 13

def remote
  @remote
end

Class Method Details

.diff(local_file, remote_file, options = { verbose: true }) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/archimate/diff/cli/diff_summary.rb', line 15

def self.diff(local_file, remote_file, options = { verbose: true })
  logger.info "Reading #{local_file}"
  local = Archimate.read(local_file)
  logger.info "Reading #{remote_file}"
  remote = Archimate.read(remote_file)

  my_diff = DiffSummary.new(local, remote)
  my_diff.diff
end

Instance Method Details

#color(kind) ⇒ Object



97
98
99
# File 'lib/archimate/diff/cli/diff_summary.rb', line 97

def color(kind)
  Color.color(kind, kind)
end

#diffObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/archimate/diff/cli/diff_summary.rb', line 31

def diff
  logger.info "Calculating differences"
  diffs = Archimate.diff(local, remote)

  puts Color.color("Summary of differences", :headline)
  puts "\n"

  summary_element_diffs = diffs.group_by { |diff| diff.summary_element.class.to_s.split("::").last }
  summarize_elements summary_element_diffs["Element"]
  summarize "Organization", summary_element_diffs["Organization"]
  summarize "Relationship", summary_element_diffs["Relationship"]
  summarize_diagrams summary_element_diffs["Diagram"]

  puts "Total Diffs: #{diffs.size}"
end

#diffs_by_kind(diffs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/archimate/diff/cli/diff_summary.rb', line 57

def diffs_by_kind(diffs)
  diffs
    .group_by(&:summary_element)
    .each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |(summary_element, element_diffs), a|
    top_level_diff = element_diffs.find { |diff| summary_element == diff.target.value }
    if top_level_diff
      a[top_level_diff.kind] << summary_element
    else
      a["Change"] << summary_element
    end
  end
end

#summarize(title, diffs) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/archimate/diff/cli/diff_summary.rb', line 47

def summarize(title, diffs)
  return if diffs.nil? || diffs.empty?
  by_kind = diffs_by_kind(diffs)

  puts color(title)
  DIFF_KINDS.each do |kind|
    puts format("  #{color(kind)}: #{by_kind[kind]&.size}") if by_kind.key?(kind)
  end
end

#summarize_diagrams(diffs) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/archimate/diff/cli/diff_summary.rb', line 82

def summarize_diagrams(diffs)
  return if diffs.nil? || diffs.empty?
  puts Color.color("Diagrams", :headline)

  by_kind = diffs_by_kind(diffs)
  %w(Delete Change Insert).each do |kind|
    next unless by_kind.key?(kind)
    diagram_names = by_kind[kind].uniq.map(&:name)
    puts "  #{color(kind)}"
    # TODO: make this magic number an option
    diagram_names[0..14].each { |diagram_name| puts "    #{diagram_name}" }
    puts "    ... and #{diagram_names.size - 15} more" if diagram_names.size > 15
  end
end

#summarize_elements(diffs) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/archimate/diff/cli/diff_summary.rb', line 70

def summarize_elements(diffs)
  return if diffs.nil? || diffs.empty?
  puts Color.color("Elements", :headline)
  by_layer = diffs.group_by { |diff| diff.summary_element.layer }
  summarize "Business", by_layer["Business"]
  summarize "Application", by_layer["Application"]
  summarize "Technology", by_layer["Technology"]
  summarize "Motivation", by_layer["Motivation"]
  summarize "Implementation and Migration", by_layer["Implementation and Migration"]
  summarize "Connectors", by_layer["Connectors"]
end