Module: BSON::Object

Defined in:
lib/bson/object.rb

Overview

Injects behaviour for all Ruby objects.

Since:

  • 2.2.4

Instance Method Summary collapse

Instance Method Details

#as_extended_json(**options) ⇒ Object

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

Subclasses should override this method to provide custom serialization to Extended JSON.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :relaxed (true | false)

    Whether to produce relaxed extended JSON representation.

Returns:

  • (Object)

    The extended json representation.

Since:

  • 2.2.4


85
86
87
# File 'lib/bson/object.rb', line 85

def as_extended_json(**options)
  self
end

#to_bson_key(validating_keys = Config.validating_keys?) ⇒ Object

Objects that don't override this method will raise an error when trying to use them as keys in a BSON document. This is only overridden in String and Symbol.

Examples:

Convert the object to a BSON key.

object.to_bson_key

Raises:

See Also:

Since:

  • 2.2.4


34
35
36
# File 'lib/bson/object.rb', line 34

def to_bson_key(validating_keys = Config.validating_keys?)
  raise InvalidKey.new(self)
end

#to_bson_normalized_keyObject

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

Examples:

Convert the object to a normalized key.

object.to_bson_normalized_key

Returns:

Since:

  • 3.0.0


46
47
48
# File 'lib/bson/object.rb', line 46

def to_bson_normalized_key
  self
end

#to_bson_normalized_valueObject

Converts the object to a normalized value in a BSON document.

Examples:

Convert the object to a normalized value.

object.to_bson_normalized_value

Returns:

Since:

  • 3.0.0


58
59
60
# File 'lib/bson/object.rb', line 58

def to_bson_normalized_value
  self
end

#to_extended_json(**options) ⇒ String

Serializes this object to Extended JSON (github.com/mongodb/specifications/blob/master/source/extended-json.rst).

Subclasses should override as_extended_json rather than this method.

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (String)

    The extended json serialization.

Since:

  • 2.2.4


71
72
73
# File 'lib/bson/object.rb', line 71

def to_extended_json(**options)
  as_extended_json(**options).to_json
end