Feature: HappyMapper.model(XML)
HappyMapper model generator
Currently the most tedious part of HappyMapper is attempting to model the XML data as a HappyMapper class. After that it becomes a fairly enjoyable experience. What if we had a method that could generate for you the source code from a specified XML input.
Within code you would write:
model_text = HappyMapper.model(ADDRESS_XML_DATA)
puts model_text
This would output:
class Address
include HappyMapper
has_one :street, String
has_one :postcode, String
has_one :housenumber, String
has_one :city, String
has_one :country, 'Country'
end
class Country
include HappyMapper
content :content, String
attribute :code, String
end
Perhaps this would be most useful as a command-line application:
$ happymapper address.xml > address.rb
This would be a nice generator to get you started. Perhaps a generator based on XSD input could even make it possible to change the String in to the correct type.
A generator that works for specific XSD's is not that hard (I've written one using Nokogiri and xpath expressions, with some inspiration from Wasabi), estimating it would take me less time than writing the classes by hand), but if you want to be able to handle any XSD, I think it becomes a pretty large task.
A parser based on XML examples may be more useful for most people.
Checkout this XML Schema to Ruby generator: https://github.com/sshaw/jaxb2ruby