Class: Pvectl::Presenters::Base Abstract

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

Overview

This class is abstract.

Subclass and implement #columns, #to_row, and #to_hash.

Abstract base class for resource presenters.

Presenters define how models are displayed in different formats. Each resource type (VM, Container, Node, etc.) has its own presenter.

Examples:

Implementing a resource presenter

class VmPresenter < Base
  def columns
    ["NAME", "STATUS", "NODE"]
  end

  def extra_columns
    ["MEMORY", "CPU"]
  end

  def to_row(model, **context)
    [model.name, model.status, model.node]
  end

  def extra_values(model, **context)
    [model.memory, model.cpu]
  end

  def to_hash(model)
    { "name" => model.name, "status" => model.status, "node" => model.node }
  end
end

See Also:

Instance Method Summary collapse

Instance Method Details

#columnsArray<String>

Returns column headers for table format.

Returns:

  • (Array<String>)

    column names (uppercase, e.g., [“NAME”, “STATUS”])

Raises:

  • (NotImplementedError)

    if not implemented by subclass



42
43
44
# File 'lib/pvectl/presenters/base.rb', line 42

def columns
  raise NotImplementedError, "#{self.class}#columns must be implemented"
end

#extra_columnsArray<String>

Returns additional columns for wide format. Override in subclass to add extra columns.

Returns:

  • (Array<String>)

    extra column names (empty by default)



58
59
60
# File 'lib/pvectl/presenters/base.rb', line 58

def extra_columns
  []
end

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

Returns additional values for wide format. Override in subclass to add extra values.

Parameters:

  • model (Object)

    domain model object

  • context (Hash)

    optional context

Returns:

  • (Array<String, nil>)

    extra values (empty by default)



88
89
90
# File 'lib/pvectl/presenters/base.rb', line 88

def extra_values(model, **context)
  []
end

#tags_arrayArray<String>

Returns tags as array.

Returns:

  • (Array<String>)

    array of tags, or empty array if no tags



134
135
136
137
138
139
# File 'lib/pvectl/presenters/base.rb', line 134

def tags_array
  tags = resource.tags
  return [] if tags.nil? || tags.empty?

  tags.split(";").map(&:strip)
end

#tags_displayString

Returns tags as comma-separated string.

Returns:

  • (String)

    formatted tags (e.g., “prod, web”) or “-” if no tags



144
145
146
147
# File 'lib/pvectl/presenters/base.rb', line 144

def tags_display
  arr = tags_array
  arr.empty? ? "-" : arr.join(", ")
end

#template_displayString

Returns template display string.

Returns:

  • (String)

    “yes” if template, “-” otherwise



152
153
154
# File 'lib/pvectl/presenters/base.rb', line 152

def template_display
  resource.template? ? "yes" : "-"
end

#to_description(model) ⇒ Hash

Converts model to description format (kubectl-style vertical layout). By default, delegates to to_hash. Override for custom describe output.

Parameters:

  • model (Object)

    domain model object

Returns:

  • (Hash)

    hash representation (keys become labels)



106
107
108
# File 'lib/pvectl/presenters/base.rb', line 106

def to_description(model)
  to_hash(model)
end

#to_hash(model) ⇒ Hash

Converts model to hash for JSON/YAML format.

Parameters:

  • model (Object)

    domain model object

Returns:

  • (Hash)

    hash representation with string keys

Raises:

  • (NotImplementedError)

    if not implemented by subclass



97
98
99
# File 'lib/pvectl/presenters/base.rb', line 97

def to_hash(model)
  raise NotImplementedError, "#{self.class}#to_hash must be implemented"
end

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

Converts model to table row values.

Parameters:

  • model (Object)

    domain model object

  • context (Hash)

    optional context (e.g., current_context: “prod”)

Returns:

  • (Array<String, nil>)

    row values matching columns order

Raises:

  • (NotImplementedError)

    if not implemented by subclass



68
69
70
# File 'lib/pvectl/presenters/base.rb', line 68

def to_row(model, **context)
  raise NotImplementedError, "#{self.class}#to_row must be implemented"
end

#to_wide_row(model, **context) ⇒ Array<String, nil>

Converts model to wide table row values. By default, appends extra_values to to_row.

Parameters:

  • model (Object)

    domain model object

  • context (Hash)

    optional context

Returns:

  • (Array<String, nil>)

    row values (normal + extra)



78
79
80
# File 'lib/pvectl/presenters/base.rb', line 78

def to_wide_row(model, **context)
  to_row(model, **context) + extra_values(model, **context)
end

#uptime_humanString

Returns uptime in human-readable format. Delegates to resource.uptime. Override in subclasses with custom logic.

Returns:

  • (String)

    formatted uptime (e.g., “15d 3h”) or “-”



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pvectl/presenters/base.rb', line 114

def uptime_human
  uptime = resource.uptime
  return "-" if uptime.nil? || uptime.zero?

  days = uptime / 86_400
  hours = (uptime % 86_400) / 3600
  minutes = (uptime % 3600) / 60

  if days.positive?
    "#{days}d #{hours}h"
  elsif hours.positive?
    "#{hours}h #{minutes}m"
  else
    "#{minutes}m"
  end
end

#wide_columnsArray<String>

Returns extended column headers for wide format. By default, appends extra_columns to columns.

Returns:

  • (Array<String>)

    column names (normal + extra)



50
51
52
# File 'lib/pvectl/presenters/base.rb', line 50

def wide_columns
  columns + extra_columns
end