module BSON::Symbol

Injects behaviour for encoding and decoding symbol values to and from raw bytes as specified by the BSON spec.

@note Symbols are deprecated in the BSON spec, but they are still

currently supported here for backwards compatibility.

@see bsonspec.org/#/specification

@since 2.0.0

Constants

BSON_TYPE

A symbol is type 0x0E in the BSON spec.

@since 2.0.0

Public Instance Methods

bson_type() click to toggle source

Symbols are serialized as strings as symbols are now removed from the BSON specification. Therefore the #bson_type when serializing must be a string.

@example Get the BSON type for the symbol.

:test.bson_type

@return [ String ] The single byte BSON type.

@see bsonspec.org/#/specification

@since 4.0.0

# File lib/bson/symbol.rb, line 45
def bson_type
  String::BSON_TYPE
end
to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) click to toggle source

Get the symbol as encoded BSON.

@example Get the symbol as encoded BSON.

:test.to_bson

@return [ BSON::ByteBuffer ] The buffer with the encoded object.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/symbol.rb, line 59
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  buffer.put_symbol(self)
end
to_bson_key(validating_keys = Config.validating_keys?) click to toggle source

Get the symbol as a BSON key name encoded C symbol.

@example Get the symbol as a key name.

:test.to_bson_key

@return [ String ] The encoded symbol as a BSON key.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/symbol.rb, line 73
def to_bson_key(validating_keys = Config.validating_keys?)
  if validating_keys
    raise BSON::String::IllegalKey.new(self) if BSON::String::ILLEGAL_KEY =~ self
  end
  self
end
to_bson_normalized_key() click to toggle source

Converts the symbol to a normalized key in a BSON document.

@example Convert the symbol to a normalized key.

:test.to_bson_normalized_key

@return [ String ] The symbol as a non interned string.

@since 3.0.0

# File lib/bson/symbol.rb, line 88
def to_bson_normalized_key
  to_s
end