Class: SpreadsheetStreamReader::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/spreadsheet_stream_reader/sheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_sheet, batch_size = 1000) ⇒ Sheet

Returns a new instance of Sheet.



6
7
8
9
10
11
12
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 6

def initialize(_sheet, batch_size = 1000)
  self.batch_size = batch_size
  self.offset = 1
  self.counter = 0
  self.data = Array.new
  self.w_sheet = _sheet
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



4
5
6
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 4

def batch_size
  @batch_size
end

#counterObject

Returns the value of attribute counter.



4
5
6
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 4

def counter
  @counter
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 4

def data
  @data
end

#offsetObject

Returns the value of attribute offset.



4
5
6
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 4

def offset
  @offset
end

#w_sheetObject

Returns the value of attribute w_sheet.



4
5
6
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 4

def w_sheet
  @w_sheet
end

Instance Method Details

#get_content_in_chunksObject



32
33
34
35
36
37
38
39
40
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 32

def get_content_in_chunks
  clear_data

  sheet.each @offset do |row|
    increment_counter
    @data << row[0..(row.size - 1)]
    break if reached_batch_size? || reached_end_of_file?
  end
end

#stream_rows_in_batchObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/spreadsheet_stream_reader/sheet.rb', line 14

def stream_rows_in_batch
  if block_given?
    while offset < max_record
      get_content_in_chunks

      @data.each do |row|
        yield row
      end

      increment_offset
    end
  else
    get_content_in_chunks
    increment_offset
    @data
  end
end