Class: WavefrontDisplayPrinter::Terse

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

Overview

Print values 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 (Hash)

    data to display, from a response object

  • keys (Array[Symbol])

    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

#map_to_string(value) ⇒ Object



36
37
38
# File 'lib/wavefront-cli/display/printer/terse.rb', line 36

def map_to_string(value)
  format('%s=%s', value.keys[0], value.values.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_string(e[k]) } } }
end

#to_sObject



40
41
42
# File 'lib/wavefront-cli/display/printer/terse.rb', line 40

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

#to_string(value) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/wavefront-cli/display/printer/terse.rb', line 26

def to_string(value)
  if value.is_a?(Array)
    value.join(', ')
  elsif value.is_a?(Map)
    map_to_string(value)
  else
    value
  end
end