Class: Daru::Formatters::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/daru/formatters/table.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, headers, row_headers) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
# File 'lib/daru/formatters/table.rb', line 9

def initialize(data, headers, row_headers)
  @data = data || []
  @headers = (headers || []).to_a
  @row_headers = (row_headers || []).to_a
  @row_headers = [''] * @data.to_a.size if @row_headers.empty?
end

Class Method Details

.format(data, options = {}) ⇒ Object



4
5
6
7
# File 'lib/daru/formatters/table.rb', line 4

def self.format data, options={}
  new(data, options[:headers], options[:row_headers])
    .format(options[:threshold], options[:spacing])
end

Instance Method Details

#format(threshold = nil, spacing = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/daru/formatters/table.rb', line 16

def format threshold=nil, spacing=nil
  rows = build_rows(threshold || Daru.max_rows)

  formatter = construct_formatter rows, spacing || Daru.spacing

  rows.map { |r| formatter % r }.join("\n")
end