Class: Daru::Formatters::Table

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

Constant Summary collapse

DEFAULT_SPACING =
10
DEFAULT_THRESHOLD =
15

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
# 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
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



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

def format threshold=nil, spacing=nil
  rows = build_rows(threshold || DEFAULT_THRESHOLD)

  formatter = construct_formatter rows, spacing || DEFAULT_SPACING

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