Class: SpreadsheetDocument

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, options = {}) ⇒ SpreadsheetDocument

Returns a new instance of SpreadsheetDocument.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spreadsheet_document.rb', line 6

def initialize(file_name, options = {})
  @sheet_number = options[:sheet_number] || 0
  @filetype = options[:filetype] || detect_filetype(file_name)

  @spreadsheet = case @filetype
    when 'xls'
      Roo::Excel.new(file_name)
    when 'xlsx'
      Roo::Excelx.new(file_name)
    when 'ods'
      Roo::Openoffice.new(file_name)
    else
      Roo::CSV.new(file_name)
  end
  @current_sheet = @spreadsheet.sheets[@sheet_number]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/spreadsheet_document.rb', line 23

def method_missing(method, *args, &block)
  if @spreadsheet.respond_to? method.to_sym
    args << @current_sheet
    @spreadsheet.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#filetypeObject (readonly)

Returns the value of attribute filetype.



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

def filetype
  @filetype
end

#sheet_numberObject (readonly)

Returns the value of attribute sheet_number.



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

def sheet_number
  @sheet_number
end