true_table 0.2.3 → 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: 1ec9c37621fb36832b5492cc2b467541ad646056fa245ed701c30a94c900028b
4
- data.tar.gz: 265b809a637e10431299f01f8a209bd038909c6d35bad8843d66763fe3fbcf23
3
+ metadata.gz: 41a7a4ccfb0c3f0d11eef867f2827c3bc64ea658301586911f1982750e46d7dd
4
+ data.tar.gz: 041fea8ad2ddc7265bf1d5d8fde35fb8b7fb892cd0fa68efa8f0ceb326ac6311
5
5
  SHA512:
6
- metadata.gz: e7ef3be0f82f07c73d9dfeec1cdedc15f79badcd6099de58f0292bda033a80ae242dee794d0e991b68e4bbb4d700d7115af677c03af55965389b47e1fb88c61b
7
- data.tar.gz: 4db2c8cfefc89afe2b2820c6e77221b90c36e227b53b700d60ae07b9b873d7b1f77490b341fe88c6d421e63997ecb0743589d3fea3b9bf395e6e7f0faaf6e8ce
6
+ metadata.gz: 54fad92ee01565916486099a327196cc81dfd7b3f1b04f31734b1d50b5d6d310656d6b22e351a1d39f9f16cd709f6fa039413edab34e44083f3d2ce1f099f9d2
7
+ data.tar.gz: adda17f895020dea8fe68a708c01ac3a94c68d1cd868ea3329ca77c4869fc3b9dbe84af216c796b8b8e9125284b0d988801e2ea9b243a3213509f204fbe920dd
@@ -1,3 +1,3 @@
1
1
  class TrueTable < Array
2
- VERSION = '0.2.3'
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)
@@ -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,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.3
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: 2023-06-01 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
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
- rubygems_version: 3.4.13
42
+ rubygems_version: 3.4.14
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: Simple and intuitive tabular data type