Module: BSON::Symbol

Defined in:
lib/bson/symbol.rb

Overview

Note:

Symbols are deprecated in the BSON spec, but they are still currently supported here for backwards compatibility.

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

See Also:

Since:

  • 2.0.0

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

BSON_TYPE =

A symbol is type 0x0E in the BSON spec.

Since:

  • 2.0.0

14.chr.force_encoding(BINARY).freeze

Instance Method Summary collapse

Instance Method Details

#bson_typeString

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

Examples:

Get the BSON type for the symbol.

:test.bson_type

Returns:

  • (String)

    The single byte BSON type.

See Also:

Since:

  • 4.0.0



45
46
47
# File 'lib/bson/symbol.rb', line 45

def bson_type
  String::BSON_TYPE
end

#to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) ⇒ BSON::ByteBuffer

Get the symbol as encoded BSON.

Examples:

Get the symbol as encoded BSON.

:test.to_bson

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.

See Also:

Since:

  • 2.0.0



59
60
61
# 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?) ⇒ String

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

Examples:

Get the symbol as a key name.

:test.to_bson_key

Returns:

  • (String)

    The encoded symbol as a BSON key.

See Also:

Since:

  • 2.0.0



73
74
75
76
77
78
# 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_keyString

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

Examples:

Convert the symbol to a normalized key.

:test.to_bson_normalized_key

Returns:

  • (String)

    The symbol as a non interned string.

Since:

  • 3.0.0



88
89
90
# File 'lib/bson/symbol.rb', line 88

def to_bson_normalized_key
  to_s
end