Class: TermUtils::Tab::Table
- Inherits:
-
Object
- Object
- TermUtils::Tab::Table
- Defined in:
- lib/term_utils/tab.rb
Overview
Represents a table.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#define_column(id, opts = {}, &block) ⇒ Tab::Column
Defines a column.
-
#find_column(id) ⇒ Tab::Column?
Finds a column.
-
#initialize(opts = {}) ⇒ Table
constructor
A new instance of Table.
-
#print_data(io, values, opts = {}) ⇒ nil
Prints a data row.
-
#print_header(io, values = nil, opts = {}) ⇒ nil
Prints a header row.
-
#print_separator(io, opts = {}) ⇒ nil
Prints a separator row.
-
#printer(io, opts = {}, &block) ⇒ Tab::Printer
Creates a new table printer.
-
#titles ⇒ Hash<Symbol, String>
Returns column titles.
Constructor Details
#initialize(opts = {}) ⇒ Table
Returns a new instance of Table.
11 12 13 14 |
# File 'lib/term_utils/tab.rb', line 11 def initialize(opts = {}) @id = opts.fetch(:id, nil) @columns = [] end |
Instance Attribute Details
#columns ⇒ Array<Tab::Column>
8 9 10 |
# File 'lib/term_utils/tab.rb', line 8 def columns @columns end |
#id ⇒ Symbol
6 7 8 |
# File 'lib/term_utils/tab.rb', line 6 def id @id end |
Instance Method Details
#define_column(id, opts = {}, &block) ⇒ Tab::Column
Defines a column.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/term_utils/tab.rb', line 20 def define_column(id, opts = {}, &block) col = @columns.find { |c| c.id == id } if col block.call(col) if block col.validate else opts[:id] = id opts[:index] = @columns.length col = Column.new(opts) block.call(col) if block col.validate @columns << col end col end |
#find_column(id) ⇒ Tab::Column?
Finds a column.
38 39 40 |
# File 'lib/term_utils/tab.rb', line 38 def find_column(id) @columns.find { |c| c.id == id } end |
#print_data(io, values, opts = {}) ⇒ nil
Prints a data row.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/term_utils/tab.rb', line 88 def print_data(io, values, opts = {}) vals = values if values.is_a? Hash vals = [] @columns.each do |col| vals << values[col.id] end end raise "wrong values (not array)" unless vals.is_a? Array offset = opts.fetch(:offset, 0) column_separator_width = opts.fetch(:column_separator_width, 2) sb = StringIO.new sb << " " * offset if offset > 0 @columns.each do |col| sb << " " * column_separator_width if col.index > 0 sb << col.render_data(vals[col.index]) end io.puts sb.string end |
#print_header(io, values = nil, opts = {}) ⇒ nil
Prints a header row.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/term_utils/tab.rb', line 60 def print_header(io, values = nil, opts = {}) vals = values if values.nil? vals = @columns.map { |col| col.id.to_s } elsif values.is_a? Hash vals = [] @columns.each do |col| vals << values[col.id] end end raise "wrong values (not array)" unless vals.is_a? Array offset = opts.fetch(:offset, 0) column_separator_width = opts.fetch(:column_separator_width, 2) sb = StringIO.new sb << " " * offset if offset > 0 @columns.each do |col| sb << " " * column_separator_width if col.index > 0 sb << col.render_header(vals[col.index]) end io.puts sb.string end |
#print_separator(io, opts = {}) ⇒ nil
Prints a separator row.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/term_utils/tab.rb', line 113 def print_separator(io, opts = {}) offset = opts.fetch(:offset, 0) column_separator_width = opts.fetch(:column_separator_width, 2) sb = StringIO.new sb << " " * offset if offset > 0 @columns.each do |col| sb << " " * column_separator_width if col.index > 0 sb << "-" * col.width end io.puts sb.string end |
#printer(io, opts = {}, &block) ⇒ Tab::Printer
Creates a new table printer.
48 49 50 51 52 |
# File 'lib/term_utils/tab.rb', line 48 def printer(io, opts = {}, &block) ptr = Printer.new(self, io, opts) block.call(ptr) if block ptr end |
#titles ⇒ Hash<Symbol, String>
Returns column titles.
126 127 128 129 130 131 132 |
# File 'lib/term_utils/tab.rb', line 126 def titles h = {} @columns.each do |col| h[col.id] = col.id.to_s end h end |