class Probe::RowMeetsCondition

Abstract class of a CSV::Row check

Attributes

fail_msg[RW]
ok_condition_fn[RW]
pre_checks[RW]
severity[RW]

Public Class Methods

new(ok_condition_fn, fail_msg) click to toggle source
# File lib/csv/probe/checks.rb, line 40
def initialize(ok_condition_fn, fail_msg)
  @pre_checks = []
  @ok_condition_fn = ok_condition_fn
  @fail_msg = fail_msg
  @severity = "ERROR"
  @error_classes = {
    "ERROR" => RowError,
    "WARNING" => RowWarning
  }
end

Public Instance Methods

error_msg(row, opts) click to toggle source
# File lib/csv/probe/checks.rb, line 62
def error_msg(row, opts)
  row_location = source_row_location(row, opts)
  row_str = source_row(row, opts) # [#{self.class}] verbose only?
  err_msg = "#{row_location} [#{@severity}] #{render_pretty_fail_msg(row, opts)}\n"
  if opts[:verbose]
    err_msg = "#{err_msg}#{self.class.name.split("::").last}\n"
    err_msg = "#{err_msg}#{@error_classes.fetch(@severity).name.split("::").last}\n"
  end
  "#{err_msg}#{row_str}\n"
end
evaluate(row, opts = {}) click to toggle source
# File lib/csv/probe/checks.rb, line 57
def evaluate(row, opts = {})
  evaluate_pre_checks(row, opts)
  raise @error_classes.fetch(@severity), error_msg(row, opts) unless @ok_condition_fn.call(row, opts)
end
evaluate_pre_checks(row, opts) click to toggle source
# File lib/csv/probe/checks.rb, line 51
def evaluate_pre_checks(row, opts)
  @pre_checks.each do |c|
    c.evaluate(row, opts)
  end
end
render_fail_msg(row, opts) click to toggle source
# File lib/csv/probe/checks.rb, line 73
def render_fail_msg(row, opts)
  return @fail_msg.call(row, opts) if @fail_msg.is_a?(Proc)

  @fail_msg
end
render_pretty_fail_msg(row, opts) click to toggle source
# File lib/csv/probe/checks.rb, line 79
def render_pretty_fail_msg(row, opts)
  render_fail_msg(row, opts)
end
report_columns_hash(row, opts) click to toggle source

def row_colval(row, varname, opts)

return row[varname] if varname.is_a? Integer
return row.fetch(varname)

end

# File lib/csv/probe/checks.rb, line 103
def report_columns_hash(row, opts)
  h = {}
  opts[:only_report_columns].each { |varname| h[varname] = row.fetch(varname) }
  h
end
source_row(row, opts = {}) click to toggle source
# File lib/csv/probe/checks.rb, line 87
def source_row(row, opts = {})
  return report_columns_hash(row, opts) if opts[:only_report_columns]

  if opts[:source_row_renderer]
    return row.inspect if opts[:source_row_renderer] == "inspect"
    return "#{Terminal::Table.new rows: row.to_a}\n" if opts[:source_row_renderer] == "tabular"
    return "#{Terminal::Table.new rows: row.to_a.transpose}\n" if opts[:source_row_renderer] == "tabular_transposed"
  end
  row
end
source_row_location(_row, opts) click to toggle source
# File lib/csv/probe/checks.rb, line 83
def source_row_location(_row, opts)
  "#{opts[:fpath]}:#{opts[:lineno]}"
end