Module: Omnibus::Reports

Extended by:
Reports
Included in:
Reports
Defined in:
lib/omnibus/reports.rb

Constant Summary collapse

PADDING =
3

Instance Method Summary collapse

Instance Method Details

#column_width(items, column_name) ⇒ Object

Determine how wide a column should be, taking into account both the column name as well as all data in that column. If no data will be stored in the column, the width is 0 (i.e., nothing should be printed, not even the column header)



27
28
29
30
31
32
33
34
35
# File 'lib/omnibus/reports.rb', line 27

def column_width(items, column_name)
  widest_item = items.max_by(&:size)
  if widest_item
    widest = (widest_item.size >= column_name.size) ? widest_item : column_name
    widest.size + PADDING
  else
    0
  end
end

#non_nil_values(hashes, selector_key) ⇒ Object



37
38
39
# File 'lib/omnibus/reports.rb', line 37

def non_nil_values(hashes, selector_key)
  hashes.map { |v| v[selector_key] }.compact
end

#pretty_version_map(project) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/omnibus/reports.rb', line 41

def pretty_version_map(project)
  out = ""
  version_map = project.library.version_map

  # Pull out data to print out
  versions = non_nil_values(version_map.values, :version)
  guids = non_nil_values(version_map.values, :version_guid)

  # We only want the versions that have truly been overridden;
  # because we want to output a column only if something was
  # overridden, but nothing if no packages were changed
  overridden_versions = non_nil_values(version_map.values.select { |v| v[:overridden] }, :default_version)

  # Determine how wide the printed table columns need to be
  name_width = column_width(version_map.keys, "Component")
  version_width = column_width(versions, "Installed Version")
  guid_width = column_width(guids, "Version GUID")
  override_width = column_width(overridden_versions, "Overridden From")

  total_width = name_width + version_width + guid_width + override_width
  divider = "-" * total_width

  # Print out the column headers
  out << "Component".ljust(name_width)
  out << "Installed Version".ljust(version_width)
  out << "Version GUID".ljust(guid_width)
  # Only print out column if something was overridden
  out << "Overridden From".ljust(override_width) if override_width > 0
  out << "\n"
  out << divider << "\n"

  # Print out the table body
  version_map.keys.sort.each do |name|
    version = version_map[name][:version]
    version_guid = version_map[name][:version_guid]

    default_version = version_map[name][:default_version]
    overridden = version_map[name][:overridden]

    out << "#{name}".ljust(name_width)
    out << version.to_s.ljust(version_width)
    out << version_guid.to_s.ljust(guid_width) if version_guid
    # Only print out column if something was overridden
    out << default_version.ljust(override_width) if overridden
    out << "\n"
  end
  out
end