lib/lazy_resource/mapping.rb in lazy_resource-0.3.2 vs lib/lazy_resource/mapping.rb in lazy_resource-0.3.3
- old
+ new
@@ -31,30 +31,37 @@
if objects.is_a?(Array)
Relation.new(self, :fetched => true).tap do |relation|
relation.load(objects)
end
else
- if self.root_node_name && objects.key?(self.root_node_name.to_s)
- self.load(objects.delete(self.root_node_name.to_s)).tap do |obj|
+ if mapped_name = self.mapped_root_node_name(objects)
+ self.load(objects.delete(mapped_name)).tap do |obj|
obj.other_attributes = objects
end
else
self.new.load(objects)
end
end
end
+
+ def mapped_root_node_name(objects)
+ if self.root_node_name
+ root_node_names = self.root_node_name.is_a?(Array) ? self.root_node_name : [self.root_node_name]
+ mapped_name = (root_node_names.map(&:to_s) & objects.keys).first
+ end
+ end
end
def load(hash, persisted=true)
hash.fetched = true and return hash if hash.kind_of?(LazyResource::Mapping)
self.tap do |resource|
resource.persisted = persisted
resource.fetched = false
- if resource.class.root_node_name && hash.key?(resource.class.root_node_name.to_s)
+ if mapped_name = resource.class.mapped_root_node_name(hash)
other_attributes = hash
- hash = other_attributes.delete(resource.class.root_node_name.to_s)
+ hash = other_attributes.delete(mapped_name)
self.other_attributes = other_attributes
end
hash.each do |name, value|
attribute = self.class.attributes[name.to_sym]