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, field = nil)
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, 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)
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 ] type The class the byte maps to.
@return [ Class ] The class.
@since 2.0.0
# File lib/bson/registry.rb, line 61 def register(byte, type) MAPPINGS[byte.ord] = type define_type_reader(type) end
Private Instance Methods
define_type_reader(type)
click to toggle source
# File lib/bson/registry.rb, line 73 def define_type_reader(type) type.module_eval <<-MOD def bson_type; BSON_TYPE; end MOD end
handle_unsupported_type!(byte, field)
click to toggle source
# File lib/bson/registry.rb, line 79 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