Class: Importu::Importer

Inherits:
Object show all
Includes:
Converters, Dsl
Defined in:
lib/importu/importer.rb

Direct Known Subclasses

Csv, Json, Xml

Defined Under Namespace

Classes: Csv, Json, Xml

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infile, options = {}) ⇒ Importer

Returns a new instance of Importer.



10
11
12
13
14
15
16
# File 'lib/importu/importer.rb', line 10

def initialize(infile, options = {})
  @options = options
  @total = @invalid = @created = @updated = @unchanged = 0
  @validation_errors = Hash.new(0) # counter for each validation error

  @infile = infile.respond_to?(:readline) ? infile : File.open(infile, 'rb')
end

Instance Attribute Details

#createdObject (readonly)

Returns the value of attribute created.



5
6
7
# File 'lib/importu/importer.rb', line 5

def created
  @created
end

#infileObject (readonly)

Returns the value of attribute infile.



4
5
6
# File 'lib/importu/importer.rb', line 4

def infile
  @infile
end

#invalidObject (readonly)

Returns the value of attribute invalid.



5
6
7
# File 'lib/importu/importer.rb', line 5

def invalid
  @invalid
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/importu/importer.rb', line 4

def options
  @options
end

#outfileObject (readonly)

Returns the value of attribute outfile.



4
5
6
# File 'lib/importu/importer.rb', line 4

def outfile
  @outfile
end

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/importu/importer.rb', line 5

def total
  @total
end

#unchangedObject (readonly)

Returns the value of attribute unchanged.



5
6
7
# File 'lib/importu/importer.rb', line 5

def unchanged
  @unchanged
end

#updatedObject (readonly)

Returns the value of attribute updated.



5
6
7
# File 'lib/importu/importer.rb', line 5

def updated
  @updated
end

#validation_errorsObject (readonly)

Returns the value of attribute validation_errors.



4
5
6
# File 'lib/importu/importer.rb', line 4

def validation_errors
  @validation_errors
end

Instance Method Details

#import!(finder_scope = nil, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/importu/importer.rb', line 26

def import!(finder_scope = nil, &block)
  # if a scope is passed in, that scope becomes the starting scope used by
  # the finder, otherwise the model's default scope is used).

  finder_scope ||= model_class.scoped
  records.each {|r| import_record(r, finder_scope, &block) }
end

#recordsObject



18
19
20
# File 'lib/importu/importer.rb', line 18

def records
  [].to_enum # implement in a subclass
end

#result_msgObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/importu/importer.rb', line 34

def result_msg
  msg = "    Total:     \#{@total}\n    Created:   \#{@created}\n    Updated:   \#{@updated}\n    Invalid:   \#{@invalid}\n    Unchanged: \#{@unchanged}\n  END\n\n  if @validation_errors.any?\n    msg << \"\\nValidation Errors:\\n\"\n    msg << @validation_errors.map {|e,c| \"  - \#{e}: \#{c}\" }.join(\"\\n\")\n  end\n\n  msg\nend\n".strip_heredoc