Class: Ace::Handbook::Organisms::StatusCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/handbook/organisms/status_collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_root: Ace::Handbook.project_root, registry: nil, inventory: nil, config: nil) ⇒ StatusCollector

Returns a new instance of StatusCollector.



9
10
11
12
13
14
# File 'lib/ace/handbook/organisms/status_collector.rb', line 9

def initialize(project_root: Ace::Handbook.project_root, registry: nil, inventory: nil, config: nil)
  @project_root = project_root
  @registry = registry || Atoms::ProviderRegistry.new(project_root: project_root)
  @inventory = inventory || SkillInventory.new(project_root: project_root)
  @config = config || Ace::Handbook.config.resolve_namespace("handbook").to_h
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/ace/handbook/organisms/status_collector.rb', line 7

def config
  @config
end

#inventoryObject (readonly)

Returns the value of attribute inventory.



7
8
9
# File 'lib/ace/handbook/organisms/status_collector.rb', line 7

def inventory
  @inventory
end

#project_rootObject (readonly)

Returns the value of attribute project_root.



7
8
9
# File 'lib/ace/handbook/organisms/status_collector.rb', line 7

def project_root
  @project_root
end

#registryObject (readonly)

Returns the value of attribute registry.



7
8
9
# File 'lib/ace/handbook/organisms/status_collector.rb', line 7

def registry
  @registry
end

Instance Method Details

#collect(provider: nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ace/handbook/organisms/status_collector.rb', line 16

def collect(provider: nil)
  skills = inventory.all

  {
    "canonical" => canonical_summary(skills),
    "providers" => selected_providers(provider).map { |provider_id| provider_status(provider_id, skills) }
  }
end

#to_table(snapshot) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/ace/handbook/organisms/status_collector.rb', line 25

def to_table(snapshot)
  summary_lines = canonical_summary_lines(snapshot.fetch("canonical"))
  rows = snapshot.fetch("providers").map do |entry|
    [
      entry.fetch("provider"),
      entry.fetch("enabled") ? "yes" : "no",
      entry.fetch("path_type"),
      entry.fetch("expected").to_s,
      entry.fetch("installed").to_s,
      entry.fetch("in_sync").to_s,
      entry.fetch("outdated").to_s,
      entry.fetch("missing").to_s,
      entry.fetch("extra").to_s,
      entry.fetch("relative_output_dir")
    ]
  end

  widths = [
    ["PROVIDER", *rows.map { |row| row[0] }].map(&:length).max,
    ["ENABLED", *rows.map { |row| row[1] }].map(&:length).max,
    ["TYPE", *rows.map { |row| row[2] }].map(&:length).max,
    ["EXPECTED", *rows.map { |row| row[3] }].map(&:length).max,
    ["INSTALLED", *rows.map { |row| row[4] }].map(&:length).max,
    ["IN_SYNC", *rows.map { |row| row[5] }].map(&:length).max,
    ["OUTDATED", *rows.map { |row| row[6] }].map(&:length).max,
    ["MISSING", *rows.map { |row| row[7] }].map(&:length).max,
    ["EXTRA", *rows.map { |row| row[8] }].map(&:length).max
  ]

  header = format(
    "%-#{widths[0]}s  %-#{widths[1]}s  %-#{widths[2]}s  %#{widths[3]}s  %#{widths[4]}s  %#{widths[5]}s  %#{widths[6]}s  %#{widths[7]}s  %#{widths[8]}s  %s",
    "PROVIDER", "ENABLED", "TYPE", "EXPECTED", "INSTALLED", "IN_SYNC", "OUTDATED", "MISSING", "EXTRA", "PATH"
  )
  lines = rows.map do |provider, enabled, type, expected, installed, in_sync, outdated, missing, extra, path|
    format(
      "%-#{widths[0]}s  %-#{widths[1]}s  %-#{widths[2]}s  %#{widths[3]}s  %#{widths[4]}s  %#{widths[5]}s  %#{widths[6]}s  %#{widths[7]}s  %#{widths[8]}s  %s",
      provider, enabled, type, expected, installed, in_sync, outdated, missing, extra, path
    )
  end

  (summary_lines + ["", header] + lines).join("\n")
end