Class: GithubCLI::Formatters::CSV

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/formatters/csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ CSV

Returns a new instance of CSV


9
10
11
# File 'lib/github_cli/formatters/csv.rb', line 9

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response


7
8
9
# File 'lib/github_cli/formatters/csv.rb', line 7

def response
  @response
end

Instance Method Details

#formatObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/github_cli/formatters/csv.rb', line 13

def format
  if response.respond_to?(:to_ary)
    render_headers(response.first)
    response.each_with_index do |item, indx|
      render_line(indx, item)
      Terminal.newline
    end
  elsif response.respond_to?(:keys)
    render_headers(response)
    render_line(1, response)
  else
    Terminal.line "#{response}\n"
  end
end

#render_headers(header) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/github_cli/formatters/csv.rb', line 28

def render_headers(header)
  output = header
  if header.respond_to?(:to_hash)
    output = GithubCLI::Util.flatten_hash(header.to_hash)
    output = "Index,#{output.keys.join(',')}\n"
  elsif header.respond_to?(:to_ary)
    output = "Index,#{header.join(',')}\n"
  end

  Terminal.line output
end

#render_line(index, item) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/github_cli/formatters/csv.rb', line 40

def render_line(index, item)
  output = item
  if item.respond_to?(:to_hash)
    output = GithubCLI::Util.flatten_hash(item.to_hash)
    output = output.values.join(',')
  elsif item.respond_to?(:to_ary)
    output = item.join(',')
  end

  $stdout.printf "%d,%s", index, output
end