Class: CliFormat::Presenter::Json

Inherits:
Base
  • Object
show all
Defined in:
lib/cli_format/presenter/json.rb

Instance Attribute Summary

Attributes inherited from Base

#empty_message, #header, #rows

Instance Method Summary collapse

Methods inherited from Base

#initialize, #show

Constructor Details

This class inherits a constructor from CliFormat::Presenter::Base

Instance Method Details

#textObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cli_format/presenter/json.rb', line 5

def text
  json_data = if @header
    @rows.map do |row|
      item = {}
      @header.each_with_index do |header, i|
        item[header] = row[i]
      end
      item
    end
  elsif @rows.first.size == 2
    @rows.each_with_object({}) { |(k, v), h|
      h[k] = v
    }
  else # 1 row
    @rows
  end

  JSON.pretty_generate(json_data)
end