Class: BBMB::Util::CsvImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/bbmb/util/csv_importer.rb

Direct Known Subclasses

CustomerImporter, ProductImporter

Instance Method Summary collapse

Instance Method Details

#import(io, persistence = BBMB.persistence) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bbmb/util/csv_importer.rb', line 12

def import(io, persistence=BBMB.persistence)
  count = 0
  if io.respond_to?(:path)
    get_encoding =  `file --brief "#{io.path}"`
    io.set_encoding('ISO-8859-1', 'UTF-8') if /^ISO-8859/.match(get_encoding)
  end

  io.each { |line|
          next if line.size < 5
    record = line.split("\t")
    if(object = import_record(record))
      persistence.save(object)
    end
    count += 1
  }
  postprocess(persistence)
  count
end

#postprocess(persistence = BBMB.persistence) ⇒ Object



30
31
# File 'lib/bbmb/util/csv_importer.rb', line 30

def postprocess(persistence=BBMB.persistence)
end

#string(str) ⇒ Object



32
33
34
35
# File 'lib/bbmb/util/csv_importer.rb', line 32

def string(str)
  str = str.to_s.strip
  str.gsub(/\s+/, ' ') unless str.empty? 
end