Skip to content

Commit 326da96

Browse files
author
Adam Gardiner
committed
Diff should compare missing fields by default
1 parent 32990e2 commit 326da96

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/csv-diff/csv_diff.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def initialize(left, right, options = {})
8686
@right = right.is_a?(CSVSource) ? right : CSVSource.new(right, options)
8787
raise "No field names found in right (to) source" unless @right.field_names && @right.field_names.size > 0
8888
@warnings = []
89-
@diff_fields = get_diff_fields(@left.field_names, @right.field_names, options[:ignore_fields])
89+
@diff_fields = get_diff_fields(@left.field_names, @right.field_names, options)
9090
@key_fields = @left.key_fields.map{ |kf| @diff_fields[kf] }
9191
diff(options)
9292
end
@@ -137,15 +137,21 @@ def diff_warnings
137137

138138
# Given two sets of field names, determines the common set of fields present
139139
# in both, on which members can be diffed.
140-
def get_diff_fields(left_fields, right_fields, ignore_fields)
140+
def get_diff_fields(left_fields, right_fields, options)
141+
ignore_fields = (options[:ignore_fields] || []).map do |f|
142+
f.is_a?(Fixnum) ? right_fields[f] : f
143+
end
141144
diff_fields = []
142-
right_fields.each_with_index do |fld, i|
143-
if left_fields.include?(fld)
144-
diff_fields << fld unless ignore_fields && (ignore_fields.include?(fld) ||
145-
ignore_fields.include?(i))
146-
else
147-
@warnings << "Field '#{fld}' is missing from the left (from) file, and won't be diffed"
145+
if options[:diff_common_fields_only]
146+
right_fields.each_with_index do |fld, i|
147+
if left_fields.include?(fld)
148+
diff_fields << fld unless ignore_fields.include?(fld)
149+
else
150+
@warnings << "Field '#{fld}' is missing from the left (from) file, and won't be diffed"
151+
end
148152
end
153+
else
154+
diff_fields = (right_fields + left_fields).uniq.reject{ |fld| ignore_fields.include?(fld) }
149155
end
150156
diff_fields
151157
end

0 commit comments

Comments
 (0)