Skip to content

Dynamically register handlers #3291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

sambostock
Copy link
Contributor

Motivation

This PR offers a proof-of-concept of dynamically registering handlers by discovering their public methods which conform to the handler name pattern. This is a similar approach to that used by other tools such as Minitest (discovering public test_* methods) or Rails generators (executing public methods in the order they are defined).

This approach was conceived while discussing the RubyLsp/UseRegisterWithHandlerMethod cop, which checks that all methods conforming to the handler name pattern are registered in initialize, and that all registered handlers have a corresponding method definition. This approach would make the cop obsolete.

Implementation

As a proof-of-concept, this prepends a module into Prism::Dispatcher to support dynamically determining the event list to handle. However, the proposal would be to upstream the following directly into Prism:

module Prism
  class Dispatcher
    def register(listener, *events)
      events = listener.public_methods.grep(/^on_.+_(?:enter|leave)$/) if events.empty?

      # ...existing code...
    end

Automated Tests

Manual Tests

Copy link

graphite-app bot commented Mar 11, 2025

How to use the Graphite Merge Queue

Add the label graphite-merge to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant