class BSON::Symbol::Raw

Public Class Methods

new(str_or_sym) click to toggle source

Create a BSON Symbol

@param [ String | Symbol ] str_or_sym The symbol represented by this

object. Can be specified as a Symbol or a String.

@see bsonspec.org/#/specification

# File lib/bson/symbol.rb, line 111
def initialize(str_or_sym)
  unless str_or_sym.is_a?(String) || str_or_sym.is_a?(Symbol)
    raise ArgumentError, "BSON::Symbol::Raw must be given a symbol or a string, not #{str_or_sym}"
  end

  @symbol = str_or_sym.to_sym
end

Public Instance Methods

==(other) click to toggle source

Check equality of the raw bson symbol against another.

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

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

# File lib/bson/symbol.rb, line 138
def ==(other)
  return false unless other.is_a?(Raw)
  to_sym == other.to_sym
end
Also aliased as: eql?
as_extended_json(**options) click to toggle source

Converts this object to a representation directly serializable to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json.rst).

This method returns the integer value if relaxed representation is requested, otherwise a $numberLong hash.

@option options [ true | false ] :relaxed Whether to produce relaxed

extended JSON representation.

@return [ Hash | Integer ] The extended json representation.

# File lib/bson/symbol.rb, line 169
def as_extended_json(**options)
  {'$symbol' => to_s}
end
bson_type() click to toggle source
# File lib/bson/symbol.rb, line 155
def bson_type
  Symbol::BSON_TYPE
end
eql?(other)
Alias for: ==
to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?) click to toggle source

Get the symbol as encoded BSON.

@raise [ EncodingError ] If the symbol is not UTF-8.

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

@see bsonspec.org/#/specification

# File lib/bson/symbol.rb, line 151
def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  buffer.put_string(to_s)
end
to_s() click to toggle source

Get the underlying symbol as a Ruby string.

@return [ String ] The symbol as a string.

# File lib/bson/symbol.rb, line 129
def to_s
  @symbol.to_s
end
to_sym() click to toggle source

Get the underlying symbol as a Ruby symbol.

@return [ Symbol ] The symbol represented by this BSON object.

# File lib/bson/symbol.rb, line 122
def to_sym
  @symbol
end