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

Defined Under Namespace

Classes: UnsupportedType

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")

See Also:

Since:

  • 2.0.0


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

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)

Since:

  • 2.0.0


61
62
63
64
# File 'lib/bson/registry.rb', line 61

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