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
-
#default_connection ⇒ Symbol?
Currently assigned default connection for clients.
- #default_connection=(key) ⇒ void
- #new(sym_or_cls = nil, *args, &block) ⇒ Object
- #register_connection(key, cls) ⇒ void
Instance Method Details
#default_connection ⇒ Symbol?
Returns 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.
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
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.
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 |