public
Rubygem
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/sam/dm-more.git
Refactored some protected methods.  Somewhat baffled at the alleged 
failure on create/update when the create and updates are actually 
occurring.  'tis something in dm-core Resource.rb that is biting us.
elight (author)
Mon Jun 16 20:50:21 -0700 2008
dkubb (committer)
Tue Jun 17 17:34:31 -0700 2008
commit  7510527a36cb1c032a649e9b0c455f2ad2c54833
tree    51805586cda21b9bd2cbabd54ea53c7e8c38d18b
parent  a3e3e58853f8001dd7aec7a6efa8108e097652d8
...
28
29
30
 
31
32
33
34
35
 
 
 
 
 
36
37
 
38
39
40
...
59
60
61
 
62
63
64
...
68
69
70
71
 
72
73
74
...
86
87
88
 
89
90
91
...
97
98
99
100
 
101
102
103
...
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
...
28
29
30
31
32
33
34
35
 
36
37
38
39
40
41
42
43
44
45
46
...
65
66
67
68
69
70
71
...
75
76
77
 
78
79
80
81
...
93
94
95
96
97
98
99
...
105
106
107
 
108
109
110
111
...
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
0
@@ -28,13 +28,19 @@ module DataMapper
0
       
0
       # Creates a new resource in the specified repository.
0
       def create(resources)
0
+ success = true
0
         resources.each do |resource|
0
           resource_name = Inflection.underscore(resource.class.name.downcase)
0
           result = http_post("/#{resource_name.pluralize}.xml", resource.to_xml)
0
           # TODO: Raise error if cannot reach server
0
- result.kind_of? Net::HTTPSuccess
0
+ success = success && result.instance_of?(Net::HTTPCreated)
0
+ if success
0
+ updated_resource = parse_resource(result.body, resource.class)
0
+ resource.id = updated_resource.id
0
+ end
0
           # TODO: We're not using the response to update the DataMapper::Resource with the newly acquired ID!!!
0
         end
1
+ success
0
       end
0
       
0
       # read_set
0
@@ -59,6 +65,7 @@ module DataMapper
0
       end
0
       
0
       def read_one(query)
0
+ # puts "---------------- QUERY: #{query} #{query.inspect}"
0
         id = query.conditions.first[2]
0
         # KLUGE: Again, we're assuming below that we're dealing with a pluralized resource mapping
0
         resource_name = resource_name_from_query(query)
0
@@ -68,7 +75,7 @@ module DataMapper
0
         return nil if response.is_a? Net::HTTPNotFound || response.content_type == "text/html"
0
         
0
         data = response.body
0
- res = parse_resource(data, resource_name, query.model, query.fields)
0
+ res = parse_resource(data, query.model)
0
         res
0
       end
0
       
0
@@ -86,6 +93,7 @@ module DataMapper
0
       end
0
       
0
       def delete(query)
0
+ #puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> QUERY: #{query} #{query.inspect}"
0
         # TODO update for v0.9.2
0
         raise NotImplementedError.new unless is_single_resource_query? query
0
         id = query.conditions.first[2]
0
@@ -97,7 +105,7 @@ module DataMapper
0
         # TODO: how do we know whether the resource we're talking to is singular or plural?
0
         res = http_get("/#{resource_name.pluralize}.xml")
0
         data = res.body
0
- parse_resources(data, resource_name, query.model, query.fields)
0
+ parse_resources(data, query.model)
0
         # TODO: Raise error if cannot reach server
0
       end
0
       
0
@@ -136,41 +144,46 @@ module DataMapper
0
         res
0
       end
0
 
0
- def resource_from_rexml(entity_element, dm_model_class, dm_properties)
0
+ def resource_from_rexml(entity_element, dm_model_class)
0
         resource = dm_model_class.new
0
         entity_element.elements.each do |field_element|
0
- dm_property = dm_properties.find do |p|
0
+ attribute = resource.attributes.find do |name, val|
0
             # *MUST* use Inflection.underscore on the XML as Rails converts '_' to '-' in the XML
0
- p.name.to_s == Inflection.underscore(field_element.name.to_s)
0
+ name.to_s == Inflection.underscore(field_element.name.to_s)
0
           end
0
- resource.send("#{Inflection.underscore(dm_property.name)}=", field_element.text) if dm_property
0
+ resource.send("#{Inflection.underscore(attribute[0])}=", field_element.text) if attribute
0
         end
0
+ resource.instance_eval { @new_record= false }
0
         resource
0
       end
0
 
0
- def parse_resource(xml, resource_name, dm_model_class, dm_properties)
0
+ def parse_resource(xml, dm_model_class)
0
         doc = REXML::Document::new(xml)
0
         # TODO: handle singular resource case as well....
0
- entity_element = REXML::XPath.first(doc, "/#{resource_name}")
0
+ entity_element = REXML::XPath.first(doc, "/#{resource_name_from_model(dm_model_class)}")
0
         return nil unless entity_element
0
- resource_from_rexml(entity_element, dm_model_class, dm_properties)
0
+ resource_from_rexml(entity_element, dm_model_class)
0
       end
0
       
0
- def parse_resources(xml, resource_name, dm_model_class, dm_properties)
0
+ def parse_resources(xml, dm_model_class)
0
         doc = REXML::Document::new(xml)
0
         # # TODO: handle singular resource case as well....
0
         # array = XPath(doc, "/*[@type='array']")
0
         # if array
0
         # parse_resources()
0
         # else
0
-
0
+ resource_name = resource_name_from_model dm_model_class
0
         doc.elements.collect("#{resource_name.pluralize}/#{resource_name}") do |entity_element|
0
- resource_from_rexml(entity_element, dm_model_class, dm_properties)
0
+ resource_from_rexml(entity_element, dm_model_class)
0
         end
0
       end
0
       
0
+ def resource_name_from_model(model)
0
+ Inflection.underscore(model.name.downcase)
0
+ end
0
+
0
       def resource_name_from_query(query)
0
- Inflection.underscore(query.model.name.downcase)
0
+ resource_name_from_model(query.model)
0
       end
0
     end
0
   end
...
36
37
38
39
 
40
41
42
...
36
37
38
 
39
40
41
42
0
@@ -36,7 +36,7 @@ steps_for :using_rest_adapter do
0
   end
0
 
0
   Given("a local representation of a remote Resource") do
0
- @resource = Book.get(1)
0
+ @resource = Book.all.first
0
   end
0
   
0
   When("I try to save the Resource") do

Comments

  • dkubb Tue Jun 17 21:44:29 -0700 2008 at adapters/dm-rest-adapter/lib/rest_adapter.rb L44

    The errors you’re probably are seeing are likely due to you returning true/false here. The Adapter create() method is supposed to return an integer of the number of objects created in the store.

  • dkubb Tue Jun 17 21:48:11 -0700 2008

    BTW create, update and destroy all are supposed to return the number of objects created, updated or destroyed.

    I’m not sure how this fits in with RESTful APIs though, or if its even possible for you to know how many resources were (for example) deleted in a bulk delete situation. If it is impossible, please let me know and we can always update the API so that it can either recieve true/false or an Integer.. and if it’s true, then we assume all the deletes (or whatever) took place.