I need a Class which has an semi-automatic 'to_s' method (to generate XML in fact). I would like to iterate through all the automatic methods set up in my 'attr_accessor' line:
class MyClass
attr_accessor :id,:a,:b,:c
end
c=MyClass.new
So far I'm doing a basic:
c.methods - Object.methods
=> ["b", "b=", "c", "c=", "id=", "a", "a="]
I am facing a few challenges:
- 'id' may cause a slight headache - as Object already seems to have an 'id'.
- The 'c.methods' call above, returns Strings - I'm not getting any other meta-data ? (In Java 'method' is an object, where I could perform further reflection).
- I have one-to-many relationships I have to deal with ('c' is an array type of other object types).
This is what I'm trying to do: I want to design a simple Object which has a 'to_s' which will build up an XML fragment: for instance.
<id> 1 </id>
<a> Title </a>
<b> Stuff </b>
<c>
<x-from-other-object>
<x-from-other-object>
....
</c>
And then inherit my data-classes from that simple object: so that (hopefully) I get a mechansim to build up an entire XML doc.
I'm sure I'm re-inventing the wheel here as well...so other tried-and-tested approaches welcome.