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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



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

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

  validate_filename_is_safe_and_readable!
end

Instance Attribute Details

#file_metadataObject

Returns the value of attribute file_metadata.



12
13
14
# File 'lib/ndr_import/file/base.rb', line 12

def 
  
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)


31
32
33
34
35
# File 'lib/ndr_import/file/base.rb', line 31

def files
  return enum_for(:files) unless block_given?

  yield @filename
end

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

This method iterates over the tables in the given file and yields with three arguments: a tablename, a row enumerator (for that table) and any file metadata. 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:



47
48
49
50
51
# File 'lib/ndr_import/file/base.rb', line 47

def tables
  return enum_for(:tables) unless block_given?

  yield nil, rows, 
end