Permalink
Browse files

Rails 3 HashWithIndifferentAccess fixes

  • Loading branch information...
banker committed Mar 2, 2010
1 parent 2251d8f commit 0709b9808276981eb3c1bf60e5f909e745704aed
Showing with 20 additions and 15 deletions.
  1. +1 −1 ext/cbson/cbson.c
  2. +19 −14 test/bson_test.rb
View
@@ -495,7 +495,7 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_
}
else {
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) &&
(rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue)) {
VALUE oid_sym = rb_hash_delete(hash, id_sym);
View
@@ -4,11 +4,18 @@
require 'bigdecimal'
require 'rational'
-# Need to simulating this class
-# without actually loading it.
-module ActiveSupport
- class TimeWithZone
+begin
+ 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
+ class TimeWithZone
+ end
end
+ Zone = ActiveSupport::TimeWithZone.new
end
class BSONTest < Test::Unit::TestCase
@@ -191,7 +198,7 @@ def test_date_before_epoch
end
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}
begin
bson = BSON.serialize(doc)
@@ -433,21 +440,19 @@ def test_move_id_with_nested_doc
BSON.serialize(c, false, false).to_s
end
- begin
- require 'active_support'
- rescue LoadError
- warn 'Could not test BSON with HashWithIndifferentAccess.'
- end
-
if defined?(HashWithIndifferentAccess)
def test_keep_id_with_hash_with_indifferent_access
doc = HashWithIndifferentAccess.new
- doc[:_id] = ObjectID.new
- BSON.serialize(doc, false, false).to_a
+ embedded = HashWithIndifferentAccess.new
+ 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['embedded'][0].has_key?("_id")
doc['_id'] = ObjectID.new
- BSON.serialize(doc, false, false).to_a
+ BSON.serialize(doc, false, true).to_a
assert doc.has_key?("_id")
end
end

0 comments on commit 0709b98

Please sign in to comment.