Skip to content

Commit af615ea

Browse files
author
Rory Gibson
committed
wiring up (tests failing right now)
1 parent 403c7fc commit af615ea

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

bin/csv-check

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ module CsvCheckRunner
7575
IN = StringIO.new($stdin.read)
7676
end
7777

78-
typeMappings = new TypeMapper.to_map(options.mappings) if options.mappings
78+
if options.mappings then
79+
typeMappings = TypeMapper.new.to_map(options.mappings)
80+
else
81+
typeMappingd = {}
82+
end
7983

8084
# run the command
8185
CsvChecker.check(IN, OUT, typeMappings, {:col_sep => options.separator}, options.skip_first_row)

lib/csvchecker.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ def check input, output, mappings={}, csv_options={}, skip_first=false
2424
end
2525

2626
lines_scanned = lines_scanned + 1
27+
#print "line #{line_no}\n"
2728

28-
check_against_mapping(row, mappings)
29+
check_against_mapping(row, mappings) unless row.empty?
2930
end
3031

3132
print "Total number of lines checked: #{lines_scanned}\n"
@@ -34,22 +35,29 @@ def check input, output, mappings={}, csv_options={}, skip_first=false
3435

3536

3637
def check_against_mapping row, mappings
37-
row.each_index { |i|
38-
if mappings[i] then
39-
print "Checking col #{i}\n"
38+
return if mappings.nil? || row.nil?
39+
40+
i = 0
41+
row.each { |item|
42+
type = mappings[i.to_s]
43+
44+
if type then
45+
print "Checking col #{i} against type #{type}\n"
46+
raise "Error at col #{i}" unless analyse_column item, type
4047
end
41-
}
48+
49+
i = i + 1
50+
}
4251
end
4352

4453

45-
def analyse_column row, index, type
46-
raise 'Nil row' unless row
47-
raise 'Empty row' unless row.size > 0
48-
raise 'Nil index' unless index
49-
raise 'Negative index' unless index >= 0
54+
def analyse_column cell, type
55+
raise 'Nil cell' unless cell
56+
raise 'Empty cell' unless cell.size > 0
57+
raise 'Nil type' unless type
58+
raise 'Empty type' unless type.size > 0
5059

5160
type.downcase!
52-
cell = row[index]
5361

5462
case type
5563
when 'integer'

0 commit comments

Comments
 (0)