Module: ZTK::Report::Spreadsheet

Included in:
ZTK::Report
Defined in:
lib/ztk/report/spreadsheet.rb

Overview

Report Spreadsheet Functionality

Instance Method Summary (collapse)

Instance Method Details

- (OpenStruct) spreadsheet(dataset, headers, &block)

Displays data in a spreadsheet style.

+-------------+-------+-------+--------+----------------+-------------------+--------------+---------+
| NAME        | ALIVE | ARCH  | DISTRO | IP             | MAC               | CHEF VERSION | PERSIST |
+-------------+-------+-------+--------+----------------+-------------------+--------------+---------+
| sudo        | false | amd64 | ubuntu | 192.168.99.110 | 00:00:5e:34:d6:aa | N/A          | true    |
| timezone    | false | amd64 | ubuntu | 192.168.122.47 | 00:00:5e:92:d7:f6 | N/A          | true    |
| chef-client | false | amd64 | ubuntu | 192.168.159.98 | 00:00:5e:c7:ce:26 | N/A          | true    |
| users       | false | amd64 | ubuntu | 192.168.7.78   | 00:00:5e:89:f9:50 | N/A          | true    |
+-------------+-------+-------+--------+----------------+-------------------+--------------+---------+

Parameters:

  • dataset (Array<Object>, Object)

    A single object or an array of objects for which we want to generate a report

  • headers (Array)

    An array of headers used for ordering the output.

Returns:

  • (OpenStruct)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ztk/report/spreadsheet.rb', line 22

def spreadsheet(dataset, headers, &block)
  !block_given? and log_and_raise(ReportError, "You must supply a block!")
  headers.nil? and log_and_raise(ReportError, "Headers can not be nil!")
  dataset.nil? and log_and_raise(ReportError, "Dataset can not be nil!")

  rows = Array.new
  max_lengths = OpenStruct.new
  headers = headers.map(&:to_s).map(&:downcase).map(&:to_sym)

  if dataset.is_a?(Array)
    dataset.each do |data|
      rows << block.call(data)
    end
  else
    rows << block.call(dataset)
  end
  rows.compact!

  if rows.count > 0
    max_lengths = max_spreadsheet_lengths(headers, rows)
    header_line = headers.collect { |header| "%-#{max_lengths.send(:table)[header]}s" % header.to_s.upcase }
    header_line = format_row(header_line)

    config.ui.stdout.puts(format_header(headers, max_lengths))
    config.ui.stdout.puts(header_line)
    config.ui.stdout.puts(format_header(headers, max_lengths))

    rows.each do |row|
      row_line = headers.collect do |header|
        header_length = max_lengths.send(:table)[header]
        content = (row.send(:table) rescue row)[header]

        "%-#{header_length}s" % content
      end

      row_line = format_row(row_line)
      config.ui.stdout.puts(row_line)
    end

    config.ui.stdout.puts(format_header(headers, max_lengths))
    OpenStruct.new(:rows => rows, :max_lengths => max_lengths, :width => calculate_spreadsheet_width(headers, max_lengths))
  else
    OpenStruct.new(:rows => rows, :max_lengths => 0, :width => 0)
  end

end