module BSON::Registry

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

@see bsonspec.org/#/specification

@since 2.0.0

Constants

MAPPINGS

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

@since 2.0.0

Public Instance Methods

get(byte) click to toggle source

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

@example Get the type for the byte.

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

@return [ Class ] The corresponding Ruby class for the type.

@see bsonspec.org/#/specification

@since 2.0.0

# File lib/bson/registry.rb, line 42
def get(byte)
  MAPPINGS[byte]
end
register(byte, type) click to toggle source

Register the Ruby type for the corresponding single byte.

@example Register the type.

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

@param [ String ] byte The single byte. @param [ Class ] The class the byte maps to.

@return [ Class ] The class.

@since 2.0.0

# File lib/bson/registry.rb, line 57
def register(byte, type)
  MAPPINGS.store(byte, type)
  define_type_reader(type)
end

Private Instance Methods

define_type_reader(type) click to toggle source
# File lib/bson/registry.rb, line 64
def define_type_reader(type)
  type.module_eval <<-MOD
    def bson_type; BSON_TYPE; end
  MOD
end