Class: TinyTable::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/tinytable/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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



54
55
56
# File 'lib/tinytable/table.rb', line 54

def footer
  Row.new(@footer, @alignments)
end

#headerObject



43
44
45
# File 'lib/tinytable/table.rb', line 43

def header
  Row.new(@header, @alignments)
end

#rowsObject (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

Returns:

  • (Boolean)


39
40
41
# File 'lib/tinytable/table.rb', line 39

def has_footer?
  !(footer.nil? || footer.empty?)
end

#has_header?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/tinytable/table.rb', line 31

def has_header?
  !(header.nil? || header.empty?)
end

#has_rows?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/tinytable/table.rb', line 35

def has_rows?
  !rows.empty?
end

#to_textObject



58
59
60
# File 'lib/tinytable/table.rb', line 58

def to_text
  TinyTable::TextFormatter.new(self).render
end