Class: RobotLab::HookRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/robot_lab/hook_registry.rb

Defined Under Namespace

Classes: Registration

Instance Method Summary collapse

Constructor Details

#initializeHookRegistry

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

#clearObject



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.

Parameters:

  • handler_class (Class)

    a RobotLab::Hook subclass

  • context (Hash, nil) (defaults to: nil)

    optional default values merged into ctx.local on each call

Returns:

Raises:

  • (ArgumentError)


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

#registrationsArray<Registration>

Returns all registrations (defensive copy).

Returns:



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.

Parameters:

  • hook_name (Symbol)

Returns:



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