Module: CsvChecker
- Defined in:
- lib/csvchecker.rb
Constant Summary collapse
- DEFAULT_DATE_FORMAT =
"%d/%m/%Y"
Class Method Summary collapse
- .check(input, output, mappings = {}, csv_options = {}, skip_first = false) ⇒ Object
- .check_row(row_num, row, mappings) ⇒ Object
- .dateFormatFrom(str) ⇒ Object
- .is_valid(cell, type) ⇒ Object
Class Method Details
.check(input, output, mappings = {}, csv_options = {}, skip_first = false) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/csvchecker.rb', line 9 def check input, output, mappings={}, ={}, skip_first=false line_no = 1 lines_scanned = 0 num_errors = 0 i = 0 FasterCSV.new(input, ).each do |row| if skip_first then line_no = line_no + 1 skip_first = false next end lines_scanned = lines_scanned + 1 num_errors = num_errors + check_row(i, row, mappings) unless row.empty? i = i + 1 end print "Total number of lines checked: #{lines_scanned}\n" print "Found #{num_errors} errors" print "\n" if num_errors > 0 then exit 1 else exit 0 end end |
.check_row(row_num, row, mappings) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/csvchecker.rb', line 40 def check_row row_num, row, mappings raise "Nil mappings" if mappings.nil? raise "Nil row" if row.nil? errors = 0 i = 0 row.each { |item| type = mappings[i.to_s] if type then puts "Checking [#{item}] against [#{type}]\n" valid = is_valid item, type if !valid then print "Error at row #{row_num} column #{i}\n" errors = errors + 1 end end i = i + 1 } return errors end |
.dateFormatFrom(str) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/csvchecker.rb', line 94 def dateFormatFrom str return DEFAULT_DATE_FORMAT if str == "date" puts "[#{str}]\n" if str.match /^date.*/ then type = str[ /'(.*)'/ , 1 ] return type end return DEFAULT_DATE_FORMAT end |
.is_valid(cell, type) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/csvchecker.rb', line 65 def is_valid cell, type raise 'Nil type' unless type raise 'Empty type' unless type.size > 0 type_selector = type.downcase type_selector = "date" if type.match /^date/ case type_selector when 'integer' return TypeChecker.new.is_integer?(cell) when 'float' return TypeChecker.new.is_float?(cell) when 'string' return TypeChecker.new.is_string?(cell) when 'date' format = dateFormatFrom(type) return TypeChecker.new.is_date?(cell, format) when 'any' return TypeChecker.new.is_any?(cell) else raise "Unrecognised column type [#{type_selector}]" end end |