Skip to content

Commit

Permalink
Merge pull request #121 from romanbsd/master
Browse files Browse the repository at this point in the history
Serialize ActiveSupport::Multibyte::Chars as String
  • Loading branch information
TylerBrock committed Oct 6, 2012
2 parents b51ca06 + eea45da commit e470ab3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
10 changes: 10 additions & 0 deletions ext/cbson/cbson.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,16 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
rb_raise(InvalidDocument, "Cannot serialize the Numeric type %s as BSON; only Bignum, Fixnum, and Float are supported.", cls);
break;
}
if (strcmp(cls, "ActiveSupport::Multibyte::Chars") == 0) {
int length;
VALUE str = StringValue(value);
write_name_and_type(buffer, key, 0x02);
length = RSTRING_LENINT(str) + 1;
SAFE_WRITE(buffer, (char*)&length, 4);
write_utf8(buffer, str, 0);
SAFE_WRITE(buffer, &zero, 1);
break;
}
bson_buffer_free(buffer);
rb_raise(InvalidDocument, "Cannot serialize an object of class %s into BSON.", cls);
break;
Expand Down
4 changes: 3 additions & 1 deletion lib/bson/bson_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,9 @@ def bson_type(o)
raise InvalidDocument, "#{o.class} is not currently supported; " +
"use a UTC Time instance instead."
else
if defined?(ActiveSupport::TimeWithZone) && o.is_a?(ActiveSupport::TimeWithZone)
if defined?(ActiveSupport::Multibyte::Chars) && o.is_a?(ActiveSupport::Multibyte::Chars)
STRING
elsif defined?(ActiveSupport::TimeWithZone) && o.is_a?(ActiveSupport::TimeWithZone)
raise InvalidDocument, "ActiveSupport::TimeWithZone is not currently supported; " +
"use a UTC Time instance instead."
else
Expand Down
23 changes: 22 additions & 1 deletion test/bson/bson_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ def initialize(utc_time, zone)
Zone = ActiveSupport::TimeWithZone.new(Time.now.utc, 'EST')
end

begin
require 'active_support/multibyte/chars'
rescue LoadError
warn 'Mocking ActiveSupport::Multibyte::Chars'
module ActiveSupport
module Multibyte
class Chars < String
end
end
end
end

class BSONTest < Test::Unit::TestCase

include BSON
Expand Down Expand Up @@ -70,6 +82,15 @@ def test_valid_utf8_string
assert_doc_pass(doc)
end

def test_valid_active_support_multibyte_chars
doc = {'doc' => ActiveSupport::Multibyte::Chars.new('aé')}
assert_doc_pass(doc)

bson = @encoder.serialize(doc)
doc = @encoder.deserialize(bson)
assert_equal doc['doc'], 'aé'
end

def test_valid_utf8_key
doc = {'aé' => 'hello'}
assert_doc_pass(doc)
Expand Down Expand Up @@ -287,7 +308,7 @@ def test_date_returns_as_utc
end

def test_date_before_epoch
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ then return true end
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ then return true end
begin
doc = {'date' => Time.utc(1600)}
bson = @encoder.serialize(doc)
Expand Down

0 comments on commit e470ab3

Please sign in to comment.