Module: MaimaiNet::Client::ConnectionProvider

Included in:
Base
Defined in:
lib/maimai_net/client.rb

Overview

Provides capability to handle the connection wrapping.

Instance Method Summary collapse

Instance Method Details

#default_connectionSymbol?

Returns currently assigned default connection for clients.

Returns:

  • (Symbol, nil)

    currently assigned default connection for clients



454
455
456
# File 'lib/maimai_net/client.rb', line 454

def default_connection
  class_variable_get(:@@default_connection)
end

#default_connection=(key) ⇒ void

This method returns an undefined value.

Parameters:

  • key (Symbol)

    connection name

Raises:

  • (KeyError)

    provided key is not a registered connection.



461
462
463
464
465
466
# File 'lib/maimai_net/client.rb', line 461

def default_connection=(key)
  key = String(key).to_sym
  fail KeyError, "'#{key}' is not registered" unless connections.key?(key)
  class_variable_set(:@@default_connection, key)
  nil
end

#new(*args) ⇒ Connection #new(key, *args) ⇒ Connection #new(cls, *args) ⇒ Connection

Overloads:

  • #new(*args) ⇒ Connection

    First form, pass the remaining arguments into #initialize and wrap it into a connection.

    Returns:

  • #new(key, *args) ⇒ Connection

    Second form, use mapped connection name to retrieve the connection,

    pass remaining arguments into #initialize and wrap it into a connection.
    

    Parameters:

    • key (Symbol)

      client connection name to use

    Returns:

    • (Connection)

      provided connection from given key

  • #new(cls, *args) ⇒ Connection

    Third form, use given class as connection,

    pass remaining arguments into #initialize and wrap it into a connection.
    

    Parameters:

    • cls (Class<Connection>)

      client connection class to use

    Returns:

    Raises:

    • (ArgumentError)

      provided class is not a Connection.

Raises:

  • (ArgumentError)

    invalid form.



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/maimai_net/client.rb', line 424

def new(sym_or_cls = nil, *args, &block)
  return method(__method__).call(nil, sym_or_cls, *args, &block) if String === sym_or_cls
  sym_or_cls = self.default_connection if sym_or_cls.nil?

  case sym_or_cls
  when Symbol
    cls = connections[sym_or_cls]
  when Class
    cls = sym_or_cls
    fail ArgumentError, "expected Connection class, given #{cls}" unless cls < Connection
  else
    fail ArgumentError, "expected a connection name or a Connection-subclass, given #{sym_or_cls.class}"
  end

  cls.new(super(*args, &block))
end

#register_connection(key, cls) ⇒ void

This method returns an undefined value.

Parameters:

  • key (Symbol)

    connection name

  • cls (Class<Connection>)

    connection class

Raises:

  • (ArgumentError)

    provided class is not a Connection.



445
446
447
448
449
450
451
# File 'lib/maimai_net/client.rb', line 445

def register_connection(key, cls)
  fail ArgumentError, "expected Connection class, given #{cls}" unless cls < Connection
  key = String(key).to_sym
  connections.store(key, cls)
  self.default_connection = key if connections.size.pred.zero?
  nil
end