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


94
95
96
97
# File 'lib/rom/support/notifications.rb', line 94

def initialize(id, payload = EMPTY_HASH)
  @id = id
  @payload = payload
end

Instance Attribute Details

#idObject (readonly)


84
85
86
# File 'lib/rom/support/notifications.rb', line 84

def id
  @id
end

Instance Method Details

#[](name) ⇒ Object

Get data from the payload

Parameters:

  • name (String, Symbol)

104
105
106
# File 'lib/rom/support/notifications.rb', line 104

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


128
129
130
131
132
133
134
# File 'lib/rom/support/notifications.rb', line 128

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)

113
114
115
# File 'lib/rom/support/notifications.rb', line 113

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)

142
143
144
# File 'lib/rom/support/notifications.rb', line 142

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)

147
148
149
# File 'lib/rom/support/notifications.rb', line 147

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