Module: SiteMaps::Notification::Publisher::ClassMethods
- Extended by:
- Forwardable
- Defined in:
- lib/site_maps/notification/publisher.rb
Overview
Class interface for publishers
Instance Method Summary collapse
- #bus ⇒ Object
-
#instrument(event_id, payload = {}) ⇒ Object
Publish an event with extra runtime information to the payload.
-
#register_event(event_id, payload = {}) ⇒ self
Register a new event type.
-
#subscribe(object_or_event_id, &block) ⇒ Object
Subscribe to events.
Instance Method Details
#bus ⇒ Object
74 75 76 |
# File 'lib/site_maps/notification/publisher.rb', line 74 def bus @bus ||= Bus.new end |
#instrument(event_id, payload = {}) ⇒ Object
Publish an event with extra runtime information to the payload
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/site_maps/notification/publisher.rb', line 37 def instrument(event_id, payload = {}) publish_event = false # ensure block is also called on error raise(UnregisteredEventError, event_id) unless bus.can_handle?(event_id) payload[:__started_at__] = Time.now yield(payload).tap { publish_event = true } ensure if publish_event payload[:runtime] ||= Time.now - payload.delete(:__started_at__) if payload[:__started_at__] bus.publish(event_id, payload) end end |
#register_event(event_id, payload = {}) ⇒ self
Register a new event type
25 26 27 28 |
# File 'lib/site_maps/notification/publisher.rb', line 25 def register_event(event_id, payload = {}) bus.events[event_id] = Event.new(event_id, payload) self end |
#subscribe(object_or_event_id, &block) ⇒ Object
Subscribe to events.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/site_maps/notification/publisher.rb', line 60 def subscribe(object_or_event_id, &block) if bus.can_handle?(object_or_event_id) if block bus.subscribe(object_or_event_id, &block) else bus.attach(object_or_event_id) end self else raise InvalidSubscriberError, object_or_event_id end end |