0
@@ -28,13 +28,19 @@ module DataMapper
0
# Creates a new resource in the specified repository.
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
+ updated_resource = parse_resource(result.body, resource.class)
0
+ resource.id = updated_resource.id
0
# TODO: We're not using the response to update the DataMapper::Resource with the newly acquired ID!!!
+ success
0
@@ -59,6 +65,7 @@ module DataMapper
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
- res = parse_resource(data,
resource_name, query.model, query.fields)
0
+ res = parse_resource(data,
query.model)
0
@@ -86,6 +93,7 @@ module DataMapper
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
- parse_resources(data,
resource_name, query.model, query.fields)
0
+ parse_resources(data,
query.model)
0
# TODO: Raise error if cannot reach server
0
@@ -136,41 +144,46 @@ module DataMapper
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
- resource.send("#{Inflection.underscore(
dm_property.name)}=", field_element.text) if dm_property0
+ resource.send("#{Inflection.underscore(
attribute[0])}=", field_element.text) if attribute0
+ resource.instance_eval { @new_record= false }
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
- 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
+
resource_name = resource_name_from_model dm_model_class0
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
+ def resource_name_from_model(model)
0
+ Inflection.underscore(model.name.downcase)
0
def resource_name_from_query(query)
0
-
Inflection.underscore(query.model.name.downcase)
0
+
resource_name_from_model(query.model)
Comments
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.
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.