From 3985a948f2fdacb1429c9ad18b1ea093a772d9a9 Mon Sep 17 00:00:00 2001 From: Hugo Corbucci Date: Fri, 25 Jul 2014 15:51:33 -0500 Subject: [PATCH] Allowing for order! to be a string key When using savon to issue SOAP requests in a background process, the request parameters (a hash) get saved by resque in redis. When pulled out of redis, all keys of the hash are stringified as they are marshalled as strings. In order to avoid ordering issues when using resque or sidekick, gyoku will accept both the symbol or the string version of the order! key and respect that order. --- lib/gyoku/hash.rb | 2 +- spec/gyoku/hash_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/gyoku/hash.rb b/lib/gyoku/hash.rb index eb67705..a625c13 100644 --- a/lib/gyoku/hash.rb +++ b/lib/gyoku/hash.rb @@ -66,7 +66,7 @@ def self.iterate_with_xml(hash) # Defaults to return the actual keys of the Hash if no :order! key could be found. # Raises an ArgumentError in case the :order! Array does not match the Hash keys. def self.order(hash) - order = hash[:order!] + order = hash[:order!] || hash.delete('order!') hash_without_order = hash.reject { |key, value| key == :order! } order = hash_without_order.keys unless order.kind_of? ::Array diff --git a/spec/gyoku/hash_spec.rb b/spec/gyoku/hash_spec.rb index 6e988be..a6fc929 100644 --- a/spec/gyoku/hash_spec.rb +++ b/spec/gyoku/hash_spec.rb @@ -112,6 +112,22 @@ def singleton.to_datetime expect(to_xml(hash)).to eq(result) end + it "preserves the order of Hash keys and values specified through 'order!' (as a string key)" do + hash = { :find_user => { :name => "Lucy", :id => 666, 'order!' => [:id, :name] } } + result = "666Lucy" + expect(to_xml(hash)).to eq(result) + + hash = { :find_user => { :mname => "in the", :lname => "Sky", :fname => "Lucy", 'order!' => [:fname, :mname, :lname] } } + result = "Lucyin theSky" + expect(to_xml(hash)).to eq(result) + end + + it "uses :order! symbol values for ordering but leaves the string key 'order!' if both are present" do + hash = { :find_user => { :name => "Lucy", :id => 666, 'order!' => 'value', :order! => [:id, :name, 'order!'] } } + result = "666Lucyvalue" + expect(to_xml(hash)).to eq(result) + end + it "raises if the :order! Array is missing Hash keys" do hash = { :name => "Lucy", :id => 666, :order! => [:name] } expect { to_xml(hash) }.to raise_error(ArgumentError, "Missing elements in :order! [:id]")