Class: HaveAPI::CLI::OutputFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/cli/output_formatter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, cols = nil, header: true, sort: nil, layout: nil, empty: '-') ⇒ OutputFormatter

Returns a new instance of OutputFormatter.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/haveapi/cli/output_formatter.rb', line 13

def initialize(objects, cols = nil, header: true, sort: nil, layout: nil, empty: '-')
  @objects = objects
  @header = header
  @sort = sort
  @layout = layout
  @empty = empty

  if @layout.nil?
    @layout = if many?
                :columns

              else
                :rows
              end
  end

  if cols
    @cols = parse_cols(cols)

  elsif @objects.is_a?(::Array)
    @cols ||= parse_cols(@objects.first.keys) # A list of items

  elsif @objects.is_a?(::Hash) # Single item
    @cols ||= parse_cols(@objects.keys)

  else
    raise "unsupported type #{@objects.class}"
  end
end

Class Method Details



8
9
10
11
# File 'lib/haveapi/cli/output_formatter.rb', line 8

def self.print(*args, **kwargs)
  f = new(*args, **kwargs)
  f.print
end

.to_s(*args) ⇒ Object



3
4
5
6
# File 'lib/haveapi/cli/output_formatter.rb', line 3

def self.to_s(*args)
  f = new(*args)
  f.to_s
end

Instance Method Details



49
50
51
52
# File 'lib/haveapi/cli/output_formatter.rb', line 49

def print
  @out = nil
  generate
end

#to_sObject



43
44
45
46
47
# File 'lib/haveapi/cli/output_formatter.rb', line 43

def to_s
  @out = ''
  generate
  @out
end