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


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

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:



70
71
72
73
# File 'lib/eco/csv/table.rb', line 70

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



111
112
113
# File 'lib/eco/csv/table.rb', line 111

def columns
  to_a.transpose
end

#columns_hashHash

Note:

it will override columns with same header name

Creates a single Hash where each key, value is a column (header + values)

Returns:

  • (Hash)

    keys are headers, values are arrays



118
119
120
121
122
# File 'lib/eco/csv/table.rb', line 118

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

#delete_column(i) ⇒ Eco::CSV::Table

Returns:



61
62
63
64
65
# File 'lib/eco/csv/table.rb', line 61

def delete_column(i)
  csv_cols = columns
  csv_cols.delete(i)
  columns_to_table(csv_cols)
end

#delete_duplicates!Object

It removes all rows where all columns' values are the same



83
84
85
86
87
88
89
90
# File 'lib/eco/csv/table.rb', line 83

def delete_duplicates!
  unique_rows = []
  self.by_row!.delete_if do |row|
    unique_rows.any? {|done| equal_rows?(row, done)}.tap do |found|
      unique_rows << row unless found
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/eco/csv/table.rb', line 106

def empty?
  length < 1
end

#equal_rows?(row1, row2) ⇒ Boolean

Parameters:

  • row1 (CSV:Row)

    row to be compared

  • row2 (CSV:Row)

    row to be compared

  • `true` (Boolean)

    if all values of row1 are as of row2

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/eco/csv/table.rb', line 95

def equal_rows?(row1, row2)
  row1.fields.zip(row2.fields).all? do |(v1, v2)|
    v1 == v2
  end
end

#group_by(&block) ⇒ Hash

Returns where keys are the groups and the values a Eco::CSV::Table.

Returns:

  • (Hash)

    where keys are the groups and the values a Eco::CSV::Table



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

def group_by(&block)
  rows.group_by(&block).transform_values do |rows|
    self.class.new(rows)
  end
end

#lengthInteger

Returns total number of rows not including the header.

Returns:

  • (Integer)

    total number of rows not including the header



102
103
104
# File 'lib/eco/csv/table.rb', line 102

def length
  to_a.length - 1
end

#rowsArray<::CSV::Row>

Returns:

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


76
77
78
79
80
# File 'lib/eco/csv/table.rb', line 76

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

#slice(*index) ⇒ Eco::CSV::Table

Slices the selected rows

Returns:



34
35
36
37
38
39
40
41
# File 'lib/eco/csv/table.rb', line 34

def slice(*index)
  case index.first
  when Range, Numeric
    self.class.new(rows.slice(index.first))
  else
    self
  end
end

#slice_columns(*index) ⇒ Eco::CSV::Table

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eco/csv/table.rb', line 44

def slice_columns(*index)
  case index.first
  when Range, Numeric
    columns_to_table(columns.slice(index.first))
  when String
    csv_cols = columns
    csv_cols = index.each_with_object([]) do |name, cols|
      col = csv_cols.find {|col| col.first == name}
      cols << col if col
    end
    columns_to_table(csv_cols)
  else
    self
  end
end

#to_a_hObject

Note:

it will override columns with same header

Returns an array of row hashes



126
127
128
# File 'lib/eco/csv/table.rb', line 126

def to_a_h
  rows.map(&:to_h)
end

#to_array_of_hashesObject

See Also:



131
132
133
# File 'lib/eco/csv/table.rb', line 131

def to_array_of_hashes
  to_a_h
end

#transform_valuesEco::CSV::Table

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eco/csv/table.rb', line 19

def transform_values
  transformed_rows = rows.map do |row|
    res = yield(row)
    case res
    when Array
      ::CSV::Row.new(row.headers, res)
    when ::CSV::Row
      res
    end
  end
  self.class.new(transformed_rows)
end