Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RUBY-434 Queries should return a Hash w/ Indifferent Access

  • Loading branch information...
commit 2d2ac087dac2682f896e2c33214d31e1e38f48f4 1 parent a00f06c
@gjmurakami-10gen gjmurakami-10gen authored
View
1  Gemfile
@@ -5,6 +5,7 @@ group :development, :test do
gem "bundler"
gem "rake"
gem "json"
+ gem "activesupport"
# Deployment
gem "git"
View
2  lib/bson/ordered_hash.rb
@@ -22,7 +22,7 @@
# Hash already keeps its keys ordered by order of insertion.
module BSON
- class OrderedHash < Hash
+ class OrderedHash < HashWithIndifferentAccess
def ==(other)
begin
View
2  lib/mongo.rb
@@ -50,6 +50,8 @@ module Constants
end
end
+require 'active_support/core_ext/hash/indifferent_access'
+
require 'bson'
require 'mongo/util/conversions'
View
15 test/bson/hash_with_indifferent_access_test.rb
@@ -1,6 +1,5 @@
# encoding:utf-8
require 'test_helper'
-require 'support/hash_with_indifferent_access'
class HashWithIndifferentAccessTest < Test::Unit::TestCase
include BSON
@@ -35,4 +34,18 @@ def test_embedded_document
bson = @encoder.serialize(person)
assert_equal person, @encoder.deserialize(bson.to_s)
end
+
+ def test_deserialize_returns_hash_with_indifferent_access
+ doc = {:a => 1, 'b' => 2, :c => {:d => 4, 'e' => 5}}
+ bson = @encoder.serialize(doc)
+ hash = @encoder.deserialize(bson.to_s)
+ assert_equal(1, hash['a'])
+ assert_equal(1, hash[:a])
+ assert_equal(2, hash['b'])
+ assert_equal(2, hash[:b])
+ assert_equal(4, hash['c']['d'])
+ assert_equal(4, hash[:c][:d])
+ assert_equal(5, hash['c']['e'])
+ assert_equal(5, hash[:c][:e])
+ end
end
View
8 test/bson/ordered_hash_test.rb
@@ -104,7 +104,7 @@ def test_replace
h2[:d] = 4
h1.replace(h2)
- assert_equal [:c, :d], h1.keys
+ assert_equal ["c", "d"], h1.keys # HashWithIndifferentAccess converts keys to strings
assert_equal [3, 4], h1.values
assert h1.keys.object_id != h2.keys.object_id
end
@@ -178,9 +178,13 @@ def test_equality_with_hash
o[:a] = 1
o[:b] = 2
o[:c] = 3
- r = {:a => 1, :b => 2, :c => 3}
+ r = HashWithIndifferentAccess.new(:a => 1, :b => 2, :c => 3)
assert r == o
assert o == r
+ # this appears to be problematic
+ #q = HashWithIndifferentAccess[:a => 1, :b => 2, :c => 3]
+ #assert q == o
+ #assert o == q
end
def test_update
Please sign in to comment.
Something went wrong with that request. Please try again.