instance method subscribe

Ruby on Rails edge

Since v8.1.4

Available in: v8.1.4 edge

Signature

subscribe(subscriber, &filter)

Registers a new event subscriber. The subscriber must respond to

“‘ emit(event: Hash) “`

The event hash will have the following keys:

“‘ name: String (The name of the event) payload: Hash, Object (The payload of the event, or the event object itself) tags: Hash (The tags of the event) context: Hash (The context of the event) timestamp: Integer (The timestamp of the event, in nanoseconds) source_location: Hash (The source location of the event, containing the filepath, lineno, and label) “`

The event hash and its payload hash are frozen. Subscribers that need to modify them should dup them first.

An optional filter proc can be provided to only receive a subset of events:

“‘ Rails.event.subscribe(subscriber) { |event| event.start_with?(“user.”) } “`

Only the :name key is available to filters, not the entire payload.

Parameters

subscriber req
filter block
Source
# File activesupport/lib/active_support/event_reporter.rb, line 351
    def subscribe(subscriber, &filter)
      unless subscriber.respond_to?(:emit)
        raise ArgumentError, "Event subscriber #{subscriber.class.name} must respond to #emit"
      end
      @subscribers << { subscriber: subscriber, filter: filter }
    end

Defined in activesupport/lib/active_support/event_reporter.rb line 351 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::EventReporter

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close