Class: ROM::Notifications::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/support/notifications.rb

Overview

Event object

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • id (Symbol)

    The event identifier

  • payload (Hash) (defaults to: EMPTY_HASH)

    Optional payload



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

#idObject (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

Parameters:

  • name (String, Symbol)


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

Overloads:

  • #Hash

    Returns payload.

    Returns:

    • (Hash)

      payload

  • #payload(data) ⇒ Event

    Returns A copy of the event with the provided payload.

    Parameters:

    • data (Hash)

      A new payload

    Returns:

    • (Event)

      A copy of the event with the provided 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_hHash Also known as: to_hash

Coerce an event to a hash

Returns:

  • (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

Parameters:

  • listener (#call)
  • query (Hash) (defaults to: EMPTY_HASH)


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.

Returns:

  • (Boolean)


151
152
153
# File 'lib/rom/support/notifications.rb', line 151

def trigger?(query)
  query.empty? || query.all? { |key, value| @payload[key] == value }
end