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.
24 25 26 27 |
# File 'lib/term_utils/tab.rb', line 24 def initialize(opts = {}) @id = opts.fetch(:id, nil) @columns = [] end |
Instance Attribute Details
#columns ⇒ Array<Tab::Column>
21 22 23 |
# File 'lib/term_utils/tab.rb', line 21 def columns @columns end |
#id ⇒ Symbol
19 20 21 |
# File 'lib/term_utils/tab.rb', line 19 def id @id end |
Instance Method Details
#define_column(id, opts = {}, &block) ⇒ Tab::Column
Defines a column.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/term_utils/tab.rb', line 33 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.
51 52 53 |
# File 'lib/term_utils/tab.rb', line 51 def find_column(id) @columns.find { |c| c.id == id } end |
#print_data(io, values, opts = {}) ⇒ nil
Prints a data row.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/term_utils/tab.rb', line 101 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/term_utils/tab.rb', line 73 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.
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/term_utils/tab.rb', line 126 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.
61 62 63 64 65 |
# File 'lib/term_utils/tab.rb', line 61 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.
139 140 141 142 143 144 145 |
# File 'lib/term_utils/tab.rb', line 139 def titles h = {} @columns.each do |col| h[col.id] = col.id.to_s end h end |