Class: Tailstrom::Table
- Inherits:
-
Object
- Object
- Tailstrom::Table
- Defined in:
- lib/tailstrom/table.rb
Instance Method Summary collapse
-
#initialize(schema) ⇒ Table
constructor
A new instance of Table.
- #print_header ⇒ Object
- #print_row(*cols) ⇒ Object
- #puts(*args) ⇒ Object
Constructor Details
#initialize(schema) ⇒ Table
Returns a new instance of Table.
3 4 5 6 |
# File 'lib/tailstrom/table.rb', line 3 def initialize(schema) @schema = schema @out = $stdout end |
Instance Method Details
#print_header ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tailstrom/table.rb', line 19 def print_header border = head = '' @schema.each_with_index do |col, i| if i > 0 border += '-' head += ' ' end align = col[:align].to_s == 'left' ? '-' : nil border += '-' * col[:width] head += "%#{align}#{col[:width]}s" % col[:name] end self.puts border, head, border end |
#print_row(*cols) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/tailstrom/table.rb', line 8 def print_row(*cols) cols.each_with_index do |col, i| col_schema = @schema[i] str = format_string col print ' ' if i > 0 align = col_schema[:align].to_s == 'left' ? '-' : nil printf "%#{align}#{col_schema[:width]}s", str end self.puts end |
#puts(*args) ⇒ Object
33 34 35 |
# File 'lib/tailstrom/table.rb', line 33 def puts(*args) @out.puts *args end |