happymapper icon indicating copy to clipboard operation
happymapper copied to clipboard

Feature: HappyMapper.model(XML)

Open burtlo opened this issue 13 years ago • 3 comments

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

burtlo avatar Sep 24 '12 15:09 burtlo

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.

benoist avatar Nov 01 '12 20:11 benoist

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.

Confusion avatar Dec 04 '12 16:12 Confusion

Checkout this XML Schema to Ruby generator: https://github.com/sshaw/jaxb2ruby

sshaw avatar Jul 05 '14 19:07 sshaw