Class: Pvectl::Formatters::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/formatters/base.rb,
sig/pvectl/formatters/base.rbs

Overview

This class is abstract.

Subclass and implement #format to create a formatter.

Abstract base class for output formatters.

Formatters implement the Strategy Pattern, converting model data to various output formats (table, json, yaml, wide).

Examples:

Implementing a custom formatter

class MyFormatter < Base
  def format(data, presenter, color_enabled: true, **context)
    # Return formatted string
  end
end

See Also:

Direct Known Subclasses

Json, Table, Wide, Yaml

Instance Method Summary collapse

Instance Method Details

#collection?(data) ⇒ Boolean

Determines if data is a collection or single resource.

Parameters:

  • data (Array, Object)

    data to check

Returns:

  • (Boolean)

    true if data is a collection (Array)



41
42
43
# File 'lib/pvectl/formatters/base.rb', line 41

def collection?(data)
  data.is_a?(Array)
end

#format(data, presenter, color_enabled: true, **context) ⇒ String

Formats data for output.

Parameters:

  • data (Array, Object)

    collection of models or single model

  • presenter (Presenters::Base)

    presenter for column/row definitions

  • color_enabled (Boolean) (defaults to: true)

    whether to apply color formatting

  • context (Hash)

    additional context (e.g., current_context for contexts)

  • color_enabled: (Boolean) (defaults to: true)

Returns:

  • (String)

    formatted output string

Raises:

  • (NotImplementedError)

    if not implemented by subclass



31
32
33
# File 'lib/pvectl/formatters/base.rb', line 31

def format(data, presenter, color_enabled: true, **context)
  raise NotImplementedError, "#{self.class}#format must be implemented"
end

#normalize_nil(value, nil_placeholder = "-") ⇒ Object, String

Normalizes nil values for display.

Parameters:

  • value (Object, nil)

    value to normalize

  • nil_placeholder (String) (defaults to: "-")

    placeholder for nil values (default: "-")

Returns:

  • (Object, String)

    original value or placeholder



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

def normalize_nil(value, nil_placeholder = "-")
  value.nil? ? nil_placeholder : value
end