123456789_123456789_123456789_123456789_123456789_

Module: Mongo::Protocol::Registry

Relationships & Source Files
Defined in: lib/mongo/protocol/registry.rb

Overview

Provides a registry for looking up a message class based on op code.

Since:

  • 2.5.0

Constant Summary

Instance Method Summary

Instance Method Details

#define_type_reader(type) (private)

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/protocol/registry.rb', line 67

def define_type_reader(type)
  type.module_eval <<-MOD
    def op_code; OP_CODE; end
  MOD
end

#get(op_code, message = nil) ⇒ Class

Get the class for the given op code and raise an error if it’s not found.

Examples:

Get the type for the op code.

Mongo::Protocol::Registry.get(1)

Returns:

  • (Class)

    The corresponding Ruby class for the message type.

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/protocol/registry.rb', line 41

def get(op_code, message = nil)
  if type = MAPPINGS[op_code]
    type
  else
    handle_unsupported_op_code!(op_code)
  end
end

#handle_unsupported_op_code!(op_code) (private)

Raises:

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/protocol/registry.rb', line 73

def handle_unsupported_op_code!(op_code)
  message = "Detected unknown message type with op code: #{op_code}."
  raise Error::UnsupportedMessageType.new(message)
end

#register(op_code, type) ⇒ Class

Register the Ruby type for the corresponding op code.

Examples:

Register the op code.

Mongo::Protocol::Registry.register(1, Reply)

Parameters:

  • op_code (Fixnum)

    The op code.

  • type (Class)

    The class the op code maps to.

Returns:

  • (Class)

    The class.

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/protocol/registry.rb', line 60

def register(op_code, type)
  MAPPINGS.store(op_code, type)
  define_type_reader(type)
end