Class: Probe::ColumnIsSetWithDomain

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

Overview

Check if a tokenized column value is a set of given values (no duplicates)

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, expected_items_arr, separator, _placeholder = nil) ⇒ ColumnIsSetWithDomain

rubocop:disable Metrics/MethodLength



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/csv/probe/checks.rb', line 245

def initialize(varname, expected_items_arr, separator, _placeholder = nil) # rubocop:disable Metrics/MethodLength
  super(varname, nil, nil)
  @pre_checks << ColumnIsSet.new(varname, separator)
  @ok_condition_fn = lambda { |val, _cfg|
    expected_items_arr.map!(&:to_s) # turn nil -> ""

    items = val.to_s.split(separator, -1)
    return true if items.empty? && expected_items_arr.include?(nil.to_s) # empty str allowed

    return false if items.empty?

    all_valid = items.all? { |i| expected_items_arr.include?(i) }
    return all_valid
  }
  @fail_msg = lambda { |row, _opts|
    items = row.fetch(@varname).to_s.split(separator, -1)
    "expected that items of tokenized value #{items.inspect} are a subset of #{expected_items_arr.inspect}"
  }
  # @fail_msg = lambda {|row, opts|
  # "Unexpected value:#{row.fetch(@varname).inspect} for column:#{@varname.inspect},
  #  expected that items of tokenized value #{row.fetch(@varname).split(separator)}
  #  are uniqe and a subset of #{expected_items_arr.inspect}" }
end