Class: DataImp

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods, Dir, Finders, Import, ImportMethods
Includes:
Import, Options
Defined in:
lib/data_imp.rb,
lib/data_imp.rb,
lib/data_imp/base.rb,
lib/data_imp/file.rb,
lib/data_imp/import.rb,
lib/data_imp/options.rb,
lib/data_imp/version.rb

Defined Under Namespace

Modules: ClassMethods, Dir, Finders, Import, ImportMethods, Options Classes: Base, File, NoImporter, NoParser, Parser, Porter

Constant Summary collapse

VERSION =
"0.4.1"
Importer =
DataImp::Porter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Finders

extended, find_importer, find_parser, included

Methods included from Dir

data_dir, data_dir=, extended, root, root=

Methods included from ImportMethods

import_list

Methods included from Import

import_list, parse

Methods included from Options

included, #options, #options=

Constructor Details

#initialize(file = nil, parser: nil, importer: nil) ⇒ DataImp

Returns a new instance of DataImp.



28
29
30
31
32
33
34
35
36
37
# File 'lib/data_imp.rb', line 28

def initialize file = nil, parser: nil, importer: nil
  return unless file

  @file = data_dir.join(file)
  extname = File.extname(file)
  @basename ||= File.basename(file, extname)
  @extname  = extname.downcase.sub('.','')
  @importer = find_importer(importer || @basename) # returns class
  @parser   = find_parser(parser || @extname) # returns class
end

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



26
27
28
# File 'lib/data_imp.rb', line 26

def basename
  @basename
end

#extnameObject

Returns the value of attribute extname.



26
27
28
# File 'lib/data_imp.rb', line 26

def extname
  @extname
end

#fileObject

Returns the value of attribute file.



26
27
28
# File 'lib/data_imp.rb', line 26

def file
  @file
end

#importerObject

Returns the value of attribute importer.



26
27
28
# File 'lib/data_imp.rb', line 26

def importer
  @importer
end

#parserObject

Returns the value of attribute parser.



26
27
28
# File 'lib/data_imp.rb', line 26

def parser
  @parser
end

Instance Method Details

#importObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/data_imp.rb', line 39

def import
  puts "Importing #{basename} with #{importer}"
  importer.before_all_imports
  parser.new(file).process_file do |hash, index|
    porter = importer.new(hash, index)
    begin
      porter.before_import
      porter.import
      porter.after_import
      show_progress index
    rescue StandardError => e
      warn "#{basename}:#{index}:#{e.class.name}"
      porter.on_error e
    end
  end
  importer.after_all_imports
  puts
end

#show_progress(index) ⇒ Object



58
59
60
61
# File 'lib/data_imp.rb', line 58

def show_progress index
  puts if index % 10_000 == 0
  print '.' if index % 100 == 0
end