Class: Pvectl::Presenters::Snapshot

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/presenters/snapshot.rb

Overview

Presenter for VM/container snapshots.

Defines column layout and formatting for snapshot table output. Supports both list (get) and describe output modes.

Standard columns: VMID, NAME, CREATED, DESCRIPTION Wide columns add: TYPE, VMSTATE, PARENT

Examples:

Using with formatter

presenter = Snapshot.new
formatter = Formatters::Table.new
output = formatter.format(snapshots, presenter)

See Also:

Instance Method Summary collapse

Methods inherited from Base

#tags_array, #tags_display, #template_display, #to_wide_row, #uptime_human, #wide_columns

Instance Method Details

#columnsArray<String>

Returns column headers for standard table output.

Returns:

  • (Array<String>)

    column headers



26
27
28
# File 'lib/pvectl/presenters/snapshot.rb', line 26

def columns
  %w[VMID NAME CREATED DESCRIPTION]
end

#extra_columnsArray<String>

Returns additional column headers for wide output.

Returns:

  • (Array<String>)

    extra column headers



33
34
35
# File 'lib/pvectl/presenters/snapshot.rb', line 33

def extra_columns
  %w[TYPE VMSTATE PARENT]
end

#extra_values(model, **_context) ⇒ Array<String>

Returns additional values for wide output.

Parameters:

  • model (Models::Snapshot)

    Snapshot model

  • context (Hash)

    optional context

Returns:

  • (Array<String>)

    extra values matching extra_columns order



56
57
58
59
60
61
62
# File 'lib/pvectl/presenters/snapshot.rb', line 56

def extra_values(model, **_context)
  [
    model.resource_type&.to_s || "-",
    model.has_vmstate? ? "yes" : "no",
    model.parent || "-"
  ]
end

#to_description(model) ⇒ Hash

Returns describe output for SnapshotDescription or plain Snapshot.

Parameters:

Returns:

  • (Hash)

    description hash for formatter



92
93
94
95
96
97
98
99
100
# File 'lib/pvectl/presenters/snapshot.rb', line 92

def to_description(model)
  return snapshot_to_hash(model) unless model.is_a?(Models::SnapshotDescription)

  if model.single?
    build_single_description(model.entries.first)
  else
    build_multi_description(model.entries)
  end
end

#to_hash(model) ⇒ Hash+

Converts model to hash for JSON/YAML output.

Handles both Models::Snapshot (for list) and Models::SnapshotDescription (for describe).

Parameters:

Returns:

  • (Hash, Array<Hash>)

    hash representation with string keys



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pvectl/presenters/snapshot.rb', line 71

def to_hash(model)
  return snapshot_to_hash(model) unless model.is_a?(Models::SnapshotDescription)

  if model.single?
    entry = model.entries.first
    snapshot_to_hash(entry.snapshot).merge(
      "snapshot_tree" => build_tree_data(entry)
    )
  else
    model.entries.map do |entry|
      snapshot_to_hash(entry.snapshot).merge(
        "snapshot_tree" => build_tree_data(entry)
      )
    end
  end
end

#to_row(model, **_context) ⇒ Array<String>

Converts Snapshot model to table row values.

Parameters:

  • model (Models::Snapshot)

    Snapshot model

  • context (Hash)

    optional context

Returns:

  • (Array<String>)

    row values matching columns order



42
43
44
45
46
47
48
49
# File 'lib/pvectl/presenters/snapshot.rb', line 42

def to_row(model, **_context)
  [
    model.vmid.to_s,
    model.name,
    format_time(model.created_at),
    model.description || "-"
  ]
end