Created
August 27, 2008 16:47
-
-
Save paul/7527 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<people> | |
<person> | |
<name>Paul</name> | |
<addresses> | |
<address> | |
<city>Boulder</city> | |
<state>Colorado</state> | |
</address> | |
</addresses> | |
</person> | |
</people> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
include DataMapper::Resource | |
property :name, String | |
has n, :addresses | |
end | |
class Address | |
include DataMapper::Resource | |
property :city, String | |
property :state, String | |
belongs_to :person | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def read_many(query) | |
pp query | |
Collection.new(query) do |collection| | |
@xml.find("/people/person").each do |item| | |
person = collection.load( | |
query.fields.map do |prop| | |
text_for_elem(item, prop_to_elem(prop)) | |
end | |
) | |
a_query = Query.new(query.repository, Address) | |
a_collection = Collection.new(a_query) do |a_collection| | |
item.find("addresses/address").each do |a_xml| | |
a_collection.load( | |
a_query.fields.map do |prop| | |
text_for_elem(a_xml, prop_to_elem(prop)) | |
end | |
) | |
end | |
end | |
person.send(:addresses_association).instance_variable_set(:@children, a_collection) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment