Class: Importu::Importer
- Includes:
- Converters, Dsl
- Defined in:
- lib/importu/importer.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#created ⇒ Object
readonly
Returns the value of attribute created.
-
#infile ⇒ Object
readonly
Returns the value of attribute infile.
-
#invalid ⇒ Object
readonly
Returns the value of attribute invalid.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#outfile ⇒ Object
readonly
Returns the value of attribute outfile.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
-
#unchanged ⇒ Object
readonly
Returns the value of attribute unchanged.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
-
#validation_errors ⇒ Object
readonly
Returns the value of attribute validation_errors.
Instance Method Summary collapse
- #import!(finder_scope = nil, &block) ⇒ Object
-
#initialize(infile, options = {}) ⇒ Importer
constructor
A new instance of Importer.
- #records ⇒ Object
- #result_msg ⇒ Object
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, = {}) = @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
#created ⇒ Object (readonly)
Returns the value of attribute created.
5 6 7 |
# File 'lib/importu/importer.rb', line 5 def created @created end |
#infile ⇒ Object (readonly)
Returns the value of attribute infile.
4 5 6 |
# File 'lib/importu/importer.rb', line 4 def infile @infile end |
#invalid ⇒ Object (readonly)
Returns the value of attribute invalid.
5 6 7 |
# File 'lib/importu/importer.rb', line 5 def invalid @invalid end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/importu/importer.rb', line 4 def end |
#outfile ⇒ Object (readonly)
Returns the value of attribute outfile.
4 5 6 |
# File 'lib/importu/importer.rb', line 4 def outfile @outfile end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
5 6 7 |
# File 'lib/importu/importer.rb', line 5 def total @total end |
#unchanged ⇒ Object (readonly)
Returns the value of attribute unchanged.
5 6 7 |
# File 'lib/importu/importer.rb', line 5 def unchanged @unchanged end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
5 6 7 |
# File 'lib/importu/importer.rb', line 5 def updated @updated end |
#validation_errors ⇒ Object (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 |
#records ⇒ Object
18 19 20 |
# File 'lib/importu/importer.rb', line 18 def records [].to_enum # implement in a subclass end |
#result_msg ⇒ Object
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 |