From 29332b3984438a6dcaf90a62b6cc5598102b4f0f Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Wed, 11 Feb 2015 10:33:59 +0100 Subject: [PATCH] Use actual regular expressions for header "regex"s. --- README.md | 8 +++----- lib/roo/base.rb | 15 +-------------- 2 files changed, 4 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 88876f4b..735e4aaa 100644 --- a/README.md +++ b/README.md @@ -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} diff --git a/lib/roo/base.rb b/lib/roo/base.rb index c608578a..b851dce8 100644 --- a/lib/roo/base.rb +++ b/lib/roo/base.rb @@ -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