Class: Pvectl::Formatters::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/formatters/registry.rb

Overview

Registry for looking up formatters by name.

Implements the Registry Pattern to map format names (“table”, “json”, “yaml”, “wide”) to formatter classes.

Examples:

Getting a formatter

formatter = Registry.for("json")
output = formatter.format(data, presenter)

Listing available formats

Registry.available_formats #=> ["table", "wide", "json", "yaml"]

Checking if format is supported

Registry.supported?("json")  #=> true
Registry.supported?("xml")   #=> false

Constant Summary collapse

FORMATS =

Mapping of format names to formatter classes.

Returns:

  • (Hash<String, Class>)

    frozen hash of format name to class

{
  "table" => Table,
  "wide" => Wide,
  "json" => Json,
  "yaml" => Yaml
}.freeze

Class Method Summary collapse

Class Method Details

.available_formatsArray<String>

Returns list of available format names.

Examples:

Registry.available_formats #=> ["table", "wide", "json", "yaml"]

Returns:

  • (Array<String>)

    available format names



54
55
56
# File 'lib/pvectl/formatters/registry.rb', line 54

def available_formats
  FORMATS.keys
end

.for(format_name) ⇒ Base

Gets a formatter instance for the specified format.

Examples:

formatter = Registry.for("json")
formatter.format(data, presenter)

Parameters:

  • format_name (String, Symbol)

    format name (table, wide, json, yaml)

Returns:

  • (Base)

    formatter instance

Raises:

  • (ArgumentError)

    if format is not found



41
42
43
44
45
46
# File 'lib/pvectl/formatters/registry.rb', line 41

def for(format_name)
  formatter_class = FORMATS[format_name.to_s]
  raise ArgumentError, "Unknown format: #{format_name}" unless formatter_class

  formatter_class.new
end

.supported?(format_name) ⇒ Boolean

Checks if a format is supported.

Examples:

Registry.supported?("json")  #=> true
Registry.supported?("xml")   #=> false

Parameters:

  • format_name (String, Symbol)

    format name

Returns:

  • (Boolean)

    true if format is supported



66
67
68
# File 'lib/pvectl/formatters/registry.rb', line 66

def supported?(format_name)
  FORMATS.key?(format_name.to_s)
end