Class: TinyTable::Table
- Inherits:
-
Object
- Object
- TinyTable::Table
- Defined in:
- lib/tinytable/table.rb
Instance Attribute Summary collapse
- #footer ⇒ Object
- #header ⇒ Object
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #add(*args) ⇒ Object (also: #<<)
- #align(column, alignment) ⇒ Object
- #each_row(&block) ⇒ Object
- #has_footer? ⇒ Boolean
- #has_header? ⇒ Boolean
- #has_rows? ⇒ Boolean
-
#initialize(*args) ⇒ Table
constructor
A new instance of Table.
- #to_text ⇒ Object
Constructor Details
#initialize(*args) ⇒ Table
Returns a new instance of Table.
6 7 8 9 10 11 |
# File 'lib/tinytable/table.rb', line 6 def initialize(*args) header = args.first.is_a?(Array) ? args.first : args @header = header @rows = [] @alignments = [] end |
Instance Attribute Details
#footer ⇒ Object
54 55 56 |
# File 'lib/tinytable/table.rb', line 54 def Row.new(@footer, @alignments) end |
#header ⇒ Object
43 44 45 |
# File 'lib/tinytable/table.rb', line 43 def header Row.new(@header, @alignments) end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
4 5 6 |
# File 'lib/tinytable/table.rb', line 4 def rows @rows end |
Instance Method Details
#add(*args) ⇒ Object Also known as: <<
13 14 15 16 |
# File 'lib/tinytable/table.rb', line 13 def add(*args) row = extract_row_args(args) rows << row end |
#align(column, alignment) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tinytable/table.rb', line 19 def align(column, alignment) idx = case column when String @header.index(column) when Fixnum column else raise ArgumentError.new("Received a #{column.class} but expecting a Fixnum or String") end @alignments[idx] = alignment unless idx.nil? end |
#each_row(&block) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/tinytable/table.rb', line 47 def each_row(&block) rows.each do |cells| row = Row.new(cells, @alignments) block.call(row) end end |
#has_footer? ⇒ Boolean
39 40 41 |
# File 'lib/tinytable/table.rb', line 39 def !(.nil? || .empty?) end |
#has_header? ⇒ Boolean
31 32 33 |
# File 'lib/tinytable/table.rb', line 31 def has_header? !(header.nil? || header.empty?) end |
#has_rows? ⇒ Boolean
35 36 37 |
# File 'lib/tinytable/table.rb', line 35 def has_rows? !rows.empty? end |
#to_text ⇒ Object
58 59 60 |
# File 'lib/tinytable/table.rb', line 58 def to_text TinyTable::TextFormatter.new(self).render end |