Class: ROM::Notifications::Event
- Inherits:
-
Object
- Object
- ROM::Notifications::Event
- Defined in:
- core/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
92 93 94 95 |
# File 'core/lib/rom/support/notifications.rb', line 92 def initialize(id, payload = EMPTY_HASH) @id = id @payload = payload end |
Instance Attribute Details
#id ⇒ Object (readonly)
82 83 84 |
# File 'core/lib/rom/support/notifications.rb', line 82 def id @id end |
Instance Method Details
#[](name) ⇒ Object
Get data from the payload
102 103 104 |
# File 'core/lib/rom/support/notifications.rb', line 102 def [](name) @payload.fetch(name) end |
# ⇒ Hash #payload(data) ⇒ Event
Get or set a payload
126 127 128 129 130 131 132 |
# File 'core/lib/rom/support/notifications.rb', line 126 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
111 112 113 |
# File 'core/lib/rom/support/notifications.rb', line 111 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
140 141 142 |
# File 'core/lib/rom/support/notifications.rb', line 140 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.
145 146 147 |
# File 'core/lib/rom/support/notifications.rb', line 145 def trigger?(query) query.empty? || query.all? { |key, value| @payload[key] == value } end |