Class: NdrImport::File::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ndr_import/file/base.rb

Overview

All common base file handler logic is defined here.

Instance Method Summary collapse

Constructor Details

#initialize(filename, format, options = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
# File 'lib/ndr_import/file/base.rb', line 12

def initialize(filename, format, options = {})
  @filename = filename
  @format = format
  @options = options.stringify_keys

  validate_filename_is_safe_and_readable!
end

Instance Method Details

#files {|@filename| ... } ⇒ Object

This method iterates over the files in the given file and yields the filenames. For a zip file it will yield for every file in the zip file and for (currently) every other file it will yield its own filename.

As the majority of files are not containers (of other files), the Base implementation is defined for these handlers. If your file contains more than one file, then override this method. If you do overide this method, then you will probably want to raise an exception if your tables method is called. E.g. a zip file handler would produce files, never tables.

Yields:

  • (@filename)


29
30
31
32
33
# File 'lib/ndr_import/file/base.rb', line 29

def files
  return enum_for(:files) unless block_given?

  yield @filename
end

#tables {|nil, rows| ... } ⇒ Object

This method iterates over the tables in the given file and yields with two arguments: a tablename and a row enumerator (for that table). For a spreadsheet it may yield for every worksheet in the file and for a CSV file it will only yield once (the entire file is one table).

As single table files are in the majority, the Base implementation is defined for single table handlers and you will only need to implement the rows iterator. If your file contains more than one table, then override this method.

NOTE: for single table handlers, the tablename argument should be nil.

Yields:

  • (nil, rows)


45
46
47
48
49
# File 'lib/ndr_import/file/base.rb', line 45

def tables
  return enum_for(:tables) unless block_given?

  yield nil, rows
end