happymapper icon indicating copy to clipboard operation
happymapper copied to clipboard

attributes serialized in random sequence

Open moabrichard opened this issue 14 years ago • 5 comments

You are using Nokogiri builder to create the XML output, which as you know, uses a Hash to pass in attribute key/value pairs when adding a new element. I am using Ruby 1.8.7, which (unlike Ruby 1.9.3) does not retain the insertion order of the items that are added to a Hash object. Therefore the to_xml method is currently outputting xml in which the attribute sequence is somewhat abitratry.

In my installation of unhappymapper, I altered this behavior by installing the new activemodel gem, then added require "active_model" at the top of the file, and replaced the line: attributes = Hash[ *attributes ] with attributes = ActiveSupport::OrderedHash[ *attributes ]

With this change the attributes are output in the sequence they appear in the Ruby class.

(I know, I know -- the XML spec says that attribute order is supposed to be arbitrary, but I like my xml to be consistent)

moabrichard avatar Nov 23 '11 19:11 moabrichard

I can understand the desire to have the output ordered particularly if you are expected children in some particular order.

Perhaps there is a way that I can provide an option that ensures the ordering in Ruby 1.8.7.

burtlo avatar Nov 23 '11 21:11 burtlo

Hello Franklin,

Thank you for your prompt response, and also for having added the to_xml functionality to happymapper. I was shocked to realize that neither of the forks you have derived from had included a marshalling capability.

Note that only the output sequence of attributes is affected. Elements are created in the same order as they appear in the class. I appreciate your having a look at this, and appreciate the desire to avoid creating a dependency on a huge library like ActiveModel

--Richard

On 11/23/11 2:22 PM, Franklin Webber wrote:

I can understand the desire to have the output ordered particularly if you are expected children in some particular order.

Perhaps there is a way that I can provide an option that ensures the ordering in Ruby 1.8.7.


Reply to this email directly or view it on GitHub: https://github.com/burtlo/happymapper/issues/3#issuecomment-2855025

moabrichard avatar Nov 23 '11 21:11 moabrichard

I was a bit surprised but realized that most of the uses for this library was to just get the XML into the application and perhaps use the Rails XML functionality or Builder to get the data back out. I, of course, had need for it in other ways because of needing to get the ugly XML back out.

Yeah this would cause namespaces and attributes to be out-of-order. Perhaps if it is very important we could just included an ordered hash for those in 1.8.7 land that have your needs.

burtlo avatar Nov 25 '11 07:11 burtlo

FYI the Hashery gem (http://rubygems.org/gems/hashery) contains an OrderedHash class derived from the same class in the ActiveSupport library. This allows introducing such a replacement for Hash without all the overhead of ActiveSupport

moabrichard avatar Dec 06 '11 20:12 moabrichard

That sounds like a good substitution. Thanks.

burtlo avatar Dec 06 '11 20:12 burtlo