Module: Scheme::Parser::Base

Defined in:
lib/scheme/parser/base.rb

Defined Under Namespace

Classes: ParameterError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/scheme/parser/base.rb', line 8

def errors
  @errors
end

#thisObject (readonly)

Returns the value of attribute this.



8
9
10
# File 'lib/scheme/parser/base.rb', line 8

def this
  @this
end

Instance Method Details

#import(xml) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/scheme/parser/base.rb', line 12

def import xml
   model_attrs = parse(xml)
   #name = File.basename(attrs.keys.first, '_attributes')
   #model = name.singularize.camelize.constantize
   #model_attrs = attrs.values.first

   model = self
   key = current_options[:key].to_s || 'id'

   if model_attrs[key]
      if instance = model.where(key => model_attrs[key]).first
         instance.update!(model_attrs)
      else
         instance = model.new(model_attrs)
         instance.save!
      end
#         binding.pry
#         model_attrs.each do |name, value|
#            if value.is_a?(Array)
#               _update_array(name, value)
#            end
#         end

#         binding.pry
   end
#   rescue => e
#      err_text = "Failed to import record #{name} with messages '#{e.message}'"
#      if instance && !(errs = show_errors(instance.errors)).empty?
#         err_text += " and #{errs}"
#      end
#      err_text += " at #{e.backtrace.first}"
#
#      Rails.logger.error(err_text)
#      error "", err_text
end

#import_attrs(attrs) ⇒ Object

def _update_array name, array

  array.each do |value|
    if value['id']
      name = File.basename(name, '_attributes')
      m = name.singularize.camelize.constantize
      i = m.where(id: value['id']).first
      binding.pry
      i.update!(value)
    end
  end

  array.delete_if { |value | value['id'] }
end


73
74
# File 'lib/scheme/parser/base.rb', line 73

def import_attrs attrs
end

#parse(xml) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
# File 'lib/scheme/parser/base.rb', line 48

def parse xml
   clean_errors
   filtered = xml.is_a?(String) && xml.gsub(/[\r\u{feff}]/,"") || xml
   doc = filtered.kind_of?(Nokogiri::XML::Element) && filtered || Nokogiri::XML.parse(filtered)
   @this = {}
   attrs = each_field_for(self.schemes[nil], doc)
   #binding.pry
   raise ParameterError if errors.present?
   attrs
end