Skip to content

Commit

Permalink
Merge a21aca7 into ecfb74f
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Bumm committed Sep 28, 2018
2 parents ecfb74f + a21aca7 commit a90304c
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
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
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 a90304c

Please sign in to comment.