Class: Probe::ColumnIsSet

Inherits:
ColumnMeetsCondition show all
Defined in:
lib/csv/probe/checks.rb

Overview

Check if a tokenized column value is a list of given values

Instance Attribute Summary

Attributes inherited from ColumnMeetsCondition

#fail_msg, #ok_condition_fn, #varname

Attributes inherited from RowMeetsCondition

#fail_msg, #ok_condition_fn, #pre_checks, #severity

Instance Method Summary collapse

Methods inherited from ColumnMeetsCondition

#evaluate, #render_pretty_fail_msg

Methods inherited from RowMeetsCondition

#error_msg, #evaluate, #evaluate_pre_checks, #render_fail_msg, #render_pretty_fail_msg, #report_columns_hash, #source_row, #source_row_location

Constructor Details

#initialize(varname, separator, _placeholder = nil) ⇒ ColumnIsSet

rubocop:disable Metrics/MethodLength



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/csv/probe/checks.rb', line 226

def initialize(varname, separator, _placeholder = nil) # rubocop:disable Metrics/MethodLength
  super(varname, nil, nil)
  @ok_condition_fn = lambda { |val, _cfg|
    return true if val.to_s == ""

    items = val.to_s.split(separator, -1)
    all_uniq = (items.size == items.uniq.size)
    return all_uniq
  }
  @fail_msg = lambda { |row, _opts|
    items = row.fetch(@varname).to_s.split(separator, -1)
    non_uniq_items = items.detect { |e| items.count(e) > 1 }
    "expected that items of tokenized value #{items.inspect} are uniqe, but items #{non_uniq_items.inspect} are not"
  }
end