Class: BSON::Symbol::Raw

Inherits:
Object
  • Object
show all
Includes:
JSON
Defined in:
lib/bson/symbol.rb

Overview

Since:

  • 2.0.0

Instance Method Summary collapse

Methods included from JSON

#to_json

Constructor Details

#initialize(str_or_sym) ⇒ Raw

Create a BSON Symbol

Parameters:

  • str_or_sym (String | Symbol)

    The symbol represented by this object. Can be specified as a Symbol or a String.

See Also:

Since:

  • 2.0.0



108
109
110
111
112
113
114
# 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

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Check equality of the raw bson symbol against another.

Parameters:

  • other (Object)

    The object to check against.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



135
136
137
138
# File 'lib/bson/symbol.rb', line 135

def ==(other)
  return false unless other.is_a?(Raw)
  to_sym == other.to_sym
end

#as_extended_json(**_options) ⇒ Hash

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

Returns:

  • (Hash)

    The extended json representation.

Since:

  • 2.0.0



172
173
174
# File 'lib/bson/symbol.rb', line 172

def as_extended_json(**_options)
  { '$symbol' => to_s }
end

#as_json(*args) ⇒ String

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

Examples:

Get the raw symbol as a JSON-serializable object.

raw_symbol.as_json

Returns:

  • (String)

    The raw symbol as a String.

Since:

  • 2.0.0



164
165
166
# File 'lib/bson/symbol.rb', line 164

def as_json(*args)
  to_s
end

#bson_typeObject

Since:

  • 2.0.0



152
153
154
# File 'lib/bson/symbol.rb', line 152

def bson_type
  Symbol::BSON_TYPE
end

#to_bson(buffer = ByteBuffer.new) ⇒ BSON::ByteBuffer

Get the symbol as encoded BSON.

Returns:

Raises:

  • (EncodingError)

    If the symbol is not UTF-8.

See Also:

Since:

  • 2.0.0



148
149
150
# File 'lib/bson/symbol.rb', line 148

def to_bson(buffer = ByteBuffer.new)
  buffer.put_string(to_s)
end

#to_sString

Get the underlying symbol as a Ruby string.

Returns:

  • (String)

    The symbol as a string.

Since:

  • 2.0.0



126
127
128
# File 'lib/bson/symbol.rb', line 126

def to_s
  @symbol.to_s
end

#to_symSymbol

Get the underlying symbol as a Ruby symbol.

Returns:

  • (Symbol)

    The symbol represented by this BSON object.

Since:

  • 2.0.0



119
120
121
# File 'lib/bson/symbol.rb', line 119

def to_sym
  @symbol
end