Class: Terminalwire::Client::Resource::Handler
- Inherits:
-
Object
- Object
- Terminalwire::Client::Resource::Handler
- Includes:
- Enumerable
- Defined in:
- lib/terminalwire/client/resource.rb
Overview
Dispatches messages from the Client::Handler to the appropriate resource.
Instance Method Summary collapse
- #add(resource) ⇒ Object (also: #<<)
- #dispatch(**message) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize {|_self| ... } ⇒ Handler
constructor
A new instance of Handler.
Constructor Details
#initialize {|_self| ... } ⇒ Handler
Returns a new instance of Handler.
9 10 11 12 |
# File 'lib/terminalwire/client/resource.rb', line 9 def initialize @resources = {} yield self if block_given? end |
Instance Method Details
#add(resource) ⇒ Object Also known as: <<
18 19 20 21 22 23 24 25 |
# File 'lib/terminalwire/client/resource.rb', line 18 def add(resource) # Detect if the resource is already registered and throw an error if @resources.key?(resource.name) raise "Resource #{resource.name} already registered" else @resources[resource.name] = resource end end |
#dispatch(**message) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/terminalwire/client/resource.rb', line 28 def dispatch(**) case in { event:, action:, name:, command:, parameters: } resource = @resources.fetch(name) resource.command(command, **parameters) end end |
#each(&block) ⇒ Object
14 15 16 |
# File 'lib/terminalwire/client/resource.rb', line 14 def each(&block) @resources.values.each(&block) end |