Class: Tablr

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Tablr

Returns a new instance of Tablr.



4
5
6
7
8
9
10
11
# File 'lib/tablr/tablr.rb', line 4

def initialize(options = {})
  @columns            ||= TablrSpace::OrderedHash.auto
  @row_data           ||= TablrSpace::OrderedHash.new
  options[:separate]  ||= "false"
  options[:headers]   ||= "true"

  @options = options
end

Instance Attribute Details

#columns(columns) ⇒ Object Also known as: column, add_column

Add columns



14
15
16
# File 'lib/tablr/tablr.rb', line 14

def columns
  @columns
end

Instance Method Details

#add_row(column, value) ⇒ Object

Add rows



21
22
23
24
25
26
27
28
# File 'lib/tablr/tablr.rb', line 21

def add_row(column, value)
  add_column(column) unless column_exists?(column) or !column.is_a? String

  Array(value).each do |v|
    @columns[column][:rows].push v
    adjust(column, v.length)
  end
end

#adjust(column, length) ⇒ Object

Adjust max width for padding



31
32
33
34
# File 'lib/tablr/tablr.rb', line 31

def adjust(column, length)
  return nil unless column_exists?(column)
  @columns[column][:length] = length if length > @columns[column][:length]
end


36
37
38
39
40
41
42
43
# File 'lib/tablr/tablr.rb', line 36

def print
  str = <<EOL
#{headers}
#{lines}
#{print_rows}#{lines}
EOL
  puts str
end