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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7c14451e2d5f7edfd5f371cb27d40b0d2660150
4
- data.tar.gz: 4c50850958423f3e2b35a544c89e6621e92984db
3
+ metadata.gz: b584803e27f06145ac112e534d19283bb680c525
4
+ data.tar.gz: 03f00dfaca603a3487db9b5f702aa188510151cb
5
5
  SHA512:
6
- metadata.gz: eb202c94dbe14cdcd58383f3ed56a57864b927c89196418672f3de5111dfc5feba82b5cbb610e150e49b9596f78c2a93243b5f3fc206c53838e3141bf5848fa6
7
- data.tar.gz: 105eef5f77140aaab7775c5d47ad6b92c3ad4beb54cf230ce207993376459559373a85c190364e52c3bb6f9eb643ae3e4072da0e4fc6d3c3642e8b2fab4c29aa
6
+ metadata.gz: 71655e984f1019ce9a7c3ae3fc6f180b317061a892dc37315070aca2c5ffc0765ba35fc4e227924bebf9145fc13b3ebe6a70dfc080a364b6d3327b1071a253b0
7
+ data.tar.gz: 7b7a975597653fa34761a7e075bb8f1f5bdd17d3018c29538bced08b47332972f208d4017d1a990a87e53f674606a61d558194a281e484e0e0838ca3ee5b2cc9
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.2.2
1
+ 2.3.0
@@ -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
- new_heads = []
170
- exps.each do |exp|
171
- case exp
172
- when Symbol, String
173
- h = exp.as_sym
174
- raise "Column '#{h}' in select does not exist" unless column?(h)
175
- new_heads << h
176
- new_cols[h] = Column.new(header: h,
177
- items: column(h).items)
178
- when Hash
179
- exp.each_pair do |key, xp|
180
- case xp
181
- when Symbol
182
- h = xp.as_sym
183
- raise "Column '#{key}' in select does not exist" unless column?(key)
184
- new_heads << h
185
- new_cols[h] = Column.new(header: h,
186
- items: column(key).items)
187
- when String
188
- # Evaluate xp in the context of a binding including a local
189
- # variable for each original column with the name as the head and
190
- # the value for the current row as the value and a local variable
191
- # for each new column with the new name and the new value.
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
- new_row[h] = ev.evaluate(xp, vars: vars)
207
- new_cols[h] << new_row[h]
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
- end
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
@@ -1,7 +1,7 @@
1
1
  module FatCore
2
2
  MAJOR = 1
3
3
  MINOR = 2
4
- PATCH = 7
4
+ PATCH = 8
5
5
 
6
6
  VERSION = [MAJOR, MINOR, PATCH].compact.join('.')
7
7
  end
@@ -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(s: :former_s, a: :new_a, c: :renew_c)
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.7
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.4.5
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