true_table 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53e26ebda846b40d7f2399e318b3be09ce49c71ddebbab97bfc34fbd64d664b4
4
- data.tar.gz: 1427d9de17b647d15469522ea5483b268ca41ef82a325b286617dc72ada93ad9
3
+ metadata.gz: 842b30f634d0113dac1917cacac6b6cf4265a3bb50b37361aa8ae4f36bd4fec6
4
+ data.tar.gz: b15972166d48d756538b22e4074f3700d862460f0e5412ce12463261998bb3da
5
5
  SHA512:
6
- metadata.gz: b2aa85ebfd3c93611bcd499fa1c551dfac70b552353e89e60e0a68362763c9e8e5663d9557d310d36fb488409aaf3fac46234dc95fe18644422fc7654ec05155
7
- data.tar.gz: 9c8d9423738e0724c3bbcac5fc93187c8bd8c2e68cd5cb8a481324bd0b2080b3087ff9dad498b024ec41f0784a061afc0988c5654f177073c2a1334ec9cb78e1
6
+ metadata.gz: 502b4c1018f441800f298a45b0cc0bfe61ae1e6f528e2178839492e2d08206d85e0672cbc0f849e47f3f4d91433fcc97f060406a8bad949453b862983cc1a31b
7
+ data.tar.gz: ea29b5abe5a3f090db4932ba64daa42d89bc8f9413ec5c8db225ce521bfd662443e619726269e4f7bba2d53c35620c093553b7b138e3bd577a6046f87a351d45
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/true_table.svg)](https://badge.fury.io/rb/true_table)
4
4
  [![Build Status](https://github.com/DannyBen/true_table/workflows/Test/badge.svg)](https://github.com/DannyBen/true_table/actions?query=workflow%3ATest)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/a03534215d36e7892730/maintainability)](https://codeclimate.com/github/DannyBen/true_table/maintainability)
5
6
 
6
7
  ---
7
8
 
@@ -0,0 +1,3 @@
1
+ class TrueTable < Array
2
+ VERSION = '0.2.2'
3
+ end
data/lib/true_table.rb CHANGED
@@ -42,10 +42,10 @@ class TrueTable < Array
42
42
  end
43
43
 
44
44
  # Returns a new table without the specified columns
45
- def -(cols)
46
- keep_keys = headers - cols
45
+ def -(other)
46
+ keep_keys = headers - other
47
47
  result = self.class.new
48
- each_row { |row, i| result << row.slice(*keep_keys) }
48
+ each_row { |row, _i| result << row.slice(*keep_keys) }
49
49
  result
50
50
  end
51
51
 
@@ -83,7 +83,7 @@ class TrueTable < Array
83
83
 
84
84
  # Removes rows with nil in any column
85
85
  def compact!
86
- delete_if { |row| row.values.include? nil }
86
+ delete_if { |row| row.has_value?(nil) }
87
87
  end
88
88
 
89
89
  # Delete a row or a column in place and returns the deleted row/column
@@ -91,7 +91,8 @@ class TrueTable < Array
91
91
  if index.is_a?(Symbol) || index.is_a?(String)
92
92
  result = self[index]
93
93
  return nil unless result
94
- each_row { |row, i| row.delete index }
94
+
95
+ each_row { |row, _i| row.delete index }
95
96
  result
96
97
  else
97
98
  super
@@ -109,12 +110,22 @@ class TrueTable < Array
109
110
  def dig(*indexes)
110
111
  key = indexes.shift
111
112
  if key.is_a?(Symbol)
112
- col(key.to_sym).dig *indexes
113
+ col(key.to_sym).dig(*indexes)
113
114
  else
114
- row(key).dig *indexes
115
+ row(key).dig(*indexes)
115
116
  end
116
117
  end
117
118
 
119
+ # Returns a new table without the first N rows
120
+ def drop(*args)
121
+ self.class.new super
122
+ end
123
+
124
+ # Returns a new table with rows until the block returns false
125
+ def drop_while(*args)
126
+ self.class.new super
127
+ end
128
+
118
129
  # Iterates over columns
119
130
  def each_col
120
131
  headers.each { |header| yield col(header), header }
@@ -187,6 +198,11 @@ class TrueTable < Array
187
198
  File.write path, to_csv
188
199
  end
189
200
 
201
+ # Saves the table as a TSV file
202
+ def save_tsv(path)
203
+ File.write path, to_tsv
204
+ end
205
+
190
206
  # Returns a new table with selected rows
191
207
  def select
192
208
  self.class.new super
@@ -198,6 +214,24 @@ class TrueTable < Array
198
214
  self.class.new super
199
215
  end
200
216
 
217
+ # Returns a new table slice
218
+ def slice(*args)
219
+ if (args.count == 1) && args.first.is_a?(Integer)
220
+ super
221
+ else
222
+ self.class.new super
223
+ end
224
+ end
225
+
226
+ # Deletes and returns one more rows
227
+ def slice!(*args)
228
+ if (args.count == 1) && args.first.is_a?(Integer)
229
+ super
230
+ else
231
+ self.class.new super
232
+ end
233
+ end
234
+
201
235
  # Returns a new sorted table
202
236
  def sort
203
237
  self.class.new super
@@ -208,20 +242,35 @@ class TrueTable < Array
208
242
  self.class.new super
209
243
  end
210
244
 
245
+ # Returns a new table with the first N rows
246
+ def take(*args)
247
+ self.class.new super
248
+ end
249
+
250
+ # Returns a new table with rows until the block returns false
251
+ def take_while(*args)
252
+ self.class.new super
253
+ end
254
+
211
255
  # Returns a CSV string
212
- def to_csv(row_separator = "\n", col_separator = ",")
256
+ def to_csv(row_separator = "\n", col_separator = ',')
257
+ join(row_separator, col_separator, with_headers: true)
258
+ end
259
+
260
+ # Returns a TSV string
261
+ def to_tsv(row_separator = "\n", col_separator = "\t")
213
262
  join(row_separator, col_separator, with_headers: true)
214
263
  end
215
264
 
216
265
  # Returns a hash representation of the table using the values of the
217
266
  # first column as hash keys
218
267
  def to_h
219
- map { |row| [row.values.first, row] }.to_h
268
+ super { |row| [row.values.first, row] }
220
269
  end
221
270
 
222
271
  # Returns only values, without any headers (array of arrays)
223
272
  def values
224
- map { |row| row.values }
273
+ map(&:values)
225
274
  end
226
275
 
227
276
  protected
@@ -232,5 +281,4 @@ protected
232
281
  self[i][key] = value
233
282
  end
234
283
  end
235
-
236
284
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: true_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-09 00:00:00.000000000 Z
11
+ date: 2022-12-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple and intuitive tabular data type
14
14
  email: db@dannyben.com
@@ -18,10 +18,12 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - README.md
20
20
  - lib/true_table.rb
21
+ - lib/true_table/version.rb
21
22
  homepage: https://github.com/dannyben/true_table
22
23
  licenses:
23
24
  - MIT
24
- metadata: {}
25
+ metadata:
26
+ rubygems_mfa_required: 'true'
25
27
  post_install_message:
26
28
  rdoc_options: []
27
29
  require_paths:
@@ -37,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
39
  - !ruby/object:Gem::Version
38
40
  version: '0'
39
41
  requirements: []
40
- rubygems_version: 3.1.2
42
+ rubygems_version: 3.3.26
41
43
  signing_key:
42
44
  specification_version: 4
43
45
  summary: Simple and intuitive tabular data type