Class: Probe::RowMeetsCondition
- Inherits:
-
Object
- Object
- Probe::RowMeetsCondition
- Defined in:
- lib/csv/probe/checks.rb
Overview
Abstract class of a CSV::Row check
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fail_msg ⇒ Object
Returns the value of attribute fail_msg.
-
#ok_condition_fn ⇒ Object
Returns the value of attribute ok_condition_fn.
-
#pre_checks ⇒ Object
Returns the value of attribute pre_checks.
-
#severity ⇒ Object
Returns the value of attribute severity.
Instance Method Summary collapse
- #error_msg(row, opts) ⇒ Object
- #evaluate(row, opts = {}) ⇒ Object
- #evaluate_pre_checks(row, opts) ⇒ Object
-
#initialize(ok_condition_fn, fail_msg) ⇒ RowMeetsCondition
constructor
A new instance of RowMeetsCondition.
- #render_fail_msg(row, opts) ⇒ Object
- #render_pretty_fail_msg(row, opts) ⇒ Object
-
#report_columns_hash(row, opts) ⇒ Object
def row_colval(row, varname, opts) return row if varname.is_a? Integer return row.fetch(varname) end.
- #source_row(row, opts = {}) ⇒ Object
- #source_row_location(_row, opts) ⇒ Object
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_msg ⇒ Object
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_fn ⇒ Object
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_checks ⇒ Object
Returns the value of attribute pre_checks.
40 41 42 |
# File 'lib/csv/probe/checks.rb', line 40 def pre_checks @pre_checks end |
#severity ⇒ Object
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
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 |