Skip to content

attributes serialized in random sequence #3

Open

Description

@moabrichard

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)

Activity

  1. ghost assigned on Nov 23, 2011
  2. burtlo commented on Nov 23, 2011

    @burtlo
    Owner

    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.

  3. moabrichard commented on Nov 23, 2011

    @moabrichard
    Author

    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:
    #3 (comment)

  4. burtlo commented on Nov 25, 2011

    @burtlo
    Owner

    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.

  5. moabrichard commented on Dec 6, 2011

    @moabrichard
    Author

    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

  6. burtlo commented on Dec 6, 2011

    @burtlo
    Owner

    That sounds like a good substitution. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions