Module: XmlActive::ClassMethods

Defined in:
lib/xml_active.rb

Instance Method Summary collapse

Instance Method Details

#many_from_xml(xml, options = []) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/xml_active.rb', line 17

def many_from_xml(xml, options = [])
  if xml.is_a?(String)
    doc = Nokogiri::XML(xml)
    current_node = doc.children.first
  else
    current_node = xml
  end

  records = []
  if self.name.pluralize.underscore.eql? current_node.name.underscore
    ids = []

    if current_node.attributes['type'].try(:value) == "array"
      current_node.element_children.each do |node|
        record = self.one_from_xml(node, options)
        if record
          ids[ids.length] = record[primary_key.to_sym]
          records[records.length] = record
        end
      end
    else
      records[records.length] = self.one_from_xml current_node
    end


    if options.include?(:sync)
      if ids.length > 0
        self.destroy_all [self.primary_key.to_s + " not in (?)", ids.collect]
      end
    elsif options.include?(:destroy)
      if ids.length > 0
        self.destroy_all [self.primary_key.to_s + " not in (?)", ids.collect]
      else
        self.destroy_all
      end
    end

  elsif self.name.underscore.equ current_node.name.underscore
    raise "The supplied XML (#{current_node.name}) is a single instance of '#{self.name}'. Please use one_from_xml"
  else
    raise "The supplied XML (#{current_node.name}) cannot be mapped to this class (#{self.name})"
  end

  records
end

#one_from_xml(xml, options = []) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/xml_active.rb', line 63

def one_from_xml(xml, options = [])
  if xml.is_a? String
    doc = Nokogiri::XML xml
    current_node = doc.children.first
  else
    current_node = xml
  end

  if xml_node_matches_class(current_node)
    # Load or create a new record
    pk_value = 0
    pk_node = current_node.xpath self.primary_key.to_s
    if pk_node
      begin
        ar = find pk_node.text
        pk_value = pk_node.text
      rescue
        # No record exists, create a new one
        if options.include?(:sync) or options.include?(:create)
          ar = self.new
        else
          # must have only have :destroy and/or :update so exit
          return nil
        end
      end
    else
      # No primary key value, must be a new record
      if options.include?(:sync) or options.include?(:create)
        ar = self.new
      else
        # must have only have :destroy and/or :update so exit
        return nil
      end
    end

    # Check through associations and apply sync appropriately
    self.reflect_on_all_associations.each do |association|
      if ActiveRecord::Reflection::AssociationReflection.method_defined? :foreign_key
        # Support for Rails 3.1 and later
        foreign_key = association.foreign_key
      elsif ActiveRecord::Reflection::AssociationReflection.method_defined? :primary_key_name
        # Support for Rails earlier than 3.1
        foreign_key = association.primary_key_name
      else
        raise "Unsupported version of ActiveRecord. Unable to identify the foreign key."
      end
      case
        when association.macro == :has_many, association.macro == :has_and_belongs_to_many
          # Check to see if xml contains elements for the association
          if pk_value == 0
            containers = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{pk_node.text}]/#{association.name}")
          else
            containers = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{pk_value}]/#{association.name}")
          end
          if containers.count > 0
            container = containers[0]
            klass = association.klass
            child_ids = []
            container.element_children.each do |single_obj|
              # TODO: Allow for child node that doesn't have a primary key value
              child_ids[child_ids.length] = single_obj.xpath(self.primary_key.to_s).text
              new_record = klass.one_from_xml(single_obj, options)
              if (new_record != nil)
                ar.__send__(container.name.underscore.to_sym) << new_record
              end
            end

            if pk_value != 0
              if options.include?(:sync)
                if child_ids.length > 0
                  klass.destroy_all [klass.primary_key.to_s + " not in (?) and #{foreign_key} = ?", child_ids.collect, pk_value]
                end
              elsif options.include?(:destroy)
                if child_ids.length > 0
                  klass.destroy_all [klass.primary_key.to_s + " not in (?) and #{foreign_key} = ?", child_ids.collect, pk_value]
                else
                  klass.destroy_all
                end
              end
            end
          end

        when association.macro == :has_one
          single_objects = current_node.xpath("//#{self.name.underscore}[#{self.primary_key}=#{pk_value}]/#{association.name}")
          klass = association.klass
          record = klass.where(foreign_key => pk_value).all
          if single_objects.count == 1
            # Check to see if the already record exists
            if record.count == 1
              db_pk_value = record[0][klass.primary_key]
              xml_pk_value = Integer(single_objects[0].element_children.xpath("//#{self.name.underscore}/#{klass.primary_key}").text)

              if db_pk_value != xml_pk_value
                # Different record in xml
                if options.include?(:sync) or options.include?(:destroy)
                  # Delete the one in the database
                  klass.destroy(record[0][klass.primary_key])
                end
              end
            elsif record.count > 1
              raise "Too many records for one to one association in the database. Found #{record.count} records of '#{association.name}' for association with '#{self.name}'"
            end

            if options.include?(:create) or options.include?(:update) or options.include?(:sync)
              new_record = klass.one_from_xml(single_objects[0], options)
              if new_record != nil
                new_record[foreign_key.to_sym] = ar[self.primary_key]
                new_record.save!
              end
            end
          elsif single_objects.count > 1
            # There are more than one associations
            raise "Too many records for one to one association in the provided XML. Found #{single_objects.count} records of '#{association.name}' for association with '#{self.name}'"
          else
            # There are no records in the XML
            if record.count > 0 and options.include?(:sync) or options.include?(:destroy)
              # Found some in the database: destroy then
              klass.destroy_all("#{foreign_key} = #{pk_value}")
            end
          end

        when association.macro == :belongs_to

        else
          raise "unsupported association #{association.macro} for #{association.name  } on #{self.name}"
      end
    end

    if options.include? :update or options.include? :sync or options.include? :create
      # Process the attributes
      current_node.element_children.each do |node|
        node_name = node.name.underscore.to_sym
        association = self.reflect_on_association node_name

        if association.nil?
          if node.attributes['nil'].try(:value)
            ar[node_name] = nil
          else
            ar[node_name] = node.text
          end
        end
      end
    end

    if options.include? :sync
      # Doing complete synchronisation with XML
      ar.save
    elsif options.include?(:create) and ar.new_record?
      ar.save
    elsif options.include?(:update) and not ar.new_record?
      ar.save
    end

    ar
  else
    raise "The supplied XML (#{current_node.name}) cannot be mapped to this class (#{self.name})"
  end
end

#xml_node_matches_class(xml_node) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/xml_active.rb', line 222

def xml_node_matches_class(xml_node)
  if xml_node.attributes['type'].blank?
    xml_node.name.underscore == self.name.underscore
  else
    xml_node.attributes['type'].value.underscore == self.name.underscore
  end
end