Class: Solr::Importer::DelimitedFileSource

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/solr/importer/delimited_file_source.rb

Overview

For files with the first line containing field names Currently not designed for enormous files, as all lines are read into an array

Instance Method Summary collapse

Constructor Details

#initialize(filename, splitter = /\t/) ⇒ DelimitedFileSource

Returns a new instance of DelimitedFileSource


19
20
21
22
# File 'lib/solr/importer/delimited_file_source.rb', line 19

def initialize(filename, splitter=/\t/)
  @filename = filename
  @splitter = splitter
end

Instance Method Details

#eachObject


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/solr/importer/delimited_file_source.rb', line 24

def each
  lines = IO.readlines(@filename)
  headers = lines[0].split(@splitter).collect{|h| h.chomp}
  
  lines[1..-1].each do |line|
    data = headers.zip(line.split(@splitter).collect{|s| s.chomp})
    def data.[](key)
      self.assoc(key.to_s)[1]
    end
    
    yield(data)
  end
end