Class: Probe::ColumnMeetsCondition

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

Overview

Abstract class of a CSV::Row single field (= column) check

Instance Attribute Summary collapse

Attributes inherited from RowMeetsCondition

#pre_checks, #severity

Instance Method Summary collapse

Methods inherited from RowMeetsCondition

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

Constructor Details

#initialize(varname, ok_condition_fn, fail_msg) ⇒ ColumnMeetsCondition

Returns a new instance of ColumnMeetsCondition.



116
117
118
119
120
121
122
123
# File 'lib/csv/probe/checks.rb', line 116

def initialize(varname, ok_condition_fn, fail_msg)
  super(ok_condition_fn, fail_msg)
  @varname = varname
  @error_classes = {
    "ERROR" => ColumnError,
    "WARNING" => ColumnWarning
  }
end

Instance Attribute Details

#fail_msgObject

Returns the value of attribute fail_msg.



114
115
116
# File 'lib/csv/probe/checks.rb', line 114

def fail_msg
  @fail_msg
end

#ok_condition_fnObject

Returns the value of attribute ok_condition_fn.



114
115
116
# File 'lib/csv/probe/checks.rb', line 114

def ok_condition_fn
  @ok_condition_fn
end

#varnameObject

Returns the value of attribute varname.



114
115
116
# File 'lib/csv/probe/checks.rb', line 114

def varname
  @varname
end

Instance Method Details

#evaluate(row, opts = {}) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/csv/probe/checks.rb', line 129

def evaluate(row, opts = {})
  evaluate_pre_checks(row, opts)

  unless @ok_condition_fn.call(row.fetch(@varname), opts) # rubocop:disable Style/GuardClause
    raise @error_classes.fetch(@severity), error_msg(row, opts)
  end
end

#render_pretty_fail_msg(row, opts) ⇒ Object



125
126
127
# File 'lib/csv/probe/checks.rb', line 125

def render_pretty_fail_msg(row, opts)
  "Unexpected value:#{row.fetch(@varname).inspect} for column:#{@varname.inspect}, #{render_fail_msg(row, opts)}"
end