Rails 3 HashWithIndifferentAccess fixes

This commit is contained in:
Kyle Banker 2010-03-02 11:11:07 -05:00
parent 2251d8f61b
commit 0709b98082
2 changed files with 20 additions and 15 deletions

View File

@ -495,7 +495,7 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_
} }
else { else {
allow_id = 1; allow_id = 1;
if (strcmp(rb_class2name(RBASIC(hash)->klass), "HashWithIndifferentAccess") != 0) { if (strcmp(rb_class2name(RBASIC(hash)->klass), "Hash") == 0) {
if ((rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) && if ((rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) &&
(rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue)) { (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue)) {
VALUE oid_sym = rb_hash_delete(hash, id_sym); VALUE oid_sym = rb_hash_delete(hash, id_sym);

View File

@ -4,12 +4,19 @@ require 'complex'
require 'bigdecimal' require 'bigdecimal'
require 'rational' require 'rational'
# Need to simulating this class begin
# without actually loading it. require 'active_support/core_ext'
require 'active_support/hash_with_indifferent_access'
Time.zone = "Pacific Time (US & Canada)"
Zone = Time.zone.now
rescue LoadError
warn 'Could not test BSON with HashWithIndifferentAccess.'
module ActiveSupport module ActiveSupport
class TimeWithZone class TimeWithZone
end end
end end
Zone = ActiveSupport::TimeWithZone.new
end
class BSONTest < Test::Unit::TestCase class BSONTest < Test::Unit::TestCase
@ -191,7 +198,7 @@ class BSONTest < Test::Unit::TestCase
end end
def test_exeption_on_using_unsupported_date_class def test_exeption_on_using_unsupported_date_class
[DateTime.now, Date.today, ActiveSupport::TimeWithZone.new].each do |invalid_date| [DateTime.now, Date.today, Zone].each do |invalid_date|
doc = {:date => invalid_date} doc = {:date => invalid_date}
begin begin
bson = BSON.serialize(doc) bson = BSON.serialize(doc)
@ -433,21 +440,19 @@ class BSONTest < Test::Unit::TestCase
BSON.serialize(c, false, false).to_s BSON.serialize(c, false, false).to_s
end end
begin
require 'active_support'
rescue LoadError
warn 'Could not test BSON with HashWithIndifferentAccess.'
end
if defined?(HashWithIndifferentAccess) if defined?(HashWithIndifferentAccess)
def test_keep_id_with_hash_with_indifferent_access def test_keep_id_with_hash_with_indifferent_access
doc = HashWithIndifferentAccess.new doc = HashWithIndifferentAccess.new
doc[:_id] = ObjectID.new embedded = HashWithIndifferentAccess.new
BSON.serialize(doc, false, false).to_a embedded['_id'] = ObjectID.new
doc['_id'] = ObjectID.new
doc['embedded'] = [embedded]
BSON.serialize(doc, false, true).to_a
assert doc.has_key?("_id") assert doc.has_key?("_id")
assert doc['embedded'][0].has_key?("_id")
doc['_id'] = ObjectID.new doc['_id'] = ObjectID.new
BSON.serialize(doc, false, false).to_a BSON.serialize(doc, false, true).to_a
assert doc.has_key?("_id") assert doc.has_key?("_id")
end end
end end