Class: TableFormatter
- Inherits:
-
Object
- Object
- TableFormatter
- Defined in:
- lib/table-formatter.rb
Overview
file: table-formatter.rb
Instance Attribute Summary collapse
-
#border ⇒ Object
Returns the value of attribute border.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #display(width = nil) ⇒ Object (also: #to_s)
-
#initialize(opt = {}) ⇒ TableFormatter
constructor
A new instance of TableFormatter.
Constructor Details
#initialize(opt = {}) ⇒ TableFormatter
Returns a new instance of TableFormatter.
9 10 11 12 13 14 15 16 |
# File 'lib/table-formatter.rb', line 9 def initialize(opt={}) o = {source: nil, labels: nil, border: true}.merge(opt) super() @source = o[:source] @labels = o[:labels] @border = o[:border] @maxwidth = 60 end |
Instance Attribute Details
#border ⇒ Object
Returns the value of attribute border.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def border @border end |
#labels ⇒ Object
Returns the value of attribute labels.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def labels @labels end |
#source ⇒ Object
Returns the value of attribute source.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def source @source end |
Instance Method Details
#display(width = nil) ⇒ Object Also known as: to_s
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/table-formatter.rb', line 18 def display(width=nil) #width ||= @maxwidth @width = width @maxwidth = width if width a = @source.map {|x| x.map.to_a}.to_a labels = @labels column_widths = fetch_column_widths(a) column_widths[-1] -= column_widths.inject(&:+) - width if width records = format_rows(a, column_widths) div = (border == true ? '-' : ' ') * records[0].length + "\n" label_buffer = '' label_buffer = format_cols(labels, column_widths) + "\n" + div if labels div + label_buffer + records.join("\n") + "\n" + div end |