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.
27 28 29 30 |
# File 'lib/term_utils/tab.rb', line 27 def initialize(opts = {}) @id = opts.fetch(:id, nil) @columns = [] end |
Instance Attribute Details
#columns ⇒ Array<Tab::Column>
24 25 26 |
# File 'lib/term_utils/tab.rb', line 24 def columns @columns end |
#id ⇒ Symbol
22 23 24 |
# File 'lib/term_utils/tab.rb', line 22 def id @id end |
Instance Method Details
#define_column(id, opts = {}, &block) ⇒ Tab::Column
Defines a column.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/term_utils/tab.rb', line 36 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.
54 55 56 |
# File 'lib/term_utils/tab.rb', line 54 def find_column(id) @columns.find { |c| c.id == id } end |
#print_data(io, values, opts = {}) ⇒ nil
Prints a data row.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/term_utils/tab.rb', line 103 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/term_utils/tab.rb', line 75 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.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/term_utils/tab.rb', line 128 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.
63 64 65 66 67 |
# File 'lib/term_utils/tab.rb', line 63 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.
141 142 143 144 145 146 147 |
# File 'lib/term_utils/tab.rb', line 141 def titles h = {} @columns.each do |col| h[col.id] = col.id.to_s end h end |