Module: Rage::Events
- Defined in:
- lib/rage/events/events.rb
Overview
Rage::Events provides a lightweight event-driven system for publishing and subscribing to events. Define events as data structures, register subscriber classes, and publish events to notify all relevant subscribers. Subscribers can process events and optionally receive additional context with each event.
Define an event:
UserRegistered = Data.define(:user_id)
Define a subscriber:
class SendWelcomeEmail
include Rage::Events::Subscriber
subscribe_to UserRegistered
def call(event)
puts "Sending welcome email to user #{event.user_id}"
end
end
Publish an event:
Rage::Events.publish(UserRegistered.new(user_id: 1))
Defined Under Namespace
Modules: Subscriber
Class Method Summary collapse
-
.publish(event, context: nil) ⇒ Object
Publish an event to all subscribers registered for the event’s class or its ancestors.
Class Method Details
.publish(event, context: nil) ⇒ Object
Publish an event to all subscribers registered for the event’s class or its ancestors. Optionally, additional context data can be provided and passed to each subscriber.
40 41 42 43 44 45 |
# File 'lib/rage/events/events.rb', line 40 def self.publish(event, context: nil) handler = __event_handlers[event.class] || __build_event_handler(event.class) handler.call(event, context) nil end |