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 108
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 135
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).

@return [ Hash ] The extended json representation.

# File lib/bson/symbol.rb, line 172
def as_extended_json(**_options)
  { '$symbol' => to_s }
end
as_json(*args) click to toggle source

Return a string representation of the raw symbol for use in application-level JSON serialization. This method is intentionally different from as_extended_json.

@example Get the raw symbol as a JSON-serializable object.

raw_symbol.as_json

@return [ String ] The raw symbol as a String.

# File lib/bson/symbol.rb, line 164
def as_json(*args)
  to_s
end
bson_type() click to toggle source
# File lib/bson/symbol.rb, line 152
def bson_type
  Symbol::BSON_TYPE
end
eql?(other)
Alias for: ==
to_bson(buffer = ByteBuffer.new) 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 148
def to_bson(buffer = ByteBuffer.new)
  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 126
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 119
def to_sym
  @symbol
end