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



53
54
55
# File 'lib/bson/open_struct.rb', line 53

def bson_type
  ::Hash::BSON_TYPE
end

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

Get the OpenStruct as encoded BSON.

Examples:

Get the OpenStruct object as encoded BSON.

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

Returns:

See Also:

Since:

  • 4.2.0



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

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