Luke Graham
4/12/2005 12:47:00 AM
On Apr 11, 2005 5:49 PM, Robert Klemme <bob.news@gmx.net> wrote:
>
> "Luke Graham" <spoooq@gmail.com> schrieb im Newsbeitrag
> news:c6afaed005041020493e803be4@mail.gmail.com...
> > Hi list,
> >
> > Ive taken a quick look at groovy for the first time, and saw an
> > interesting idea. Using xml to build an object tree, its possible to
> > write code like this...
> >
> > <foo>
> > <bar>
> > <jim age="1"/>
> > <joe age="2"/>
> > </bar>
> > <bar/>
> > </foo>
> >
> > foo.bar.star.select { |it| it[@age] == "1" && it.parent.name == "bar"
> > && it.star.count == 0 }
> >
> > Note that foo.bar and foo.bar.star are not arrays, rather
> > transparently hide multiple elements in one object. Requesting a value
> > would be handled by the first element.
> >
> > The interesting part is that theres no quotes around the path
> > expression, its just evaluated in the context of bar. Complex
> > expressions could be broken into multiple selects and combined with
> > normal array operations.
> >
> > Im sure someones done the xml->object (method in this case) thing, but
> > has the native path trick been tried in ruby yet?
>
> REXML comes pretty close. I'm sure with a small addition like this one
> you get pretty far:
>
> class REXML::Element
> def method_missing(sym,*a,&b)
> elements[sym.to_s] or super
> end
> end
>
> Maybe you should add this to module REXML::Node.
>
> > PS. Extra kudos if someone implements not only the above, but foo.bar
> > { ... }, where foo and bar are methods...
>
> Dunno what you mean here. foo and bar have to be methods.
I have no idea what I meant either, it was late :(
--
spooq