true_table 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3373190811f6525585ae75db80f24ba653816005938d107734b653dd7a84d9cd
4
- data.tar.gz: ced003b960d0be4b45a0272f3186d7d5fcd0161ffd609967850ca5d69289e424
3
+ metadata.gz: 1ec9c37621fb36832b5492cc2b467541ad646056fa245ed701c30a94c900028b
4
+ data.tar.gz: 265b809a637e10431299f01f8a209bd038909c6d35bad8843d66763fe3fbcf23
5
5
  SHA512:
6
- metadata.gz: 176bb664a7e4357c43fcad729ba7334599e461e8ed1579336078ff7704eafa1b038c2b3cd085559a89cc7147ba13a3ff8f1c853f498b333dbf03284e2d52e0bd
7
- data.tar.gz: 61b623d2fd727b89c2e972fa859906f11f042474e77a933239a83354ea6f6175f2704317ea2cc6aca8d1f3f43f6843baeace9a7de84280c621fb16bd096e4306
6
+ metadata.gz: e7ef3be0f82f07c73d9dfeec1cdedc15f79badcd6099de58f0292bda033a80ae242dee794d0e991b68e4bbb4d700d7115af677c03af55965389b47e1fb88c61b
7
+ data.tar.gz: 4db2c8cfefc89afe2b2820c6e77221b90c36e227b53b700d60ae07b9b873d7b1f77490b341fe88c6d421e63997ecb0743589d3fea3b9bf395e6e7f0faaf6e8ce
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.3'
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,9 +110,9 @@ 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
 
@@ -197,6 +198,11 @@ class TrueTable < Array
197
198
  File.write path, to_csv
198
199
  end
199
200
 
201
+ # Saves the table as a TSV file
202
+ def save_tsv(path)
203
+ File.write path, to_tsv
204
+ end
205
+
200
206
  # Returns a new table with selected rows
201
207
  def select
202
208
  self.class.new super
@@ -210,7 +216,7 @@ class TrueTable < Array
210
216
 
211
217
  # Returns a new table slice
212
218
  def slice(*args)
213
- if args.count == 1 and args.first.is_a? Integer
219
+ if (args.count == 1) && args.first.is_a?(Integer)
214
220
  super
215
221
  else
216
222
  self.class.new super
@@ -219,7 +225,7 @@ class TrueTable < Array
219
225
 
220
226
  # Deletes and returns one more rows
221
227
  def slice!(*args)
222
- if args.count == 1 and args.first.is_a? Integer
228
+ if (args.count == 1) && args.first.is_a?(Integer)
223
229
  super
224
230
  else
225
231
  self.class.new super
@@ -247,19 +253,29 @@ class TrueTable < Array
247
253
  end
248
254
 
249
255
  # Returns a CSV string
250
- 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")
251
262
  join(row_separator, col_separator, with_headers: true)
252
263
  end
253
264
 
254
265
  # Returns a hash representation of the table using the values of the
255
- # first column as hash keys
266
+ # first column as hash keys. If a block is given, the results of the block
267
+ # on each element of the array will be used as key => value pair.
256
268
  def to_h
257
- map { |row| [row.values.first, row] }.to_h
269
+ if block_given?
270
+ super
271
+ else
272
+ super { |row| [row.values.first, row] }
273
+ end
258
274
  end
259
275
 
260
276
  # Returns only values, without any headers (array of arrays)
261
277
  def values
262
- map { |row| row.values }
278
+ map(&:values)
263
279
  end
264
280
 
265
281
  protected
@@ -270,5 +286,4 @@ protected
270
286
  self[i][key] = value
271
287
  end
272
288
  end
273
-
274
289
  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.1
4
+ version: 0.2.3
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-12-31 00:00:00.000000000 Z
11
+ date: 2023-06-01 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:
@@ -30,14 +32,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
30
32
  requirements:
31
33
  - - ">="
32
34
  - !ruby/object:Gem::Version
33
- version: 2.7.0
35
+ version: '3.0'
34
36
  required_rubygems_version: !ruby/object:Gem::Requirement
35
37
  requirements:
36
38
  - - ">="
37
39
  - !ruby/object:Gem::Version
38
40
  version: '0'
39
41
  requirements: []
40
- rubygems_version: 3.2.3
42
+ rubygems_version: 3.4.13
41
43
  signing_key:
42
44
  specification_version: 4
43
45
  summary: Simple and intuitive tabular data type