Module: BSON::OpenStruct

Defined in:
lib/bson/open_struct.rb

Overview

Injects behaviour for encoding OpenStruct objects using hashes to raw bytes as specified by the BSON spec.

See Also:

Since:

  • 4.2.0

Instance Method Summary collapse

Instance Method Details

#bson_typeString

The BSON type for OpenStruct objects is the Hash type of 0x03.

Examples:

Get the bson type.

struct.bson_type

Returns:

  • (String)

    The character 0x03.

Since:

  • 4.2.0



51
52
53
# File 'lib/bson/open_struct.rb', line 51

def bson_type
  ::Hash::BSON_TYPE
end

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

Get the OpenStruct as encoded BSON.

Examples:

Get the OpenStruct object as encoded BSON.

OpenStruct.new({ "field" => "value" }).to_bson

Returns:

  • (BSON::ByteBuffer)

    The buffer with the encoded object.

See Also:

Since:

  • 4.2.0



35
36
37
38
39
40
41
# File 'lib/bson/open_struct.rb', line 35

def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
  if Environment.ruby_1_9?
    marshal_dump.dup
  else
    to_h
  end.to_bson(buffer, validating_keys)
end