Module: BSON::Registry

Extended by:
Registry
Included in:
Registry
Defined in:
lib/bson/registry.rb

Overview

Provides constant values for each to the BSON types and mappings from raw bytes back to these types.

See Also:

Since:

  • 2.0.0

Constant Summary collapse

MAPPINGS =

A Mapping of all the BSON types to their corresponding Ruby classes.

Since:

  • 2.0.0

{}

Instance Method Summary collapse

Instance Method Details

#get(byte, field = nil) ⇒ Class

Get the class for the single byte identifier for the type in the BSON specification.

Examples:

Get the type for the byte.

BSON::Registry.get("\x01")

Returns:

  • (Class)

    The corresponding Ruby class for the type.

See Also:

Since:

  • 2.0.0



44
45
46
47
48
49
50
# File 'lib/bson/registry.rb', line 44

def get(byte, field = nil)
  if type = MAPPINGS[byte] || (byte.is_a?(String) && type = MAPPINGS[byte.ord])
    type
  else
    handle_unsupported_type!(byte, field)
  end
end

#register(byte, type) ⇒ Class

Register the Ruby type for the corresponding single byte.

Examples:

Register the type.

BSON::Registry.register("\x01", Float)

Parameters:

  • byte (String)

    The single byte.

  • type (Class)

    The class the byte maps to.

Returns:

  • (Class)

    The class.

Since:

  • 2.0.0



63
64
65
66
# File 'lib/bson/registry.rb', line 63

def register(byte, type)
  MAPPINGS[byte.ord] = type
  define_type_reader(type)
end