Class: Bukovina::Importers::Common

Inherits:
Object
  • Object
show all
Includes:
FindInit
Defined in:
lib/bukovina/importers/common.rb

Direct Known Subclasses

Calendary, Memory

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FindInit

#foreign_class, #parse_array, #parse_hash, #separate_hash

Constructor Details

#initialize(attrs, o_attrs = {}) ⇒ Common

Returns a new instance of Common.



24
25
26
# File 'lib/bukovina/importers/common.rb', line 24

def initialize attrs, o_attrs = {}
@errors = []
@attrs = [ attrs.deep_dup ].flatten ;end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/bukovina/importers/common.rb', line 6

def errors
  @errors
end

Instance Method Details

#importObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bukovina/importers/common.rb', line 12

def import
      @attrs.each do |attrs|
(search_attrs, new_attrs) = separate_hash( parse_hash( model, attrs ) )

#binding.pry
o = model.where( search_attrs ).first_or_create( new_attrs )

errs = o.errors.map do |field, message|
   StandardError.new("#{field}: #{message}") ;end

@errors.concat(errs) ;end;end

#modelObject



8
9
10
# File 'lib/bukovina/importers/common.rb', line 8

def model
   @model ||= self.class::MODEL.to_s.constantize
end