Class: Sheetah::Backends::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Sheet
Defined in:
lib/sheetah/backends/wrapper.rb

Constant Summary

Constants included from Sheet

Sheet::COL_CONVERTER

Instance Method Summary collapse

Methods included from Sheet

col2int, included, int2col

Constructor Details

#initialize(table) ⇒ Wrapper

Returns a new instance of Wrapper.

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sheetah/backends/wrapper.rb', line 10

def initialize(table)
  raise Error if table.nil?

  @table = table

  if (table_size = @table.size).positive?
    @headers    = @table[0]
    @rows_count = table_size - 1
    @cols_count = @headers.size
  else
    @headers    = []
    @rows_count = 0
    @cols_count = 0
  end
end

Instance Method Details

#closeObject



52
53
54
# File 'lib/sheetah/backends/wrapper.rb', line 52

def close
  # nothing to do here
end

#each_headerObject



26
27
28
29
30
31
32
33
34
# File 'lib/sheetah/backends/wrapper.rb', line 26

def each_header
  return to_enum(:each_header) { @cols_count } unless block_given?

  1.upto(@cols_count) do |col|
    yield Header.new(col: Sheet.int2col(col), value: @headers[col - 1])
  end

  self
end

#each_rowObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sheetah/backends/wrapper.rb', line 36

def each_row
  return to_enum(:each_row) unless block_given?

  1.upto(@rows_count) do |row|
    raw = @table[row]

    value = Array.new(@cols_count) do |col_idx|
      Cell.new(row: row, col: Sheet.int2col(col_idx + 1), value: raw[col_idx])
    end

    yield Row.new(row: row, value: value)
  end

  self
end