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.



231
232
233
234
235
236
237
# File 'lib/remi/cucumber/business_rules.rb', line 231

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.



240
241
242
# File 'lib/remi/cucumber/business_rules.rb', line 240

def data_obj
  @data_obj
end

#nameObject (readonly)

Returns the value of attribute name.



239
240
241
# File 'lib/remi/cucumber/business_rules.rb', line 239

def name
  @name
end

Instance Method Details

#_dfObject

For debugging only



272
273
274
# File 'lib/remi/cucumber/business_rules.rb', line 272

def _df
  @data_obj.df
end

#add_field(field_name) ⇒ Object



242
243
244
# File 'lib/remi/cucumber/business_rules.rb', line 242

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

#append_data_with(example) ⇒ Object



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

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.



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

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

#csv_optionsObject



384
385
386
# File 'lib/remi/cucumber/business_rules.rb', line 384

def csv_options
  @data_obj.csv_options
end

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



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/remi/cucumber/business_rules.rb', line 332

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



359
360
361
362
363
364
365
366
367
# File 'lib/remi/cucumber/business_rules.rb', line 359

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



310
311
312
# File 'lib/remi/cucumber/business_rules.rb', line 310

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

#extractObject



380
381
382
# File 'lib/remi/cucumber/business_rules.rb', line 380

def extract
  @data_obj.extractor.extract
end

#fieldObject



246
247
248
# File 'lib/remi/cucumber/business_rules.rb', line 246

def field
  @fields.only
end

#fieldsObject



250
251
252
# File 'lib/remi/cucumber/business_rules.rb', line 250

def fields
  @fields
end

#freq_by(*field_names) ⇒ Object



369
370
371
# File 'lib/remi/cucumber/business_rules.rb', line 369

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/remi/cucumber/business_rules.rb', line 344

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



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

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

#mock_extractor(filestore) ⇒ Object



373
374
375
376
377
378
# File 'lib/remi/cucumber/business_rules.rb', line 373

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



324
325
326
327
328
329
330
# File 'lib/remi/cucumber/business_rules.rb', line 324

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



254
255
256
# File 'lib/remi/cucumber/business_rules.rb', line 254

def size
  @data_obj.df.size
end

#stub_dataObject



306
307
308
# File 'lib/remi/cucumber/business_rules.rb', line 306

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

#stub_data_with(example) ⇒ Object



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

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.



280
281
282
# File 'lib/remi/cucumber/business_rules.rb', line 280

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



296
297
298
# File 'lib/remi/cucumber/business_rules.rb', line 296

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



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

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

#where_in(field_name, list) ⇒ Object



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

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



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

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

#where_lt(field_name, value) ⇒ Object



288
289
290
# File 'lib/remi/cucumber/business_rules.rb', line 288

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