data_active 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/data_active/version.rb +1 -1
- data/lib/data_active.rb +135 -135
- metadata +3 -3
data/lib/data_active/version.rb
CHANGED
data/lib/data_active.rb
CHANGED
@@ -19,84 +19,6 @@ module DataActive
|
|
19
19
|
many_from root_node_in source_xml
|
20
20
|
end
|
21
21
|
|
22
|
-
def many_from(current_node)
|
23
|
-
case
|
24
|
-
when self.name.pluralize.underscore.eql?(current_node.name.underscore)
|
25
|
-
many_from_rails_xml current_node
|
26
|
-
|
27
|
-
when (current_node.name.eql?('dataroot') \
|
28
|
-
and current_node.namespace_definitions.map { |ns| ns.href }.include?('urn:schemas-microsoft-com:officedata'))
|
29
|
-
# Identified as data generated from Microsoft Access
|
30
|
-
many_from_ms_xml current_node
|
31
|
-
|
32
|
-
when self.name.underscore.eql?(current_node.name.underscore)
|
33
|
-
raise "The supplied XML (#{current_node.name}) is a single instance of '#{self.name}'. Please use one_from_xml"
|
34
|
-
|
35
|
-
else
|
36
|
-
raise "The supplied XML (#{current_node.name}) cannot be mapped to this class (#{self.name})"
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def many_from_ms_xml(current_node)
|
42
|
-
records = []
|
43
|
-
recorded_ids = []
|
44
|
-
|
45
|
-
current_node.element_children.each do |node|
|
46
|
-
if self.name.underscore.eql?(node.name.underscore)
|
47
|
-
record = self.one_from_xml(node, @data_active_options)
|
48
|
-
if record
|
49
|
-
recorded_ids << record[primary_key.to_sym]
|
50
|
-
records << record
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
remove_records_not_in recorded_ids
|
56
|
-
|
57
|
-
records
|
58
|
-
end
|
59
|
-
|
60
|
-
def many_from_rails_xml(current_node)
|
61
|
-
records = []
|
62
|
-
recorded_ids = []
|
63
|
-
|
64
|
-
current_node.element_children.each do |node|
|
65
|
-
record = self.one_from_xml(node, @data_active_options)
|
66
|
-
if record
|
67
|
-
recorded_ids << record[primary_key.to_sym]
|
68
|
-
records << record
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
remove_records_not_in recorded_ids
|
73
|
-
|
74
|
-
records
|
75
|
-
end
|
76
|
-
|
77
|
-
def remove_records_not_in(recorded_ids)
|
78
|
-
if @data_active_options.include?(:sync)
|
79
|
-
if recorded_ids.length > 0
|
80
|
-
self.destroy_all [self.primary_key.to_s + " not in (?)", recorded_ids.collect]
|
81
|
-
end
|
82
|
-
elsif @data_active_options.include?(:destroy)
|
83
|
-
if recorded_ids.length > 0
|
84
|
-
self.destroy_all [self.primary_key.to_s + " not in (?)", recorded_ids.collect]
|
85
|
-
else
|
86
|
-
self.destroy_all
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def root_node_in(source_xml)
|
92
|
-
if source_xml.is_a?(String)
|
93
|
-
doc = Nokogiri::XML(source_xml)
|
94
|
-
doc.children.first
|
95
|
-
else
|
96
|
-
source_xml
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
22
|
def one_from_xml(source_xml, options = [])
|
101
23
|
@data_active_options = options
|
102
24
|
|
@@ -108,8 +30,22 @@ module DataActive
|
|
108
30
|
|
109
31
|
active_record = find_record_based_on(pk_node)
|
110
32
|
|
111
|
-
|
112
33
|
unless active_record.nil?
|
34
|
+
# Process the attributes
|
35
|
+
if options.include? :update or options.include? :sync or options.include? :create
|
36
|
+
assign_attributes_from current_node, :to => active_record
|
37
|
+
end
|
38
|
+
|
39
|
+
# Save the record
|
40
|
+
if options.include? :sync
|
41
|
+
# Doing complete synchronisation with XML
|
42
|
+
active_record.save
|
43
|
+
elsif options.include?(:create) and active_record.new_record?
|
44
|
+
active_record.save
|
45
|
+
elsif options.include?(:update) and not active_record.new_record?
|
46
|
+
active_record.save
|
47
|
+
end
|
48
|
+
|
113
49
|
# Check through associations and apply sync appropriately
|
114
50
|
self.reflect_on_all_associations.each do |association|
|
115
51
|
foreign_key = foreign_key_from(association)
|
@@ -184,21 +120,6 @@ module DataActive
|
|
184
120
|
raise "unsupported association #{association.macro} for #{association.name } on #{self.name}"
|
185
121
|
end
|
186
122
|
end
|
187
|
-
|
188
|
-
# Process the attributes
|
189
|
-
if options.include? :update or options.include? :sync or options.include? :create
|
190
|
-
assign_attributes_from current_node, :to => active_record
|
191
|
-
end
|
192
|
-
|
193
|
-
# Save the record
|
194
|
-
if options.include? :sync
|
195
|
-
# Doing complete synchronisation with XML
|
196
|
-
active_record.save
|
197
|
-
elsif options.include?(:create) and active_record.new_record?
|
198
|
-
active_record.save
|
199
|
-
elsif options.include?(:update) and not active_record.new_record?
|
200
|
-
active_record.save
|
201
|
-
end
|
202
123
|
end
|
203
124
|
|
204
125
|
active_record
|
@@ -207,17 +128,50 @@ module DataActive
|
|
207
128
|
end
|
208
129
|
end
|
209
130
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
elsif ActiveRecord::Reflection::AssociationReflection.method_defined? :primary_key_name
|
215
|
-
# Support for Rails earlier than 3.1
|
216
|
-
foreign_key = association.primary_key_name
|
131
|
+
private
|
132
|
+
def xml_node_matches_class(xml_node)
|
133
|
+
if xml_node.attributes['type'].blank?
|
134
|
+
xml_node.name.underscore == self.name.underscore
|
217
135
|
else
|
218
|
-
|
136
|
+
xml_node.attributes['type'].value.underscore == self.name.underscore
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def find_record_based_on(pk_node)
|
141
|
+
ar = nil
|
142
|
+
if pk_node
|
143
|
+
begin
|
144
|
+
ar = find pk_node.text
|
145
|
+
rescue
|
146
|
+
# No record exists, create a new one
|
147
|
+
if @data_active_options.include?(:sync) or @data_active_options.include?(:create)
|
148
|
+
ar = self.new
|
149
|
+
end
|
150
|
+
end
|
151
|
+
else
|
152
|
+
# No primary key value, must be a new record
|
153
|
+
if @data_active_options.include?(:sync) or @data_active_options.include?(:create)
|
154
|
+
ar = self.new
|
155
|
+
end
|
156
|
+
end
|
157
|
+
ar
|
158
|
+
end
|
159
|
+
|
160
|
+
def assign_attributes_from(current_node, options)
|
161
|
+
record = options[:to]
|
162
|
+
|
163
|
+
record.attributes.each do |name, value|
|
164
|
+
attribute_nodes = current_node.xpath name.to_s
|
165
|
+
if attribute_nodes.count == 1
|
166
|
+
if attribute_nodes[0].attributes['nil'].try(:value)
|
167
|
+
record[name] = nil
|
168
|
+
else
|
169
|
+
record[name] = attribute_nodes[0].text
|
170
|
+
end
|
171
|
+
elsif attribute_nodes.count > 1
|
172
|
+
raise "Found duplicate elements in xml for active record attribute '#{name}'"
|
173
|
+
end
|
219
174
|
end
|
220
|
-
foreign_key
|
221
175
|
end
|
222
176
|
|
223
177
|
def instances_for(association, options)
|
@@ -257,48 +211,94 @@ module DataActive
|
|
257
211
|
results
|
258
212
|
end
|
259
213
|
|
260
|
-
def
|
261
|
-
|
214
|
+
def foreign_key_from(association)
|
215
|
+
if ActiveRecord::Reflection::AssociationReflection.method_defined? :foreign_key
|
216
|
+
# Support for Rails 3.1 and later
|
217
|
+
foreign_key = association.foreign_key
|
218
|
+
elsif ActiveRecord::Reflection::AssociationReflection.method_defined? :primary_key_name
|
219
|
+
# Support for Rails earlier than 3.1
|
220
|
+
foreign_key = association.primary_key_name
|
221
|
+
else
|
222
|
+
raise "Unsupported version of ActiveRecord. Unable to identify the foreign key."
|
223
|
+
end
|
224
|
+
foreign_key
|
225
|
+
end
|
262
226
|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
227
|
+
def root_node_in(source_xml)
|
228
|
+
if source_xml.is_a?(String)
|
229
|
+
doc = Nokogiri::XML(source_xml)
|
230
|
+
doc.children.first
|
231
|
+
else
|
232
|
+
source_xml
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
def remove_records_not_in(recorded_ids)
|
237
|
+
if @data_active_options.include?(:sync)
|
238
|
+
if recorded_ids.length > 0
|
239
|
+
self.destroy_all [self.primary_key.to_s + " not in (?)", recorded_ids.collect]
|
240
|
+
end
|
241
|
+
elsif @data_active_options.include?(:destroy)
|
242
|
+
if recorded_ids.length > 0
|
243
|
+
self.destroy_all [self.primary_key.to_s + " not in (?)", recorded_ids.collect]
|
244
|
+
else
|
245
|
+
self.destroy_all
|
273
246
|
end
|
274
247
|
end
|
275
248
|
end
|
276
249
|
|
277
|
-
def
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
end
|
250
|
+
def many_from_rails_xml(current_node)
|
251
|
+
records = []
|
252
|
+
recorded_ids = []
|
253
|
+
|
254
|
+
current_node.element_children.each do |node|
|
255
|
+
record = self.one_from_xml(node, @data_active_options)
|
256
|
+
if record
|
257
|
+
recorded_ids << record[primary_key.to_sym]
|
258
|
+
records << record
|
287
259
|
end
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
260
|
+
end
|
261
|
+
|
262
|
+
remove_records_not_in recorded_ids
|
263
|
+
|
264
|
+
records
|
265
|
+
end
|
266
|
+
|
267
|
+
def many_from_ms_xml(current_node)
|
268
|
+
records = []
|
269
|
+
recorded_ids = []
|
270
|
+
|
271
|
+
current_node.element_children.each do |node|
|
272
|
+
if self.name.underscore.eql?(node.name.underscore)
|
273
|
+
record = self.one_from_xml(node, @data_active_options)
|
274
|
+
if record
|
275
|
+
recorded_ids << record[primary_key.to_sym]
|
276
|
+
records << record
|
277
|
+
end
|
292
278
|
end
|
293
279
|
end
|
294
|
-
|
280
|
+
|
281
|
+
remove_records_not_in recorded_ids
|
282
|
+
|
283
|
+
records
|
295
284
|
end
|
296
285
|
|
297
|
-
def
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
286
|
+
def many_from(current_node)
|
287
|
+
case
|
288
|
+
when self.name.pluralize.underscore.eql?(current_node.name.underscore)
|
289
|
+
many_from_rails_xml current_node
|
290
|
+
|
291
|
+
when (current_node.name.eql?('dataroot') \
|
292
|
+
and current_node.namespace_definitions.map { |ns| ns.href }.include?('urn:schemas-microsoft-com:officedata'))
|
293
|
+
# Identified as data generated from Microsoft Access
|
294
|
+
many_from_ms_xml current_node
|
295
|
+
|
296
|
+
when self.name.underscore.eql?(current_node.name.underscore)
|
297
|
+
raise "The supplied XML (#{current_node.name}) is a single instance of '#{self.name}'. Please use one_from_xml"
|
298
|
+
|
299
|
+
else
|
300
|
+
raise "The supplied XML (#{current_node.name}) cannot be mapped to this class (#{self.name})"
|
301
|
+
|
302
302
|
end
|
303
303
|
end
|
304
304
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_active
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -273,7 +273,7 @@ rubyforge_project: data_active
|
|
273
273
|
rubygems_version: 1.8.22
|
274
274
|
signing_key:
|
275
275
|
specification_version: 3
|
276
|
-
summary: data_active 0.0.
|
276
|
+
summary: data_active 0.0.2
|
277
277
|
test_files:
|
278
278
|
- features/remove_records_missing_in_xml.feature
|
279
279
|
- features/step_definitions/remove_records_missing_in_xml.rb
|