This commit is contained in:
Kyle Banker 2010-03-31 15:16:12 -04:00
parent a07662a898
commit 40b481ad3c
2 changed files with 16 additions and 3 deletions

View File

@ -34,13 +34,15 @@ module Mongo
# Create a buffer for storing binary data in MongoDB.
#
# @param [Array] initia_data
# @param [Array, String] data to story as BSON binary. If a string is given, the value will be
# concerted to an array of bytes using String#unpack("c*").
# @param [Fixnum] one of four values specifying a BSON binary subtype. Possible values are
# SUBTYPE_BYTES, SUBTYPE_UUID, SUBTYPE_MD5, and SUBTYPE_USER_DEFINED.
#
# @see http://www.mongodb.org/display/DOCS/BSON#BSON-noteondatabinary BSON binary subtypes.
def initialize(initial_data=[], subtype=SUBTYPE_BYTES)
super(initial_data)
def initialize(data=[], subtype=SUBTYPE_BYTES)
data = data.unpack("c*") if data.is_a?(String)
super(data)
@subtype = subtype
end

View File

@ -249,6 +249,17 @@ class BSONTest < Test::Unit::TestCase
assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
end
def test_binary_with_string
b = Binary.new('somebinarystring')
doc = {'bin' => b}
bson = Mongo::BSON_CODER.serialize(doc)
doc2 = Mongo::BSON_CODER.deserialize(bson)
bin2 = doc2['bin']
assert_kind_of Binary, bin2
assert_equal 'somebinarystring', bin2.to_s
assert_equal Binary::SUBTYPE_BYTES, bin2.subtype
end
def test_binary_type
bin = Binary.new([1, 2, 3, 4, 5], Binary::SUBTYPE_USER_DEFINED)