Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing for order! to be a string key #44

Merged
merged 1 commit into from
Aug 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/gyoku/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions spec/gyoku/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<findUser><id>666</id><name>Lucy</name></findUser>"
expect(to_xml(hash)).to eq(result)

hash = { :find_user => { :mname => "in the", :lname => "Sky", :fname => "Lucy", 'order!' => [:fname, :mname, :lname] } }
result = "<findUser><fname>Lucy</fname><mname>in the</mname><lname>Sky</lname></findUser>"
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 = "<findUser><id>666</id><name>Lucy</name><order>value</order></findUser>"
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]")
Expand Down