fat_core 1.2.7 → 1.2.8
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 +4 -4
- data/.ruby-version +1 -1
- data/lib/fat_core/table.rb +33 -44
- data/lib/fat_core/version.rb +1 -1
- data/spec/lib/table_spec.rb +14 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b584803e27f06145ac112e534d19283bb680c525
|
4
|
+
data.tar.gz: 03f00dfaca603a3487db9b5f702aa188510151cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71655e984f1019ce9a7c3ae3fc6f180b317061a892dc37315070aca2c5ffc0765ba35fc4e227924bebf9145fc13b3ebe6a70dfc080a364b6d3327b1071a253b0
|
7
|
+
data.tar.gz: 7b7a975597653fa34761a7e075bb8f1f5bdd17d3018c29538bced08b47332972f208d4017d1a990a87e53f674606a61d558194a281e484e0e0838ca3ee5b2cc9
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.3.0
|
data/lib/fat_core/table.rb
CHANGED
@@ -165,58 +165,47 @@ module FatCore
|
|
165
165
|
# hash arguments. Each expression results in a column in the resulting Table
|
166
166
|
# in the order given. The expressions are evaluated in order as well.
|
167
167
|
def select(*exps)
|
168
|
+
result = Table.new
|
168
169
|
new_cols = {}
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
h = key.as_sym
|
193
|
-
new_heads << h
|
194
|
-
new_cols[h] = Column.new(header: h)
|
195
|
-
ev = Evaluator.new(vars: { row: 0 }, before: '@row += 1')
|
196
|
-
rows.each_with_index do |old_row, row_num|
|
197
|
-
new_row ||= {}
|
198
|
-
# Gather the new values computed so far for this row
|
199
|
-
new_vars = new_heads.zip(new_cols.keys
|
200
|
-
.map { |k| new_cols[k] }
|
201
|
-
.map { |c| c[row_num] })
|
202
|
-
vars = old_row.merge(Hash[new_vars])
|
170
|
+
ev = Evaluator.new(vars: { row: 0 }, before: '@row += 1')
|
171
|
+
rows.each do |old_row|
|
172
|
+
new_heads = []
|
173
|
+
new_row ||= {}
|
174
|
+
exps.each do |exp|
|
175
|
+
case exp
|
176
|
+
when Symbol, String
|
177
|
+
h = exp.as_sym
|
178
|
+
raise "Column '#{h}' in select does not exist" unless column?(h)
|
179
|
+
new_row[h] = old_row[h]
|
180
|
+
when Hash
|
181
|
+
# Note that when one of the exps is a Hash, it will contain an
|
182
|
+
# output expression for each member of the Hash, so we have to loop
|
183
|
+
# through them here.
|
184
|
+
exp.each_pair do |key, val|
|
185
|
+
# Gather the new values computed so far for this row
|
186
|
+
vars = old_row.merge(new_row)
|
187
|
+
case val
|
188
|
+
when Symbol
|
189
|
+
h = val.as_sym
|
190
|
+
raise "Column '#{h}' in select does not exist" unless vars.keys.include?(h)
|
191
|
+
new_row[key] = vars[h]
|
192
|
+
when String
|
203
193
|
# Now we have a hash, vars, of all local variables we want to be
|
204
194
|
# defined while evaluating expression xp as the value of column
|
205
195
|
# key in the new column.
|
206
|
-
|
207
|
-
|
196
|
+
h = key.as_sym
|
197
|
+
new_row[h] = ev.evaluate(val, vars: vars)
|
198
|
+
# Don't add this column to new_heads until after the eval so it
|
199
|
+
# does not shadow the existing value of row[h].
|
200
|
+
else
|
201
|
+
raise 'Hash parameters to select must be a symbol or string'
|
208
202
|
end
|
209
|
-
else
|
210
|
-
raise 'Hash parameters to select must be a symbol or string'
|
211
203
|
end
|
204
|
+
else
|
205
|
+
raise 'Parameters to select must be a symbol, string, or hash'
|
212
206
|
end
|
213
|
-
else
|
214
|
-
raise 'Parameters to select must be a symbol, string, or hash'
|
215
207
|
end
|
216
|
-
|
217
|
-
result = Table.new
|
218
|
-
new_heads.each do |h|
|
219
|
-
result.add_column(new_cols[h])
|
208
|
+
result.add_row(new_row)
|
220
209
|
end
|
221
210
|
result
|
222
211
|
end
|
data/lib/fat_core/version.rb
CHANGED
data/spec/lib/table_spec.rb
CHANGED
@@ -593,7 +593,7 @@ EOS
|
|
593
593
|
{ a: '7', 'Two words' => '8', s: '$1821', c: '$1888' }
|
594
594
|
]
|
595
595
|
tab1 = Table.new(aoh)
|
596
|
-
tab2 = tab1.select(
|
596
|
+
tab2 = tab1.select(former_s: :s, new_a: :a, renew_c: :c)
|
597
597
|
expect(tab2.headers).to eq [:former_s, :new_a, :renew_c]
|
598
598
|
end
|
599
599
|
|
@@ -608,6 +608,19 @@ EOS
|
|
608
608
|
arb: 's_squared / (a + c).to_d')
|
609
609
|
expect(tab2.headers).to eq [:two_words, :row, :s_squared, :arb]
|
610
610
|
end
|
611
|
+
|
612
|
+
it 'should be able to use old value of current column to compute new value' do
|
613
|
+
aoh = [
|
614
|
+
{ a: '5', 'Two words' => '20', s: '5_143', c: '3123' },
|
615
|
+
{ a: '4', 'Two words' => '5', s: 412, c: 6412 },
|
616
|
+
{ a: '7', 'Two words' => '8', s: '$1821', c: '$1_888' }
|
617
|
+
]
|
618
|
+
tab1 = Table.new(aoh)
|
619
|
+
tab2 = tab1.select(:two_words, s: 's * s', nc: 'c + c', c: 'nc+nc')
|
620
|
+
expect(tab2.headers).to eq [:two_words, :s, :nc, :c]
|
621
|
+
expect(tab2[:s].items).to eq([26450449, 169744, 3316041])
|
622
|
+
expect(tab2[:c].items).to eq([12492, 25648, 7552])
|
623
|
+
end
|
611
624
|
end
|
612
625
|
|
613
626
|
describe 'where' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fat_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel E. Doherty
|
@@ -256,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
256
256
|
version: '0'
|
257
257
|
requirements: []
|
258
258
|
rubyforge_project:
|
259
|
-
rubygems_version: 2.
|
259
|
+
rubygems_version: 2.5.1
|
260
260
|
signing_key:
|
261
261
|
specification_version: 4
|
262
262
|
summary: fat_core provides some useful core extensions
|