Class: Sheets::Base

Inherits:
Object
  • Object
show all
Includes:
Parseable, Renderable
Defined in:
lib/sheets/base.rb

Instance Method Summary collapse

Methods included from Renderable

included, #method_missing

Methods included from Parseable

#each, included

Constructor Details

#initialize(file, options = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(file, options = {})
  if file.is_a?(Array)
    @data = file
    @extension = options[:format].to_s
    return
  end

  file = File.open(file.to_s) unless file.respond_to? :read
  options[:format] ||= File.basename(file.path || "").split('.')[-1]
  
  @data = file.read
  @extension = options[:format].to_s
  @file_path = File.expand_path(file.path || "")

  raise UnsupportedSpreadsheetFormatError, "Couldn't find a parser for the '#{@extension}' format." if parser.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Sheets::Renderable

Instance Method Details

#default_to_arrayObject



23
# File 'lib/sheets/base.rb', line 23

alias_method :default_to_array, :to_array

#to_arrayObject



24
25
26
# File 'lib/sheets/base.rb', line 24

def to_array
  @data.is_a?(Array) ? @data : default_to_array
end