Class: Pvectl::Presenters::Template

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

Overview

Presenter for template listing (mixed VM and Container templates).

Handles both Models::Vm and Models::Container via duck typing. Both models share: vmid, name, type, node, maxdisk, tags.

Examples:

Using with formatter

presenter = Template.new
formatter = Formatters::Table.new
output = formatter.format(templates, presenter)

See Also:

Instance Method Summary collapse

Methods inherited from Base

#extra_columns, #extra_values, #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 template listing.



22
23
24
# File 'lib/pvectl/presenters/template.rb', line 22

def columns
  %w[ID NAME TYPE NODE DISK TAGS]
end

#format_disk(bytes) ⇒ String

Formats disk size in human-readable format.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pvectl/presenters/template.rb', line 63

def format_disk(bytes)
  return "-" unless bytes && bytes.positive?

  if bytes >= 1_073_741_824
    "#{(bytes.to_f / 1_073_741_824).round(1)}G"
  elsif bytes >= 1_048_576
    "#{(bytes.to_f / 1_048_576).round(1)}M"
  else
    "#{bytes}B"
  end
end

#to_hash(model) ⇒ Hash

Converts a template model to hash for JSON/YAML.



46
47
48
49
50
51
52
53
54
55
# File 'lib/pvectl/presenters/template.rb', line 46

def to_hash(model)
  {
    "id" => model.vmid,
    "name" => model.name,
    "type" => model.type,
    "node" => model.node,
    "disk" => model.maxdisk,
    "tags" => model.tags
  }
end

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

Converts a template model to table row.



31
32
33
34
35
36
37
38
39
40
# File 'lib/pvectl/presenters/template.rb', line 31

def to_row(model, **_context)
  [
    model.vmid.to_s,
    model.name || "-",
    model.type || "-",
    model.node || "-",
    format_disk(model.maxdisk),
    model.tags || "-"
  ]
end