Class: Tap::Support::SimpleTable

Inherits:
Object
  • Object
show all
Defined in:
lib/tap/support/simple_table.rb

Overview

SimpleTable represents a simple table where each row has the same number of columns. Provides accessors of column data, and supports headers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ SimpleTable

Returns a new instance of SimpleTable.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tap/support/simple_table.rb', line 33

def initialize(data, options={})
  @data = data
  @default_value = options[:default_value] || nil
  @headers = case options[:header_row]
  when true
    if @data.length == 0
      raise "no header row available"
    end
    
    @data.shift
  else []
  end
   
  normalize!
end

Instance Attribute Details

#dataObject (readonly)

An array of table data. Each entry (row) is an array of column data padded to a uniform length (n_columns) using default_value.



22
23
24
# File 'lib/tap/support/simple_table.rb', line 22

def data
  @data
end

#default_valueObject (readonly)

The default value for empty cells



25
26
27
# File 'lib/tap/support/simple_table.rb', line 25

def default_value
  @default_value
end

#headersObject (readonly)

An array of headers, padded to n_columns.



31
32
33
# File 'lib/tap/support/simple_table.rb', line 31

def headers
  @headers
end

#n_columnsObject (readonly)

The number of columns in self



28
29
30
# File 'lib/tap/support/simple_table.rb', line 28

def n_columns
  @n_columns
end

Class Method Details

.parse_data(string, row_delimiter = /\r?\n/, col_delimiter = "\t") ⇒ Object

Parses the string into an array of table data, using the specified row and column delimiters.



13
14
15
16
17
# File 'lib/tap/support/simple_table.rb', line 13

def parse_data(string, row_delimiter=/\r?\n/, col_delimiter="\t")
  string.split(row_delimiter).collect do |row|
    row.split(col_delimiter)
  end
end

Instance Method Details

#blank_rowObject

Returns an array of n_columns length and default_value



68
69
70
# File 'lib/tap/support/simple_table.rb', line 68

def blank_row
  Array.new(n_columns, default_value)
end

#column(index) ⇒ Object

Returns column data for the specified index.



73
74
75
# File 'lib/tap/support/simple_table.rb', line 73

def column(index)
  data.collect {|row| row[index] }
end

#column_by_header(header) ⇒ Object

Returns column data for the specified header.



78
79
80
81
82
83
# File 'lib/tap/support/simple_table.rb', line 78

def column_by_header(header)
  index = headers.index(header)
  raise "could not find header: #{header}" if index == nil

  column(index)
end

#join(row_delimiter = "\n", col_delimiter = "\t", include_headers = true) ⇒ Object



85
86
87
88
89
90
# File 'lib/tap/support/simple_table.rb', line 85

def join(row_delimiter="\n", col_delimiter="\t", include_headers=true)
  (include_headers ? headers.join(col_delimiter) + row_delimiter : "") +
  data.collect do |row|
    row.join(col_delimiter)
  end.join(row_delimiter)
end

#n_rowsObject

The number of rows in self.



63
64
65
# File 'lib/tap/support/simple_table.rb', line 63

def n_rows
  @data.length
end

#normalize!Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tap/support/simple_table.rb', line 49

def normalize!
  # Determine the number of columns in self.
  @n_columns = data.inject(0) do |max, column|
    max > column.length ? max : column.length
  end
  
  # Normalize the data rows to the number of columns.
  @data = data.collect {|row| normalize_row(row) }
  @headers = normalize_row(@headers)
  
  self
end