module Rooibos::Router::ClassMethods
Class methods added when Router is included.
Public Instance Methods
Source
# File lib/rooibos/router.rb, line 94 def action(name = nil, value = nil, keymap: nil, key: nil, keys: nil, mousemap: nil, **kwargs) # key: and keys: are aliases for keymap: effective_keymap = keymap || key || keys action_name, action_value = if name && value # Positional: action :name, handler [name, value] elsif name.respond_to?(:call) && value.nil? # Anonymous: action -> { ... }, keymap: %i[...] # No name, just handler with bindings [nil, name] elsif kwargs.size == 1 # Keyword: action name: handler kwargs.first else raise ArgumentError, "action requires (name, value) or (name: value)" end # @type var action_name: Symbol? # @type var action_value: (^() -> Command::execution? | Module)? register_action(action_name, action_value) if action_name && action_value # For anonymous actions, store handler directly in keymap handler_for_keymap = action_name.nil? ? action_value : nil # Register keymap bindings if provided if effective_keymap Array(effective_keymap).each do |key_name| key_handlers[key_name.to_s.to_sym] = Router::KeyHandlerConfig.new( handler: handler_for_keymap, action: action_name&.to_s&.to_sym, guard: nil, route: nil ) end end # Register mousemap bindings if provided if mousemap Array(mousemap).each do |mouse_event| scroll_handlers[mouse_event.to_s.to_sym] = Router::ScrollHandlerConfig.new( handler: handler_for_keymap, action: action_name&.to_s&.to_sym ) end end end
Declares a named action.
Actions are shared handlers that keymap and mousemap can reference. This avoids duplicating logic for keys and mouse events that do the same thing.
Supports both positional and keyword syntax:
action :scroll_up, -> { Command.scroll(-1) } # Positional action scroll_up: -> { Command.scroll(-1) } # Keyword
- name
-
Symbol or String identifying the action (normalized via
.to_s.to_sym). - value
-
Callable that returns a command or message.
Source
# File lib/rooibos/router.rb, line 152 def actions @actions ||= {} end
Returns the registered handler actions hash.
Source
# File lib/rooibos/router.rb, line 212 def from_router RouterUpdate.new( routes:, actions:, routed_actions:, key_handlers:, scroll_handlers:, click_handler: ) end
Generates an UPDATE lambda from routes, keymap, and mousemap.
The generated UPDATE:
-
Routes prefixed messages to child UPDATEs
-
Handles keyboard events via keymap
-
Handles mouse events via mousemap
-
Returns model unchanged for unhandled messages
Source
# File lib/rooibos/router.rb, line 169 def keymap(&) builder = KeymapBuilder.new builder.instance_eval(&) @key_handlers = builder.handlers end
Declares key handlers in a block.
Example
keymap do key "q", -> { Command.exit } key :up, :scroll_up # Delegate to action end
Source
# File lib/rooibos/router.rb, line 183 def mousemap(&) builder = MousemapBuilder.new builder.instance_eval(&) @scroll_handlers = builder.scroll_handlers @click_handler = builder.click_handler end
Declares mouse handlers in a block.
Example
mousemap do click -> (x, y) { [:clicked, x, y] } scroll :up, :scroll_up # Delegate to action end
Source
# File lib/rooibos/router.rb, line 73 def route(fragment_model_instance_attr, to:) routes[fragment_model_instance_attr.to_s.to_sym] = to end
Declares a route to a child fragment.
- fragment_model_instance_attr
-
Symbol naming the attr on the parentโs model
that holds this fragment's model instance (normalized via +.to_s.to_sym+).
- to
-
The child fragment module (must have Update and Init constants).
Source
# File lib/rooibos/router.rb, line 157 def routed_actions @routed_actions ||= {} end
Returns the registered routed actions hash.
Source
# File lib/rooibos/router.rb, line 78 def routes @routes ||= {} end
Returns the registered routes hash.