true_table 0.2.2 → 0.2.4

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: 842b30f634d0113dac1917cacac6b6cf4265a3bb50b37361aa8ae4f36bd4fec6
4
- data.tar.gz: b15972166d48d756538b22e4074f3700d862460f0e5412ce12463261998bb3da
3
+ metadata.gz: 41a7a4ccfb0c3f0d11eef867f2827c3bc64ea658301586911f1982750e46d7dd
4
+ data.tar.gz: 041fea8ad2ddc7265bf1d5d8fde35fb8b7fb892cd0fa68efa8f0ceb326ac6311
5
5
  SHA512:
6
- metadata.gz: 502b4c1018f441800f298a45b0cc0bfe61ae1e6f528e2178839492e2d08206d85e0672cbc0f849e47f3f4d91433fcc97f060406a8bad949453b862983cc1a31b
7
- data.tar.gz: ea29b5abe5a3f090db4932ba64daa42d89bc8f9413ec5c8db225ce521bfd662443e619726269e4f7bba2d53c35620c093553b7b138e3bd577a6046f87a351d45
6
+ metadata.gz: 54fad92ee01565916486099a327196cc81dfd7b3f1b04f31734b1d50b5d6d310656d6b22e351a1d39f9f16cd709f6fa039413edab34e44083f3d2ce1f099f9d2
7
+ data.tar.gz: adda17f895020dea8fe68a708c01ac3a94c68d1cd868ea3329ca77c4869fc3b9dbe84af216c796b8b8e9125284b0d988801e2ea9b243a3213509f204fbe920dd
@@ -1,3 +1,3 @@
1
1
  class TrueTable < Array
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.4'
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)
@@ -263,9 +291,14 @@ class TrueTable < Array
263
291
  end
264
292
 
265
293
  # Returns a hash representation of the table using the values of the
266
- # first column as hash keys
294
+ # first column as hash keys. If a block is given, the results of the block
295
+ # on each element of the array will be used as key => value pair.
267
296
  def to_h
268
- super { |row| [row.values.first, row] }
297
+ if block_given?
298
+ super
299
+ else
300
+ super { |row| [row.values.first, row] }
301
+ end
269
302
  end
270
303
 
271
304
  # Returns only values, without any headers (array of arrays)
@@ -281,4 +314,8 @@ protected
281
314
  self[i][key] = value
282
315
  end
283
316
  end
317
+
318
+ def add_row(key, values)
319
+ row(key).merge! values
320
+ end
284
321
  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.2
4
+ version: 0.2.4
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: 2022-12-09 00:00:00.000000000 Z
11
+ date: 2023-06-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple and intuitive tabular data type
14
14
  email: db@dannyben.com
@@ -32,14 +32,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
- version: 2.7.0
35
+ version: '3.0'
36
36
  required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
- rubygems_version: 3.3.26
42
+ rubygems_version: 3.4.14
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: Simple and intuitive tabular data type