Class: RobotLab::HookRegistry
- Inherits:
-
Object
- Object
- RobotLab::HookRegistry
- Defined in:
- lib/robot_lab/hook_registry.rb
Defined Under Namespace
Classes: Registration
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize ⇒ HookRegistry
constructor
A new instance of HookRegistry.
-
#on(handler_class, context: nil) ⇒ Registration
Register a hook handler class.
-
#registrations ⇒ Array<Registration>
Returns all registrations (defensive copy).
-
#registrations_for(hook_name) ⇒ Array<Registration>
Returns registrations whose handler implements the given hook method.
Constructor Details
#initialize ⇒ HookRegistry
Returns a new instance of HookRegistry.
15 16 17 |
# File 'lib/robot_lab/hook_registry.rb', line 15 def initialize @registrations = [] end |
Instance Method Details
#clear ⇒ Object
51 52 53 |
# File 'lib/robot_lab/hook_registry.rb', line 51 def clear @registrations.clear end |
#on(handler_class, context: nil) ⇒ Registration
Register a hook handler class.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/robot_lab/hook_registry.rb', line 24 def on(handler_class, context: nil) unless handler_class.is_a?(Class) && handler_class < RobotLab::Hook raise ArgumentError, "#{handler_class.inspect} must be a RobotLab::Hook subclass" end raise ArgumentError, "#{handler_class} has no namespace" if handler_class.namespace.nil? registration = Registration.new(handler_class: handler_class, context: context) @registrations << registration registration end |
#registrations ⇒ Array<Registration>
Returns all registrations (defensive copy).
47 48 49 |
# File 'lib/robot_lab/hook_registry.rb', line 47 def registrations @registrations.dup end |
#registrations_for(hook_name) ⇒ Array<Registration>
Returns registrations whose handler implements the given hook method.
39 40 41 42 |
# File 'lib/robot_lab/hook_registry.rb', line 39 def registrations_for(hook_name) hook_sym = hook_name.to_sym @registrations.select { |reg| reg.handler_class.singleton_class.public_method_defined?(hook_sym) } end |