Class: Spreadshot::Reader

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Reader

Returns a new instance of Reader

Examples:

reader = Spreadshot::Reader.new(backend: :smarter_csv)

reader = Spreadshot::Reader.new(backend: :ruby_xl, worksheet_index: 0, cell_index_to_field_mapper: {0 => :H1, 1 => :H2, 2 => :H3, 3 => :H4})

xlsx_backend = Spreadshot::Backends::RubyXLBackend.new(worksheet_index: 0, cell_index_to_field_mapper: {0 => :H1, 1 => :H2, 2 => :H3, 3 => :H4})
reader = Spreadshot::Reader.new(backend: xlsx_backend)

Options Hash (options):

  • :backend (Symbol, Spreadshot::ReaderBackend)

    The reader backend to use or the id if one of the provided backends

  • :backend_options (Hash)

    Required if one of the provided backends is used

    • :headers (Boolean) [true] Specifies whether the spreadsheet to be read contains headers. Will be ignored if the backend doesn't provide this option (E.g. SmarterCSV expects headers)

    • :worksheet_index (Integer) [0] The index of the worksheet to be read. Used for the RubyXL backend

    • :cell_index_to_field_mapper (Hash<Integer, [String, Symbol]>) A hash which maps each cell in a row of the spreadsheet to a key in the result hash created for that row. Must be provided for the RubyXL backend


20
21
22
# File 'lib/spreadshot/reader.rb', line 20

def initialize(options)
  set_backend(options[:backend], options[:backend_options])
end

Instance Method Details

#build_backend(backend_id, backend_options = {}) ⇒ Spreadshot::Backends::ReaderBackend (private)

Creates a reader backend using the backend (id) and options/backend_options provided in #initialize or #set_backend

Options Hash (backend_options):

  • :headers (Boolean)

    Specifies whether the spreadsheet to be read contains headers. Will be ignored if the backend doesn't provide this option (E.g. SmarterCSV expects headers) Defaults to true

  • :worksheet_index (Integer)

    The index of the worksheet to be read. Used for the RubyXL backend. Defaults to true

  • :cell_index_to_field_mapper (Hash<Integer, [Symbol, String]>)

    A hash which maps each cell in a row of the spreadsheet to a key in the result hash created for that row. Used for the RubyXL backend. Must be provided


112
113
114
115
116
117
# File 'lib/spreadshot/reader.rb', line 112

def build_backend(backend_id, backend_options = {})
  backend_options ||= {}
  return provided_backends[backend_id].call(backend_options)
rescue NoMethodError
  return nil
end

#provided_backendsHash (private)

The provided backends


125
126
127
128
129
130
# File 'lib/spreadshot/reader.rb', line 125

def provided_backends
  {
    smarter_csv: ->(backend_options){ Spreadshot::Backends::SmarterCSVBackend.new(backend_options) },
    ruby_xl: ->(backend_options){ Spreadshot::Backends::RubyXLBackend.new(backend_options) },
  }
end

#read(path_to_spreadsheet) {|row_index, row_data| ... } ⇒ Object

Note:

This method delegates actual reading to the backend

Reads data from the specified spreadsheet

Examples:

reader = Spreadshot::Reader.new(backend: :ruby_xl, worksheet_index: 0, cell_index_to_field_mapper: {0 => :H1, 1 => :H2, 2 => :H3, 3 => :H4})
reader.read('spreadshot_test.xlsx'){|row_index, row_data| puts "#{row_index} - #{row_data}"}

Sample output
2 - {:H1=>11, :H2=>22, :H3=>33, :H4=>44}
3 - {:H1=>111, :H2=>222, :H3=>333, :H4=>444}
4 - {:H1=>1111, :H2=>2222, :H3=>3333, :H4=>4444}

Yields:

  • (row_index, row_data)

Yield Parameters:

  • row_index (Integer)

    The index of the current row being read. The first row has an index of 1

  • row_data (Hash)

    A hash representation of the data read from the current row


85
86
87
# File 'lib/spreadshot/reader.rb', line 85

def read(path_to_spreadsheet)
  @backend.read(path_to_spreadsheet) {|row_index, row_data| yield(row_index, row_data)}
end

#set_backend(backend, backend_options = {}) ⇒ Object

Set the backend to use

Examples:

reader = Spreadshot::Reader.new(backend: :ruby_xl, worksheet_index: 0, cell_index_to_field_mapper: {0 => :H1, 1 => :H2, 2 => :H3, 3 => :H4})
csv_backend = Spreadshot::Backends::SmarterCSVBackend.new
reader.set_backend(csv_backend)

Options Hash (backend_options):

  • :headers (Boolean)

    Specifies whether the spreadsheet to be read contains headers. Will be ignored if the backend doesn't provide this option (E.g. SmarterCSV expects headers) Defaults to true

  • :worksheet_index (Integer)

    The index of the worksheet to be read. Used for the RubyXL backend. Defaults to 0

  • :cell_index_to_field_mapper (Hash<Integer, [Symbol, String]>)

    A hash which maps each cell in a row of the spreadsheet to a key in the result hash created for that row. Used for the RubyXL backend. Must be provided

Raises:


52
53
54
55
56
57
58
59
# File 'lib/spreadshot/reader.rb', line 52

def set_backend(backend, backend_options = {})
  @backend = (backend.is_a?(Spreadshot::Backends::ReaderBackend)) ? backend : build_backend(
    backend,
    backend_options
  )

  raise Spreadshot::BackendNotFound if @backend.nil?
end