Class: TrueTable::Table

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

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object

Combines table with other and returns a new one



4
5
6
7
8
# File 'lib/true_table/table.rb', line 4

def +(other)
  result = self.class.new
  each_row { |row, i| result << row.merge(other[i]) }
  result
end

#-(cols) ⇒ Object

Returns a new table without the specified columns



11
12
13
14
15
16
# File 'lib/true_table/table.rb', line 11

def -(cols)
  keep_keys = headers - cols
  result = self.class.new
  each_row { |row, i| result << row.slice(*keep_keys) }
  result
end

#[](key) ⇒ Object Also known as: row

Returns a row or a column



19
20
21
# File 'lib/true_table/table.rb', line 19

def [](key)
  key.is_a?(Symbol) ? col(key) : super
end

#[]=(key, value) ⇒ Object

Adds or updates a row or a column



24
25
26
# File 'lib/true_table/table.rb', line 24

def []=(key, value)
  key.is_a?(Symbol) ? add_col(key.to_sym, value) : super
end

#cloneObject

Returns a deep copy of self



29
30
31
# File 'lib/true_table/table.rb', line 29

def clone
  self.class.new map(&:clone)
end

#col(key) ⇒ Object

Returns a column as Array



34
35
36
# File 'lib/true_table/table.rb', line 34

def col(key)
  map { |row| row[key] }
end

#colsObject

Returns a hash of columns



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

def cols
  result = {}
  each_col { |col, header| result[header] = col }
  result
end

#compactObject

Returns a copy of self without rows that contain nil in any column



46
47
48
# File 'lib/true_table/table.rb', line 46

def compact
  dup.compact!
end

#compact!Object

Removes rows with nil in any column



51
52
53
# File 'lib/true_table/table.rb', line 51

def compact!
  delete_if { |row| row.values.include? nil }
end

#delete_at(index) ⇒ Object Also known as: delete_col, delete_row

Delete a row or a column in place and returns the deleted row/column



56
57
58
59
60
61
62
63
64
65
# File 'lib/true_table/table.rb', line 56

def delete_at(index)
  if index.is_a?(Symbol) || index.is_a?(String)
    result = self[index]
    return nil unless result
    each_row { |row, i| row.delete index }
    result
  else
    super
  end
end

#difference(*others) ⇒ Object

Returns a table with different rows



70
71
72
# File 'lib/true_table/table.rb', line 70

def difference(*others)
  self.class.new super
end

#dig(*indexes) ⇒ Object

Extracts nested value. Accepts row, column or column, row



75
76
77
78
79
80
81
82
# File 'lib/true_table/table.rb', line 75

def dig(*indexes)
  key = indexes.shift
  if key.is_a?(Symbol)
    col(key.to_sym).dig *indexes
  else
    row(key).dig *indexes
  end
end

#each_colObject

Iterates over columns



85
86
87
# File 'lib/true_table/table.rb', line 85

def each_col
  headers.each { |header| yield col(header), header }
end

#fetch(key, *default) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/true_table/table.rb', line 92

def fetch(key, *default)
  if key.is_a?(Symbol)
    if headers.include? key
      col(key.to_sym)
    elsif default.any?
      default.first
    elsif block_given?
      yield key
    else
      raise IndexError, "row :#{key} does not exist"
    end
  else
    super
  end
end

#headersObject

Returns an array of column headers



109
110
111
# File 'lib/true_table/table.rb', line 109

def headers
  first.keys
end

#intersection(*others) ⇒ Object

Returns a new table with intersecting rows



114
115
116
# File 'lib/true_table/table.rb', line 114

def intersection(*others)
  self.class.new super
end

#join(row_separator = $,, col_separator = nil, with_headers: false) ⇒ Object

Returns a string with joined rows and columns



119
120
121
122
123
124
125
126
# File 'lib/true_table/table.rb', line 119

def join(row_separator = $,, col_separator = nil, with_headers: false)
  if col_separator
    result = map { |row| row.values.join col_separator }.join(row_separator)
    with_headers ? headers.join(col_separator) + row_separator + result : result
  else
    super row_separator
  end
end

#last(*args) ⇒ Object

Returns the last row or a new table with the last N rows



129
130
131
# File 'lib/true_table/table.rb', line 129

def last(*args)
  args.empty? ? super : self.class.new(super)
end

#rejectObject

Returns a new table without rejected rows



134
135
136
# File 'lib/true_table/table.rb', line 134

def reject
  self.class.new super
end

#reverseObject

Returns a reversed copy



139
140
141
# File 'lib/true_table/table.rb', line 139

def reverse
  self.class.new super
end

#rotate(count = 1) ⇒ Object

Returns a new table with the element at count as the first element



144
145
146
# File 'lib/true_table/table.rb', line 144

def rotate(count = 1)
  self.class.new super
end

#selectObject Also known as: filter

Returns a new table with selected rows



152
153
154
# File 'lib/true_table/table.rb', line 152

def select
  self.class.new super
end

#shuffle(*args) ⇒ Object

Returns a new shuffled tables



158
159
160
# File 'lib/true_table/table.rb', line 158

def shuffle(*args)
  self.class.new super
end

#sortObject

Returns a new sorted table



163
164
165
# File 'lib/true_table/table.rb', line 163

def sort
  self.class.new super
end

#sort_byObject

Returns a new sorted table



168
169
170
# File 'lib/true_table/table.rb', line 168

def sort_by
  self.class.new super
end

#to_csv(row_separator = "\n", col_separator = ",") ⇒ Object

Returns a CSV string



173
174
175
# File 'lib/true_table/table.rb', line 173

def to_csv(row_separator = "\n", col_separator = ",")
  join(row_separator, col_separator, with_headers: true)
end

#to_hObject

Returns a hash representation of the table using the values of the first column as hash keys



179
180
181
# File 'lib/true_table/table.rb', line 179

def to_h
  map { |row| [row.values.first, row] }.to_h
end

#valuesObject

Returns only values, without any headers (array of arrays)



184
185
186
# File 'lib/true_table/table.rb', line 184

def values
  map { |row| row.values }
end