Ruby ROXML - how to get an array to render its xml? -


I am trying to create a message object that is the successor of Array, will gather a group of message message objects. I am trying to create an XML output with ROXML which looks like this:

  & lt; Message & gt; & Lt; Message & gt; & Lt; Type & gt; & Lt; / Type & gt; & Lt; Code & gt; & Lt; / Code & gt; & Lt; Body & gt; & Lt; / Body & gt; & Lt; / Messages & gt; ... & lt; / Messages & gt;  

However, how can I understand the message object how to display in the XML in the message object? Here is the code I am working on:

  Required ROXML xml_accessor: type xml_accessor: code xml_accessor: body end class message in the 'Roxmal' class message & lt; Roux in Array # I think this is a problem - but how do I tell Rox ML that the # messages are in this example of the array? Xml_accessor: Message as, in & gt; [Message] Add DEF (message) itself & lt; & Lt; Message End and Message = Meansense New message Type = "error" table code = "1234" message. Ford = "This is a test message." Messages = messages.new messages.add message messages. Length messages.to_xml returns  

this output:

  1 & lt; Messages / & gt;  

So, the message I added to the message is not being displayed. Any one have any ideas? Or am I going wrong about this?

Thanks for any help.

I do not think what you want is possible in any way you array are trying to gain access to the internal state of the class, which is not only impossible because most implement those internal c / c ++ / java / .net / objective-c / abp runtime but also quite bad Ideas and Bad Object-Oriented Design

This is, message is not actually a array hence it should not be inherited by the array . Tell me: Are you really 100% sure that your message class all array is able to fulfill the contract of 81 methods honestly ? And when assoc , rassoc , rindex and transfer also mean , when applicable Message

You would be better than using a delegation instead of heritage here. It gives you a well-named unit that requires 'forwardable' for xml_accessor :

 , the 'Roxmal' category is required Forwarding includes ROXML class & lt; & Lt; Self; Alias_method: [],: new end xml_reader: message, as in => [Message] DEF start (* message) @ messages = message end def_delegators: message, length,: & lt; & Lt; End  

Note: I have changed some other things here too. For example, I personally believe that after an creation one object should be valid and usable. In your version of the code, a message is basically invalid after it's created and only after calling type = , code = Validate and body = Sets:

  The ROXML class in the class message & lt; & Lt; Self; Alias_method: [],: New end xml_reader: type, body xml_reader: code ,: as = & gt; Integer def start (type = zero, code = zero, body = zero) @ type, @ code, @body = case opts = type when hash option [: type], opts [: code], opts [: body] else Type, code, body end and end  

Here are a few extended uses examples:

  msgs = message [message ['error', 1234, 'this A test message. '], Message []] msgs & lt; & Lt; Message [Type: 'Warning', Code: 4815162342, Body: 'This is another test message.' ] Says msgs.to_xml # = & gt; & Lt; Message & gt; # = & Gt; & Lt; Message & gt; # = & Gt; & Lt; Type & gt; Error & lt; / Type & gt; # = & Gt; & Lt; Body & gt; This is a test message. & Lt; / Body & gt; # = & Gt; & Lt; Code & gt; 1234 & lt; / Code & gt; # = & Gt; & Lt; / Messages & gt; # = & Gt; & Lt; Message /> # = & Gt; & Lt; Message & gt; # = & Gt; & Lt; Type & gt; Warning & lt; / Type & gt; # = & Gt; & Lt; Body & gt; This is another test message. & Lt; / Body & gt; # = & Gt; & Lt; Code & gt; 4815162342 & lt; / Code & gt; # = & Gt; & Lt; / Messages & gt; # = & Gt; & Lt; / Messages & gt;  

Comments