how to define recursive structure

28 views
Skip to first unread message

Jason McLaren

unread,
Apr 18, 2012, 8:44:48 PM4/18/12
to happy...@googlegroups.com
I'm trying to map a slightly recursive (3 layers max) XML structure that looks like this (apologies for any typos):

<order>
<line-items>
<line-item>
<sub-items>
<line-item>
<sub-items>
<line-item/>
</sub-items>
</line-item>
</sub-items>
</line-item>
</line-items>
</order>

I'm not sure how to define the ruby classes. It seems as if you have to define the line-items class before you can refer to it in the sub-items class definition, and vice versa. Here's what I've tried:

module Foo
class LineItem
include HappyMapper
tag 'line-item'
has_one :subitems, SubItems
end

class LineItems
include HappyMapper
tag 'line-items'
has_many :'line-item', LineItem
end

class SubItems
include HappyMapper
tag 'sub-items'
has_many :'line-item', LineItem
end

class Order
include HappyMapper
tag 'order'
has_one :line_items, LineItems
end
end

But of course this fails at runtime with "uninitialized constant APE::LineItem::SubItems (NameError)".

Is it possible to define recursive structures like this with HappyMapper? It would be great to be able to scope the line-items so they're not all in one flat list (which is what happens if you leave out sub-items).

Any pointers appreciated! I've read the docs and some of the source code but couldn't puzzle this one out.

Reply all
Reply to author
Forward
0 new messages