Class: Ditch::Sheet

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

Overview

Class wrapping the Creek::Sheet class for adding small things

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheet, options = {}) ⇒ Sheet

Create a Ditch::Sheet

  • sheet Creek::Sheet to make the base

  • options Options to control the behaviour of the sheet



9
10
11
12
13
14
15
# File 'lib/ditch/sheet.rb', line 9

def initialize(sheet,options={})
  @creek_sheet = sheet
  @opts = options
  if @opts[:first_row_is_header]
    @headers = headers
  end
end

Instance Attribute Details

#creek_sheetObject (readonly)

Returns the value of attribute creek_sheet.



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

def creek_sheet
  @creek_sheet
end

Instance Method Details

#headersObject

Retrieve headers, if :first_row_is_header option is set



18
19
20
21
22
23
24
25
# File 'lib/ditch/sheet.rb', line 18

def headers
  headers = Hash.new
  header_row = @creek_sheet.rows.first
  header_row.each_key do |k|
    headers[header_row[k]]= k.gsub(/\d/,'')
  end
  headers
end

#rowsObject

Enumerate the Rows in the given Sheet, As Ditch::IndexedRows



28
29
30
31
32
33
34
35
36
# File 'lib/ditch/sheet.rb', line 28

def rows
  Enumerator.new do |y|
    @creek_sheet.rows.each_with_index do |row, index|
      unless index == 0 && @opts[:first_row_is_header]
        y << Ditch::IndexedRow.new(@headers, index+1, row)
      end
    end
  end
end