Class: ExcelWalker::Reader::Hook
- Inherits:
-
Object
- Object
- ExcelWalker::Reader::Hook
- Defined in:
- lib/excel_walker/reader/hook.rb
Instance Method Summary collapse
- #call(row, row_num, sheet, sheet_num) ⇒ Object
- #columns(cols_matcher = nil, &block) ⇒ Object (also: #pluck_columns)
-
#initialize(condition) ⇒ Hook
constructor
A new instance of Hook.
- #match?(row_num, sheet_num = nil) ⇒ Boolean
- #run(&block) ⇒ Object
Constructor Details
#initialize(condition) ⇒ Hook
Returns a new instance of Hook.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/excel_walker/reader/hook.rb', line 5 def initialize(condition) @matcher = case true when condition.is_a?(Proc) 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 } else raise ArgumentError.new('Can only take Array, Number, Range or a Block') end end |
Instance Method Details
#call(row, row_num, sheet, sheet_num) ⇒ Object
50 51 52 53 |
# File 'lib/excel_walker/reader/hook.rb', line 50 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_matcher = nil, &block) ⇒ Object Also known as: pluck_columns
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/excel_walker/reader/hook.rb', line 18 def columns(cols_matcher = nil, &block) cols_matcher = block if block_given? @cols_extractor = case true when cols_matcher.is_a?(Array) proc { |row| cols_matcher.collect{|idx| row[idx - 1]} } when cols_matcher.is_a?(Fixnum) proc { |row| row[cols_matcher - 1] } when cols_matcher.is_a?(Range) proc { |row| row[(cols_matcher.min - 1)..(cols_matcher.max - 1)] } when cols_matcher.is_a?(Proc) proc { |row| cols_matcher[row] } when cols_matcher.is_a?(Hash) cols_idxs = cols_matcher.values cols_names = cols_matcher.keys proc { |row| Hash[cols_names.zip(cols_idxs.collect{|idx| row[idx - 1]})] } else raise ArgumentError.new('Can only take Array, Number, Range or a Block') end self end |
#match?(row_num, sheet_num = nil) ⇒ Boolean
46 47 48 |
# File 'lib/excel_walker/reader/hook.rb', line 46 def match?(row_num, sheet_num = nil) @matcher[row_num, sheet_num] end |
#run(&block) ⇒ Object
42 43 44 |
# File 'lib/excel_walker/reader/hook.rb', line 42 def run(&block) @run_block = block end |