Sha256: 86cb6d152d27344356fba4e8c663460a9fdb58bb6751cefb8917888a74668aa0

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

require 'set'
module ExcelWalker
  module Reader
    class Hook
      def initialize(condition)
        @matcher = case true
                     when condition.respond_to?(:call)
                       condition
                     when condition.is_a?(Array), condition.is_a?(Range)
                       proc { |row_num| condition.include?(row_num) }
                     when condition.is_a?(Fixnum)
                       proc { |row_num| condition === row_num }
                   end
      end

      def columns(cols_condition = nil, &block)
        cols_condition = block if block_given?
        @cols_extractor =
            case cols_condition.class.name
              when 'Array'
                cols_set = Set.new(cols_condition)
                proc { |row| row.values.select.with_index { |_, idx| cols_set.include?(idx + 1) } }
              when 'Fixnum'
                proc { |row| row.values[cols_condition - 1] }
              when 'Range'
                proc { |row| row.values[(cols_condition.min - 1)..(cols_condition.max - 1)] }
              when 'Proc'
                proc { |row| cols_condition[row.values] }
            end
        self
      end

      alias pluck_columns columns

      def run(&block)
        @run_block = block
      end

      def match?(row_num, sheet_num)
        @matcher[row_num, sheet_num]
      end

      def call(row, row_num, sheet, sheet_num)
        data = extract_columns(row, row_num, sheet, sheet_num)
        @run_block[data, row_num, sheet, sheet_num]
      end

      protected

      def extract_columns(*args)
        @cols_extractor[*args]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
excel_walker-0.1.1 lib/excel_walker/reader/hook.rb
excel_walker-0.1.0 lib/excel_walker/reader/hook.rb
excel_walker-0.0.1 lib/excel_walker/reader/hook.rb