Skip to content

Commit

Permalink
return missing headers when raising HeaderRowNotFoundError (#461)
Browse files Browse the repository at this point in the history
return missing headers when raising HeaderRowNotFoundError
  • Loading branch information
Felix Bumm authored and chopraanmol1 committed Sep 28, 2018
1 parent ecfb74f commit 8d5ea7a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/roo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,22 @@ def parse(options = {})

def row_with(query, return_headers = false)
line_no = 0
closest_mismatched_headers = []
each do |row|
line_no += 1
headers = query.map { |q| row.grep(q)[0] }.compact

if headers.length == query.length
@header_line = line_no
return return_headers ? headers : line_no
elsif line_no > 100
raise Roo::HeaderRowNotFoundError
else
closest_mismatched_headers = headers if headers.length > closest_mismatched_headers.length
if line_no > 100
break
end
end
end
raise Roo::HeaderRowNotFoundError
missing_headers = query.select { |q| closest_mismatched_headers.grep(q).empty? }
raise Roo::HeaderRowNotFoundError, missing_headers
end

protected
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/roo/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ def sheets
expect { spreadsheet.row_with([/Missing Header/]) }.to \
raise_error(Roo::HeaderRowNotFoundError)
end

it 'returns missing headers' do
expect { spreadsheet.row_with([/Header/, /Missing Header 1/, /Missing Header 2/]) }.to \
raise_error(Roo::HeaderRowNotFoundError, '[/Missing Header 1/, /Missing Header 2/]')
end
end
end

Expand Down

0 comments on commit 8d5ea7a

Please sign in to comment.