Class: ExcelWalker::Reader::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/excel_walker/reader/hook.rb

Instance Method Summary collapse

Constructor Details

#initialize(condition) ⇒ Hook

Returns a new instance of Hook.



5
6
7
8
9
10
11
12
13
14
# File 'lib/excel_walker/reader/hook.rb', line 5

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

Instance Method Details

#call(row, row_num, sheet, sheet_num) ⇒ Object



43
44
45
46
# File 'lib/excel_walker/reader/hook.rb', line 43

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

#columns(cols_condition = nil, &block) ⇒ Object Also known as: pluck_columns



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/excel_walker/reader/hook.rb', line 16

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

#match?(row_num, sheet_num) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/excel_walker/reader/hook.rb', line 39

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

#run(&block) ⇒ Object



35
36
37
# File 'lib/excel_walker/reader/hook.rb', line 35

def run(&block)
  @run_block = block
end