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
42
43
# 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?
    if many?
      @layout = :columns

    else
      @layout = :rows
    end
  end

  if cols
    @cols = parse_cols(cols)

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

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

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

Class Method Details

.format(*args) ⇒ Object



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

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


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

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

Instance Method Details

#formatObject



45
46
47
48
49
# File 'lib/haveapi/cli/output_formatter.rb', line 45

def format
  @out = ''
  generate
  @out
end


51
52
53
54
# File 'lib/haveapi/cli/output_formatter.rb', line 51

def print
  @out = nil
  generate
end