Class: Pvectl::Presenters::Backup

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/presenters/backup.rb,
sig/pvectl/presenters/backup.rbs

Overview

Presenter for Backup models.

Formats backups for table, wide, JSON, and YAML output. Standard columns: VMID, TYPE, CREATED, SIZE, STORAGE, PROTECTED Wide columns add: FORMAT, NOTES, VOLID

Examples:

Using with formatter

presenter = Backup.new
formatter = Formatters::Table.new
output = formatter.format(backups, presenter)

See Also:

Instance Method Summary collapse

Methods inherited from Base

#format_bytes, #format_firewall, #format_firewall_rules, #format_task_history, #resource, #tags_array, #tags_display, #template_display, #to_description, #to_wide_row, #uptime_human, #wide_columns

Instance Method Details

#columnsArray<String>

Returns column headers for standard table output.

Returns:

  • (Array<String>)

    column headers



23
24
25
# File 'lib/pvectl/presenters/backup.rb', line 23

def columns
  %w[VMID TYPE CREATED SIZE STORAGE PROTECTED]
end

#extra_columnsArray<String>

Returns additional column headers for wide output.

Returns:

  • (Array<String>)

    extra column headers



30
31
32
# File 'lib/pvectl/presenters/backup.rb', line 30

def extra_columns
  %w[FORMAT NOTES VOLID]
end

#extra_values(model, **_context) ⇒ Array

Returns additional values for wide output.

Parameters:

  • model (Models::Backup)

    Backup model

  • context (Hash)

    optional context

Returns:

  • (Array)

    extra values matching extra_columns order



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

def extra_values(model, **_context)
  [
    model.format,
    truncate(model.notes, 30),
    model.volid
  ]
end

#format_protected(protected_status) ⇒ String

Formats protected status for display.

Parameters:

  • protected (Boolean)

    protection status

  • protected_status (Boolean)

Returns:

  • (String)

    "yes" or "no"



111
112
113
# File 'lib/pvectl/presenters/backup.rb', line 111

def format_protected(protected_status)
  protected_status ? "yes" : "no"
end

#format_time(time) ⇒ String

Formats time for display.

Parameters:

  • time (Time, nil)

    time to format

Returns:

  • (String)

    formatted time or "-" if nil



101
102
103
104
105
# File 'lib/pvectl/presenters/backup.rb', line 101

def format_time(time)
  return "-" if time.nil?

  time.strftime("%Y-%m-%d %H:%M:%S")
end

#format_type(resource_type) ⇒ String

Formats resource type for display.

Parameters:

  • resource_type (Symbol, nil)

    :qemu or :lxc

Returns:

  • (String)

    formatted type



89
90
91
92
93
94
95
# File 'lib/pvectl/presenters/backup.rb', line 89

def format_type(resource_type)
  case resource_type
  when :qemu then "qemu"
  when :lxc then "lxc"
  else "-"
  end
end

#to_hash(model) ⇒ Hash

Converts Backup model to hash for JSON/YAML output.

Parameters:

Returns:

  • (Hash)

    hash representation with string keys



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pvectl/presenters/backup.rb', line 67

def to_hash(model)
  {
    "vmid" => model.vmid,
    "type" => format_type(model.resource_type),
    "volid" => model.volid,
    "storage" => model.storage,
    "node" => model.node,
    "size" => model.size,
    "size_human" => model.human_size,
    "created_at" => model.created_at&.iso8601,
    "format" => model.format,
    "notes" => model.notes,
    "protected" => model.protected?
  }
end

#to_row(model, **_context) ⇒ Array

Converts Backup model to table row values.

Parameters:

  • model (Models::Backup)

    Backup model

  • context (Hash)

    optional context

Returns:

  • (Array)

    row values matching columns order



39
40
41
42
43
44
45
46
47
48
# File 'lib/pvectl/presenters/backup.rb', line 39

def to_row(model, **_context)
  [
    model.vmid,
    format_type(model.resource_type),
    format_time(model.created_at),
    model.human_size,
    model.storage,
    format_protected(model.protected?)
  ]
end

#truncate(text, max_length) ⇒ String?

Truncates text to specified length.

Parameters:

  • text (String, nil)

    text to truncate

  • max_length (Integer)

    maximum length

Returns:

  • (String, nil)

    truncated text or nil



120
121
122
123
124
125
# File 'lib/pvectl/presenters/backup.rb', line 120

def truncate(text, max_length)
  return nil if text.nil?
  return text if text.length <= max_length

  "#{text[0, max_length - 3]}..."
end