Skip to content

Commit

Permalink
Merge pull request #173 from airblade/master
Browse files Browse the repository at this point in the history
Use actual regular expressions for header "regex"s.
  • Loading branch information
simonoff committed Feb 11, 2015
2 parents 3a370f8 + 29332b3 commit 707ed15
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,14 @@ xls.each(:id => 'UPC',:qty => 'ATS') {|hash| arr << hash}

# NOTE: .parse does the same as .each, except it returns an array (similar to each vs. map)

# not sure exactly what a column will be named? try a wildcard search with the character *
# regex characters are allowed ('^price\s')
# case insensitive
# not sure exactly what a column will be named? try a wildcard search with a regex

xls.parse(:id => 'UPC*SKU',:qty => 'ATS*\sATP\s*QTY$')
xls.parse(:id => /UPC|SKU/,:qty => /ATS*\sATP\s*QTY\z/)

# if you need to locate the header row and assign the header names themselves,
# use the :header_search option

xls.parse(:header_search => ['UPC*SKU','ATS*\sATP\s*QTY$'])
xls.parse(:header_search => [/UPC*SKU/,/ATS*\sATP\s*QTY\z/])
#=> each element will appear in this fashion:
#=> {"UPC" => 123456789012, "STYLE" => "987B0", "COLOR" => "blue", "QTY" => 78}

Expand Down
15 changes: 1 addition & 14 deletions lib/roo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,23 +392,10 @@ def parse(options = {})
end

def row_with(query, return_headers = false)
query.map! { |x| Array(x.split('*')) }
line_no = 0
each do |row|
line_no += 1
# makes sure headers is the first part of wildcard search for priority
# ex. if UPC and SKU exist for UPC*SKU search, UPC takes the cake
headers = query.map do |q|
q.map do |i|
#if header has brackets in it for ex: date(yyyy-mm-dd),
#can add other special characterss to this list.
if i.include?('(')
row.grep(i)[0]
else
row.grep(/#{i}/i)[0]
end
end.compact[0]
end.compact
headers = query.map { |q| row.grep(q)[0] }.compact

if headers.length == query.length
@header_line = line_no
Expand Down

0 comments on commit 707ed15

Please sign in to comment.