Class: ExcelWalker::Reader::Reader

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

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Reader

Returns a new instance of Reader.



4
5
6
7
8
9
10
# File 'lib/excel_walker/reader/reader.rb', line 4

def initialize(file_path)
  @xl = Creek::Book.new(file_path)
  @hooks = {}
  @max_rows = {}
  @current_sheet = 1
  @max_sheets = 1
end

Instance Method Details

#exitObject

Raises:



58
59
60
# File 'lib/excel_walker/reader/reader.rb', line 58

def exit
  raise StopIteration.new
end

#for_sheet(sheet_num) ⇒ Object Also known as: set_sheet



12
13
14
15
16
# File 'lib/excel_walker/reader/reader.rb', line 12

def for_sheet(sheet_num)
  @current_sheet = sheet_num
  @max_sheets = sheet_num if sheet_num > @max_sheets
  self
end

#hooksObject



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

def hooks
  @hooks[@current_sheet]
end

#max_rows(max) ⇒ Object



20
21
22
23
# File 'lib/excel_walker/reader/reader.rb', line 20

def max_rows(max)
  @max_rows[@current_sheet] = max
  self
end

#on_row(condition = nil, &block) ⇒ Object Also known as: on_rows



25
26
27
28
29
30
31
# File 'lib/excel_walker/reader/reader.rb', line 25

def on_row(condition = nil, &block)
  condition = block if block_given?
  Hook.new(condition).tap do |hook|
    @hooks[@current_sheet] ||= []
    @hooks[@current_sheet] << hook
  end
end

#startObject Also known as: walk



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/excel_walker/reader/reader.rb', line 39

def start
  sheet_num = 0
  sheets_done = []
  begin
    @xl.sheets.each do |sheet|
      sheet_num += 1
      break if sheet_num > @max_sheets
      process_rows(sheet, sheet_num)
      sheets_done << sheet.name
    end
  rescue StopIteration
  end
  sheets_done
ensure
  @xl.close
end