mvz / happymapper

Object to XML mapping library, using Nokogiri (Fork from John Nunemaker's Happymapper)
http://github.com/mvz/happymapper/
MIT License
151 stars 44 forks source link

Pass options into wrapping element #225

Closed jbennett closed 1 year ago

jbennett commented 1 year ago

I’m working with some XML that has a wrapping element with the same name as what I want for the inner element:

<foo>
  <bars>
    <bar id="1" />
    <bar id="2" />
    <bar id="3" />
  </bars>
</foo>
class Foo
  include HappyMappy

  wrap "bars" do
    has_many :bars, Bar # this doesn’t work
  end
end

Passing the options through lets me give the wrapper a different name:

class Foo
  include HappyMappy

  wrap "bars_wrapper", tag: "bars" do
    has_many :bars, Bar
  end
end

This allows access to the wrapper via foo.bars_wrapper and foo.bars directly

mvz commented 1 year ago

Looks good. I can't believe there's no shortcut yet for this very common type of structure!