true_table 0.2.3 → 0.2.5

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: 1ec9c37621fb36832b5492cc2b467541ad646056fa245ed701c30a94c900028b
4
- data.tar.gz: 265b809a637e10431299f01f8a209bd038909c6d35bad8843d66763fe3fbcf23
3
+ metadata.gz: 3adeeb706cba6ebb57901dde8535477b461a3643ffed7453bba2a27cda4c7c54
4
+ data.tar.gz: 1f1f800fc57e62e607f048ae78b4d7312990aad82ef0508b0d9faa87873c8afd
5
5
  SHA512:
6
- metadata.gz: e7ef3be0f82f07c73d9dfeec1cdedc15f79badcd6099de58f0292bda033a80ae242dee794d0e991b68e4bbb4d700d7115af677c03af55965389b47e1fb88c61b
7
- data.tar.gz: 4db2c8cfefc89afe2b2820c6e77221b90c36e227b53b700d60ae07b9b873d7b1f77490b341fe88c6d421e63997ecb0743589d3fea3b9bf395e6e7f0faaf6e8ce
6
+ metadata.gz: d2326fbc527be2af1b6521bdfd83ff5ee8def1795bc2c295613913a1235c0afd5cb34c88da2b9a3fa043c2c67a494a3c568facba6fc54717f4c098a979ac0076
7
+ data.tar.gz: 74c5c8b8d589928f9cda11dc04e7fa3f4088eb83b11bc17001c017afb3542994747d53309460c4f8f06c06561949cca46f0caff062a52a3abe43b423b8510133
@@ -1,3 +1,3 @@
1
1
  class TrueTable < Array
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.5'
3
3
  end
data/lib/true_table.rb CHANGED
@@ -51,12 +51,20 @@ class TrueTable < Array
51
51
 
52
52
  # Returns a row or a column
53
53
  def [](key)
54
- key.is_a?(Symbol) ? col(key) : super
54
+ case key
55
+ when Symbol then col(key)
56
+ when Hash then row(key)
57
+ else super
58
+ end
55
59
  end
56
60
 
57
61
  # Adds or updates a row or a column
58
62
  def []=(key, value)
59
- key.is_a?(Symbol) ? add_col(key.to_sym, value) : super
63
+ case key
64
+ when Symbol then add_col(key.to_sym, value)
65
+ when Hash then add_row(key, value)
66
+ else super
67
+ end
60
68
  end
61
69
 
62
70
  # Returns a deep copy of self
@@ -134,19 +142,29 @@ class TrueTable < Array
134
142
  # Iterates over rows
135
143
  alias each_row each_with_index
136
144
 
145
+ # Returns a row or a column
137
146
  def fetch(key, *default)
138
- if key.is_a?(Symbol)
139
- if headers.include? key
140
- col(key.to_sym)
141
- elsif default.any?
142
- default.first
143
- elsif block_given?
144
- yield key
147
+ case key
148
+ when Symbol
149
+ if headers.include?(key) then col(key.to_sym)
150
+ elsif default.any? then default.first
151
+ elsif block_given? then yield key
145
152
  else
146
- raise IndexError, "row :#{key} does not exist"
153
+ raise IndexError, "col :#{key} does not exist"
147
154
  end
155
+
156
+ when Hash
157
+ result = row(key)
158
+ if result then result
159
+ elsif default.any? then default.first
160
+ elsif block_given? then yield key
161
+ else
162
+ raise IndexError, "row #{key} does not exist"
163
+ end
164
+
148
165
  else
149
166
  super
167
+
150
168
  end
151
169
  end
152
170
 
@@ -155,6 +173,11 @@ class TrueTable < Array
155
173
  first.keys
156
174
  end
157
175
 
176
+ # Returns a short inspection string
177
+ def inspect
178
+ "<#{self.class}:#{object_id} size=#{size} headers=#{headers}>"
179
+ end
180
+
158
181
  # Returns a new table with intersecting rows
159
182
  def intersection(*others)
160
183
  self.class.new super
@@ -191,7 +214,12 @@ class TrueTable < Array
191
214
  end
192
215
 
193
216
  # Returns a row
194
- alias row []
217
+ def row(key)
218
+ key.is_a?(Hash) ? find { |x| x[key.first.first] == key.first.last } : self[key]
219
+ end
220
+
221
+ # Returns all rows
222
+ alias rows to_a
195
223
 
196
224
  # Saves the table as a CSV file
197
225
  def save_csv(path)
@@ -286,4 +314,8 @@ protected
286
314
  self[i][key] = value
287
315
  end
288
316
  end
317
+
318
+ def add_row(key, values)
319
+ row(key).merge! values
320
+ end
289
321
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: true_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-01 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: csv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
13
27
  description: Simple and intuitive tabular data type
14
28
  email: db@dannyben.com
15
29
  executables: []
@@ -24,7 +38,7 @@ licenses:
24
38
  - MIT
25
39
  metadata:
26
40
  rubygems_mfa_required: 'true'
27
- post_install_message:
41
+ post_install_message:
28
42
  rdoc_options: []
29
43
  require_paths:
30
44
  - lib
@@ -39,8 +53,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  requirements: []
42
- rubygems_version: 3.4.13
43
- signing_key:
56
+ rubygems_version: 3.5.3
57
+ signing_key:
44
58
  specification_version: 4
45
59
  summary: Simple and intuitive tabular data type
46
60
  test_files: []