class Importer::CustomReader

Special data reader that allows you to define a block to do the import yourself for cases where you have an odd text-based format or something else you want to be able to process using this gem. Check out Importer#on_file and Importer#on_stream to see how to use this reader type.

Attributes

readers[RW]

Public Class Methods

new(importer) click to toggle source
Calls superclass method
# File lib/iron/import/custom_reader.rb, line 11
def initialize(importer)
  super(importer, :custom)
  @readers = {}
end

Public Instance Methods

init_source(mode, source) click to toggle source
# File lib/iron/import/custom_reader.rb, line 22
def init_source(mode, source)
  @mode = mode
  @source = source
end
load_raw(scopes, &block) click to toggle source
# File lib/iron/import/custom_reader.rb, line 27
def load_raw(scopes, &block)
  # Default to just running one scope passing nil
  if scopes.nil? || scopes.empty?
    scopes = [nil]
  end
  
  # Get the proper reader
  reader = @readers[@mode]
  scopes.each do |scope|
    rows = DslProxy.exec(self, @source, scope, &reader)
    if rows.is_a?(Array) && !@importer.has_errors?
      found = block.call(rows)
      break if found
    end
  end
  
rescue Exception => e
  # Catch any exceptions thrown and note them with helpful stacktrace info for debugging custom readers
  add_exception(e)
end
set_reader(mode, block) click to toggle source

Called by the importer to add a handler for the given mode

# File lib/iron/import/custom_reader.rb, line 17
def set_reader(mode, block)
  @readers[mode] = block
  @supports << mode
end