Class: Importer::CustomReader
- Inherits:
-
DataReader
- Object
- DataReader
- Importer::CustomReader
- Defined in:
- lib/iron/import/custom_reader.rb
Overview
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.
Instance Attribute Summary collapse
-
#readers ⇒ Object
Returns the value of attribute readers.
Attributes inherited from DataReader
Instance Method Summary collapse
- #init_source(mode, source) ⇒ Object
-
#initialize(importer) ⇒ CustomReader
constructor
A new instance of CustomReader.
- #load_raw(scopes, &block) ⇒ Object
-
#set_reader(mode, block) ⇒ Object
Called by the importer to add a handler for the given mode.
Methods inherited from DataReader
#add_error, #add_exception, for_format, for_path, for_source, for_stream, is_stream?, #load, #load_each, #parse_value, path_from_stream, #supports?, #supports_file!, #supports_file?, #supports_stream!, #supports_stream?, verify_nokogiri!, verify_roo!
Constructor Details
#initialize(importer) ⇒ CustomReader
Returns a new instance of CustomReader.
11 12 13 14 |
# File 'lib/iron/import/custom_reader.rb', line 11 def initialize(importer) super(importer, :custom) @readers = {} end |
Instance Attribute Details
#readers ⇒ Object
Returns the value of attribute readers.
9 10 11 |
# File 'lib/iron/import/custom_reader.rb', line 9 def readers @readers end |
Instance Method Details
#init_source(mode, source) ⇒ Object
22 23 24 25 |
# File 'lib/iron/import/custom_reader.rb', line 22 def init_source(mode, source) @mode = mode @source = source end |
#load_raw(scopes, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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) ⇒ Object
Called by the importer to add a handler for the given mode
17 18 19 20 |
# File 'lib/iron/import/custom_reader.rb', line 17 def set_reader(mode, block) @readers[mode] = block @supports << mode end |