Class: ROM::Notifications::Event
- Inherits:
-
Object
- Object
- ROM::Notifications::Event
- Defined in:
- lib/rom/support/notifications.rb
Overview
Event object
Instance Attribute Summary collapse
- #id ⇒ Object readonly
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get data from the payload.
-
#initialize(id, payload = EMPTY_HASH) ⇒ Event
constructor
private
Initialize a new event.
-
#payload(data = nil) ⇒ Object
Get or set a payload.
-
#to_h ⇒ Hash
(also: #to_hash)
Coerce an event to a hash.
-
#trigger(listener, query = EMPTY_HASH) ⇒ Object
private
Trigger the event.
- #trigger?(query) ⇒ Boolean private
Constructor Details
#initialize(id, payload = EMPTY_HASH) ⇒ Event
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new event
98 99 100 101 |
# File 'lib/rom/support/notifications.rb', line 98 def initialize(id, payload = EMPTY_HASH) @id = id @payload = payload end |
Instance Attribute Details
#id ⇒ Object (readonly)
88 89 90 |
# File 'lib/rom/support/notifications.rb', line 88 def id @id end |
Instance Method Details
#[](name) ⇒ Object
Get data from the payload
108 109 110 |
# File 'lib/rom/support/notifications.rb', line 108 def [](name) @payload.fetch(name) end |
# ⇒ Hash #payload(data) ⇒ Event
Get or set a payload
132 133 134 135 136 137 138 |
# File 'lib/rom/support/notifications.rb', line 132 def payload(data = nil) if data self.class.new(id, @payload.merge(data)) else @payload end end |
#to_h ⇒ Hash Also known as: to_hash
Coerce an event to a hash
117 118 119 |
# File 'lib/rom/support/notifications.rb', line 117 def to_h @payload end |
#trigger(listener, query = EMPTY_HASH) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Trigger the event
146 147 148 |
# File 'lib/rom/support/notifications.rb', line 146 def trigger(listener, query = EMPTY_HASH) listener.(self) if trigger?(query) end |
#trigger?(query) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
151 152 153 |
# File 'lib/rom/support/notifications.rb', line 151 def trigger?(query) query.empty? || query.all? { |key, value| @payload[key] == value } end |