Class: Textus::Hooks::RpcRegistry
- Inherits:
-
Object
- Object
- Textus::Hooks::RpcRegistry
- Defined in:
- lib/textus/hooks/rpc_registry.rb
Constant Summary collapse
- EVENTS =
{ resolve_intake: %i[caps config args], transform_rows: %i[caps rows config], validate: %i[caps], }.freeze
- PUBSUB_EVENTS =
EventBus::EVENTS.keys.freeze
Instance Method Summary collapse
- #callable(event, name) ⇒ Object
-
#initialize ⇒ RpcRegistry
constructor
A new instance of RpcRegistry.
-
#invoke(event, name, caps:, **other) ⇒ Object
Invoke a registered callable, injecting ‘caps:` under the kwarg name the callable declares.
- #names(event) ⇒ Object
- #register(event, name, &blk) ⇒ Object
Constructor Details
#initialize ⇒ RpcRegistry
Returns a new instance of RpcRegistry.
14 15 16 |
# File 'lib/textus/hooks/rpc_registry.rb', line 14 def initialize @table = Hash.new { |h, k| h[k] = {} } end |
Instance Method Details
#callable(event, name) ⇒ Object
32 33 34 |
# File 'lib/textus/hooks/rpc_registry.rb', line 32 def callable(event, name) @table[event.to_sym][name.to_sym] or raise UsageError.new("unknown #{event}: #{name}") end |
#invoke(event, name, caps:, **other) ⇒ Object
Invoke a registered callable, injecting ‘caps:` under the kwarg name the callable declares. Legacy `store:` is rejected (no shim).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/textus/hooks/rpc_registry.rb', line 38 def invoke(event, name, caps:, **other) blk = callable(event, name) params = blk.parameters accepts_keyrest = params.any? { |t, _| t == :keyrest } declared = params.each_with_object([]) { |(t, n), acc| acc << n if %i[key keyreq].include?(t) } if declared.include?(:store) raise UsageError.new( "RPC callable for #{event} '#{name}' declares legacy `store:`; rename to `caps:` " \ "(Textus::Application::ReadCaps / WriteCaps)", ) end kwargs = other.dup kwargs[:caps] = caps if accepts_keyrest || declared.include?(:caps) blk.call(**kwargs) end |
#names(event) ⇒ Object
30 |
# File 'lib/textus/hooks/rpc_registry.rb', line 30 def names(event) = @table[event.to_sym].keys |
#register(event, name, &blk) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/textus/hooks/rpc_registry.rb', line 18 def register(event, name, &blk) event_sym = event.to_sym raise UsageError.new("#{event_sym} is a pubsub event; register on EventBus") if PUBSUB_EVENTS.include?(event_sym) required = EVENTS[event_sym] or raise UsageError.new("unknown RPC event: #{event}") shape_check!(event_sym, required, blk) name = name.to_sym raise UsageError.new("#{event_sym} '#{name}' already registered") if @table[event_sym].key?(name) @table[event_sym][name] = blk end |