Parent

Namespace

Included Modules

BSON::Binary

Represents binary data.

@see bsonspec.org/#/specification

@since 2.0.0

Constants

BSON_TYPE

A binary is type 0x05 in the BSON spec.

@since 2.0.0

SUBTYPES

The mappings of subtypes to their single byte identifiers.

@since 2.0.0

TYPES

The mappings of single byte subtypes to their symbol counterparts.

@since 2.0.0

Attributes

data[R]

@!attribute data

@return [ Object ] The raw binary data.
@since 2.0.0

@!attribute type

@return [ Symbol ] The binary type.
@since 2.0.0
type[R]

@!attribute data

@return [ Object ] The raw binary data.
@since 2.0.0

@!attribute type

@return [ Symbol ] The binary type.
@since 2.0.0

Public Class Methods

from_bson(bson) click to toggle source

Deserialize the binary data from BSON.

@param [ BSON ] bson The bson representing binary data.

@return [ Binary ] The decoded binary data.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/binary.rb, line 126
def self.from_bson(bson)
  length = Int32.from_bson(bson)
  type = TYPES[bson.read(1)]
  length = Int32.from_bson(bson) if type == :old
  data = bson.read(length)
  new(data, type)
end
new(data = "", type = :generic) click to toggle source

Instantiate the new binary object.

@example Instantiate a binary.

BSON::Binary.new(data, :md5)

@param [ Object ] data The raw binary data. @param [ Symbol ] type The binary type.

@since 2.0.0

# File lib/bson/binary.rb, line 93
def initialize(data = "", type = :generic)
  validate_type!(type)
  @data = data
  @type = type
end

Public Instance Methods

==(other) click to toggle source

Determine if this binary object is equal to another object.

@example Check the binary equality.

binary == other

@param [ Object ] other The object to compare against.

@return [ true, false ] If the objects are equal.

@since 2.0.0

# File lib/bson/binary.rb, line 67
def ==(other)
  return false unless other.is_a?(Binary)
  type == other.type && data == other.data
end
as_json(*args) click to toggle source

Get the binary as JSON hash data.

@example Get the binary as a JSON hash.

binary.as_json

@return [ Hash ] The binary as a JSON hash.

@since 2.0.0

# File lib/bson/binary.rb, line 80
def as_json(*args)
  { "$binary" => data, "$type" => type }
end
to_bson(encoded = ''.force_encoding(BINARY)) click to toggle source

Encode the binary type

@example Encode the binary.

binary.to_bson

@return [ String ] The encoded binary.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/binary.rb, line 109
def to_bson(encoded = ''.force_encoding(BINARY))
  encode_binary_data_with_placeholder(encoded) do |encoded|
    encoded << SUBTYPES.fetch(type)
    encoded << data.bytesize.to_bson if type == :old
    encoded << data
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.