Class: Pvectl::Formatters::Yaml

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

Overview

Formats data as YAML output.

Collections are rendered as YAML arrays. Single resources are rendered as YAML mappings. Empty collections return ā€œā€” []nā€. Nil values are rendered as YAML null (~).

Examples:

YAML output for collection

---
- name: vm-100
  status: running
- name: vm-101
  status: stopped

YAML output for single resource

---
name: vm-100
status: running
node: pve1

Instance Method Summary collapse

Instance Method Details

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

Formats data as YAML output.

Parameters:

  • data (Array, Object)

    collection of models or single model

  • presenter (Presenters::Base)

    presenter for hash conversion

  • color_enabled (Boolean) (defaults to: true)

    ignored for YAML (always plain text)

  • describe (Boolean) (defaults to: false)

    whether this is a describe command

  • context (Hash)

    ignored for YAML output

Returns:

  • (String)

    formatted YAML string



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pvectl/formatters/yaml.rb', line 36

def format(data, presenter, color_enabled: true, describe: false, **context)
  if describe && !collection?(data)
    # Use to_description for describe mode
    presenter.to_description(data).to_yaml
  elsif collection?(data)
    hashes = data.map { |model| presenter.to_hash(model) }
    hashes.to_yaml
  else
    presenter.to_hash(data).to_yaml
  end
end