Class: Eco::CSV::Table

Inherits:
CSV::Table
  • Object
show all
Defined in:
lib/eco/csv/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Table

Returns a new instance of Table.

Parameters:

  • input (Array<Row>, Array<Array>, Eco::CSV::Table, ::CSV::Table)
    • when Array<Array> => all rows as arrays where first array is the header


8
9
10
# File 'lib/eco/csv/table.rb', line 8

def initialize(input)
  super(to_rows_array(input))
end

Instance Method Details

#add_column(header_name) ⇒ Eco::CSV::Table

Adds a new column at the end

Parameters:

  • header_name (String)

    header of the new column

Returns:



32
33
34
35
# File 'lib/eco/csv/table.rb', line 32

def add_column(header_name)
  new_col = Array.new(length).unshift(header_name)
  columns_to_table(columns.push(new_col))
end

#columnsArray<Array>

Returns each array is the column header followed by its values.

Returns:

  • (Array<Array>)

    each array is the column header followed by its values



25
26
27
# File 'lib/eco/csv/table.rb', line 25

def columns
  to_a.transpose
end

#columns_hashHash

Note:

it will override columns with same header name

Returns keys are headers, values are arrays.

Returns:

  • (Hash)

    keys are headers, values are arrays



39
40
41
42
43
# File 'lib/eco/csv/table.rb', line 39

def columns_hash
  columns.map do |col|
    [col.first, col[1..-1]]
  end.to_h
end

#lengthInteger

Returns total number of rows not including the header.

Returns:

  • (Integer)

    total number of rows not including the header



20
21
22
# File 'lib/eco/csv/table.rb', line 20

def length
  to_a.length - 1
end

#rowsArray<::CSV::Row>

Returns:

  • (Array<::CSV::Row>)


13
14
15
16
17
# File 'lib/eco/csv/table.rb', line 13

def rows
  [].tap do |out|
    self.each {|row| out << row}
  end
end