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
Source
# File lib/bson/registry.rb, line 43 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
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
Source
# File lib/bson/registry.rb, line 62 def register(byte, type) MAPPINGS[byte.ord] = type define_type_reader(type) end
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 ] type The class the byte maps to.
@return [ Class ] The class.
@since 2.0.0
Private Instance Methods
Source
# File lib/bson/registry.rb, line 74 def define_type_reader(type) type.module_eval <<-MOD def bson_type; BSON_TYPE; end MOD end
Source
# File lib/bson/registry.rb, line 80 def handle_unsupported_type!(byte, field) message = "Detected unknown BSON type #{byte.inspect} " message += (field ? "for fieldname \"#{field}\". " : "in array. ") message +="Are you using the latest BSON version?" raise UnsupportedType.new(message) end