Class: Remi::BusinessRules::DataSubject

Inherits:
Object
  • Object
show all
Defined in:
lib/remi/cucumber/business_rules.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, subject) ⇒ DataSubject

Returns a new instance of DataSubject.



251
252
253
254
255
256
257
# File 'lib/remi/cucumber/business_rules.rb', line 251

def initialize(name, subject)
  @name = name
  @data_obj = subject
  @fields = DataFieldCollection.new

  stub_data
end

Instance Attribute Details

#data_objObject (readonly)

Returns the value of attribute data_obj.



260
261
262
# File 'lib/remi/cucumber/business_rules.rb', line 260

def data_obj
  @data_obj
end

#nameObject (readonly)

Returns the value of attribute name.



259
260
261
# File 'lib/remi/cucumber/business_rules.rb', line 259

def name
  @name
end

Instance Method Details

#_dfObject

For debugging only



292
293
294
# File 'lib/remi/cucumber/business_rules.rb', line 292

def _df
  @data_obj.df
end

#add_field(field_name) ⇒ Object



262
263
264
# File 'lib/remi/cucumber/business_rules.rb', line 262

def add_field(field_name)
  @fields.add_field(self, field_name)
end

#append_data_with(example) ⇒ Object



339
340
341
# File 'lib/remi/cucumber/business_rules.rb', line 339

def append_data_with(example)
  @data_obj.df = @data_obj.df.concat example_to_df(example)
end

#column_hashObject

Public: Converts the data subject to a hash where the keys are the table columns and the values are an array for the value of column for each row.



284
285
286
287
288
289
# File 'lib/remi/cucumber/business_rules.rb', line 284

def column_hash
  @data_obj.df.to_hash.reduce({}) do |h, (k,v)|
    h[k.symbolize] = v.to_a
    h
  end
end

#csv_optionsObject



404
405
406
# File 'lib/remi/cucumber/business_rules.rb', line 404

def csv_options
  @data_obj.csv_options
end

#cumulative_dist_from_freq_table(table, freq_field: 'frequency') ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
# File 'lib/remi/cucumber/business_rules.rb', line 352

def cumulative_dist_from_freq_table(table, freq_field: 'frequency')
  cumulative_dist = {}
  freq_total = 0
  table.hashes.each do |row|
    low = freq_total
    high = freq_total + row[freq_field].to_f
    freq_total = high
    cumulative_dist[(low...high)] =   row.tap { |r| r.delete(freq_field) }
  end
  cumulative_dist
end

#distribute_values(table) ⇒ Object



379
380
381
382
383
384
385
386
387
# File 'lib/remi/cucumber/business_rules.rb', line 379

def distribute_values(table)
  cumulative_dist = cumulative_dist_from_freq_table(table)
  generated_data = generate_values_from_cumulative_dist(@data_obj.df.size, cumulative_dist)

  generated_data.each do |field_name, data_array|
    vector_name = fields[field_name].field_name
    @data_obj.df[vector_name] = Daru::Vector.new(data_array, index: @data_obj.df.index)
  end
end

#example_to_df(example) ⇒ Object



330
331
332
# File 'lib/remi/cucumber/business_rules.rb', line 330

def example_to_df(example)
  example.to_df(@data_obj.df.row[0].to_hash, field_symbolizer: @data_obj.field_symbolizer)
end

#extractObject



400
401
402
# File 'lib/remi/cucumber/business_rules.rb', line 400

def extract
  @data_obj.extractor.extract
end

#fieldObject



266
267
268
# File 'lib/remi/cucumber/business_rules.rb', line 266

def field
  @fields.only
end

#fieldsObject



270
271
272
# File 'lib/remi/cucumber/business_rules.rb', line 270

def fields
  @fields
end

#freq_by(*field_names) ⇒ Object



389
390
391
# File 'lib/remi/cucumber/business_rules.rb', line 389

def freq_by(*field_names)
  @data_obj.df.group_by(field_names).size * 1.0 / @data_obj.df.size
end

#generate_values_from_cumulative_dist(n_records, cumulative_dist) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/remi/cucumber/business_rules.rb', line 364

def generate_values_from_cumulative_dist(n_records, cumulative_dist)
  # Use the same key for reproducible tests
  psuedorand = Random.new(3856382695386)

  1.upto(n_records).reduce({}) do |h, idx|
    r = psuedorand.rand
    row_as_hash = cumulative_dist.select { |range| range.include? r }.values.first
    row_as_hash.each do |field_name, value|
      h[field_name] ||= []
      h[field_name] << value
    end
    h
  end
end

#get_attrib(name) ⇒ Object



278
279
280
# File 'lib/remi/cucumber/business_rules.rb', line 278

def get_attrib(name)
  @data_obj.send(name)
end

#mock_extractor(filestore) ⇒ Object



393
394
395
396
397
398
# File 'lib/remi/cucumber/business_rules.rb', line 393

def mock_extractor(filestore)
  extractor = class << @data_obj.extractor; self; end

  extractor.send(:define_method, :all_entries, ->() { filestore.sftp_entries })
  extractor.send(:define_method, :download, ->(to_download) { to_download.map { |e| e.name } })
end

#replicate_rows(n_rows) ⇒ Object



344
345
346
347
348
349
350
# File 'lib/remi/cucumber/business_rules.rb', line 344

def replicate_rows(n_rows)
  replicated_df = Daru::DataFrame.new([], order: @data_obj.df.vectors.to_a)
  @data_obj.df.each do |vector|
    replicated_df[vector.name] = vector.to_a * n_rows
  end
  @data_obj.df = replicated_df
end

#sizeObject



274
275
276
# File 'lib/remi/cucumber/business_rules.rb', line 274

def size
  @data_obj.df.size
end

#stub_dataObject



326
327
328
# File 'lib/remi/cucumber/business_rules.rb', line 326

def stub_data
  @data_obj.stub_df if @data_obj.respond_to? :stub_df
end

#stub_data_with(example) ⇒ Object



334
335
336
337
# File 'lib/remi/cucumber/business_rules.rb', line 334

def stub_data_with(example)
  stub_data
  @data_obj.df = example_to_df(example)
end

#where(field_name, operation) ⇒ Object

Would like to have this return a new DataSubject and not a dataframe. Need more robust duping to make that feasible. Don’t use results for anything more than size.



300
301
302
# File 'lib/remi/cucumber/business_rules.rb', line 300

def where(field_name, operation)
  @data_obj.df.where(@data_obj.df[field_name.symbolize(@data_obj.field_symbolizer)].recode { |v| operation.call(v) })
end

#where_between(field_name, low_value, high_value) ⇒ Object



316
317
318
# File 'lib/remi/cucumber/business_rules.rb', line 316

def where_between(field_name, low_value, high_value)
  where(field_name, ->(v) { v.to_f.between?(low_value.to_f, high_value.to_f) })
end

#where_gt(field_name, value) ⇒ Object



312
313
314
# File 'lib/remi/cucumber/business_rules.rb', line 312

def where_gt(field_name, value)
  where(field_name, ->(v) { v.to_f > value.to_f })
end

#where_in(field_name, list) ⇒ Object



320
321
322
323
# File 'lib/remi/cucumber/business_rules.rb', line 320

def where_in(field_name, list)
  list_array = list.split(',').map { |v| v.strip }
  where(field_name, ->(v) { list_array.include?(v) })
end

#where_is(field_name, value) ⇒ Object



304
305
306
# File 'lib/remi/cucumber/business_rules.rb', line 304

def where_is(field_name, value)
  where(field_name, ->(v) { v == value })
end

#where_lt(field_name, value) ⇒ Object



308
309
310
# File 'lib/remi/cucumber/business_rules.rb', line 308

def where_lt(field_name, value)
  where(field_name, ->(v) { v.to_f < value.to_f })
end