Class: Probe::RowMeetsCondition

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

Overview

Abstract class of a CSV::Row check

Direct Known Subclasses

ColumnMeetsCondition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ok_condition_fn, fail_msg) ⇒ RowMeetsCondition



42
43
44
45
46
47
48
49
50
51
# File 'lib/csv/probe/checks.rb', line 42

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

Instance Attribute Details

#fail_msgObject

Returns the value of attribute fail_msg.



40
41
42
# File 'lib/csv/probe/checks.rb', line 40

def fail_msg
  @fail_msg
end

#ok_condition_fnObject

Returns the value of attribute ok_condition_fn.



40
41
42
# File 'lib/csv/probe/checks.rb', line 40

def ok_condition_fn
  @ok_condition_fn
end

#pre_checksObject

Returns the value of attribute pre_checks.



40
41
42
# File 'lib/csv/probe/checks.rb', line 40

def pre_checks
  @pre_checks
end

#severityObject

Returns the value of attribute severity.



40
41
42
# File 'lib/csv/probe/checks.rb', line 40

def severity
  @severity
end

Instance Method Details

#error_msg(row, opts) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/csv/probe/checks.rb', line 64

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 = {}) ⇒ Object

Raises:



59
60
61
62
# File 'lib/csv/probe/checks.rb', line 59

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) ⇒ Object



53
54
55
56
57
# File 'lib/csv/probe/checks.rb', line 53

def evaluate_pre_checks(row, opts)
  @pre_checks.each do |c|
    c.evaluate(row, opts)
  end
end

#render_fail_msg(row, opts) ⇒ Object



75
76
77
78
79
# File 'lib/csv/probe/checks.rb', line 75

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) ⇒ Object



81
82
83
# File 'lib/csv/probe/checks.rb', line 81

def render_pretty_fail_msg(row, opts)
  render_fail_msg(row, opts)
end

#report_columns_hash(row, opts) ⇒ Object

def row_colval(row, varname, opts)

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

end



105
106
107
108
109
# File 'lib/csv/probe/checks.rb', line 105

def report_columns_hash(row, opts)
  h = {}
  opts[:only_report_columns].each { |varname| h[varname] = row.fetch(varname) }
  h
end

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



89
90
91
92
93
94
95
96
97
98
# File 'lib/csv/probe/checks.rb', line 89

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) ⇒ Object



85
86
87
# File 'lib/csv/probe/checks.rb', line 85

def source_row_location(_row, opts)
  "#{opts[:fpath]}:#{opts[:lineno]}"
end