While I have your attention
2007-03-07 17:32:00 -0500I built a Ruby module for a class recently that’s designed to allow for flexible de/serialization of XML documents into and out of classes. The syntax looks something like this:
class Foo
include XmlSerializable
xml_element_name "foo-element"
xml_has_attributes :id => Integer, :name => String
end
class Bar
include XmlSerializable
xml_element_name "bar-element"
xml_has_children [:foos, [Foo]]
end
For convenience, and because I am both peachy keen and silky smooth:
# Generates a constructor and equality methods.
class Baz < XmlStruct
xml_element_name "baz-element"
xml_has_attributes :you_get => Integer, :the_idea => Float
end
The syntax is a little bit odd because of the constraints placed on us for this particular assignment (some very strange, arbitrary, not-really-XML grammar definitions) which is why the children in the example above are defined in an ordered list rather than a hash or something. But that’s easily cleaned up.
I’m thinking of releasing it as a gem, which is not something to which I’m accustomed. Would this be useful to anyone? It’d be cool to be able to define the deserialization grammar and the actions on a class simultaneously–say, for an RssFeed class, with RssItem children. I mean, not that there isn’t already an RSS gem. Because there is. But the idea of it makes me giddy, very much like a schoolchild of either gender.