lib/csv2hash/discover.rb in csv2hash-0.6.8 vs lib/csv2hash/discover.rb in csv2hash-0.7.0

- old
+ new

@@ -1,22 +1,42 @@ module Csv2hash module Discover - def find_dynamic_position cell - y, x = cell.rules.fetch :position - column, matcher = y - dynamic_y_axe = data_source.index { |entries| entries[column] =~ matcher } + def find_dynamic_position cell, header = nil + header.present? ? find_dynamic_position_x(cell, header) : find_dynamic_position_y(cell) + end - if dynamic_y_axe.nil? - if cell.rules.fetch(:allow_blank) - return nil + private + def find_dynamic_position_y cell + y, x = cell.rules.fetch :position + column, matcher = y + dynamic_y_axe = data_source.index { |entries| entries[column] =~ matcher } + + if dynamic_y_axe.nil? + if cell.rules.fetch(:allow_blank) + return nil + else + raise "Y doesn't found for #{cell.rules[:position]} on :#{cell.rules.fetch(:key)}" + end else - raise "Y doesn't found for #{cell.rules[:position]} on :#{cell.rules.fetch(:key)}" + cell.rules[:position] = [dynamic_y_axe, x] + cell end - else - cell.rules[:position] = [dynamic_y_axe, x] - cell end - end + def find_dynamic_position_x cell, header + x = cell.rules.fetch :position + dynamic_x_axe = header.index { |column| column =~ x } + + if dynamic_x_axe.nil? + if cell.rules.fetch(:allow_blank) + return nil + else + raise "Column doesn't found in #{definition.name}" + end + else + cell.rules[:position] = dynamic_x_axe + cell + end + end end end