Class: WavefrontDisplayPrinter::Terse

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-cli/display/printer/terse.rb

Overview

Print things which are per-row. The terse listings, primarily

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, keys) ⇒ Terse

Returns a new instance of Terse.

Parameters:

  • data to display, from a response object

  • keys to display, in order



13
14
15
16
# File 'lib/wavefront-cli/display/printer/terse.rb', line 13

def initialize(data, keys)
  @data = stringify(data, keys)
  @fmt  = format_string(data, keys)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/wavefront-cli/display/printer/terse.rb', line 8

def data
  @data
end

#fmtObject (readonly)

Returns the value of attribute fmt.



8
9
10
# File 'lib/wavefront-cli/display/printer/terse.rb', line 8

def fmt
  @fmt
end

Instance Method Details

#format_string(data, keys) ⇒ Object



18
19
20
# File 'lib/wavefront-cli/display/printer/terse.rb', line 18

def format_string(data, keys)
  keys.map { |k| "%-#{data.longest_value_of(k)}<#{k}>s" }.join('  ')
end

#stringify(data, keys) ⇒ Object



22
23
24
# File 'lib/wavefront-cli/display/printer/terse.rb', line 22

def stringify(data, keys)
  data.map { |e| e.tap { keys.each { |k| e[k] = to_list(e[k]) } } }
end

#to_list(thing) ⇒ Object



26
27
28
# File 'lib/wavefront-cli/display/printer/terse.rb', line 26

def to_list(thing)
  thing.is_a?(Array) ? thing.join(', ') : thing
end

#to_sObject



30
31
32
# File 'lib/wavefront-cli/display/printer/terse.rb', line 30

def to_s
  data.map { |e| format(fmt, e).rstrip }.join("\n")
end