Class: ROM::Notifications::Event

Inherits:
Object
  • Object
show all
Defined in:
core/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



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

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

Parameters:

  • name (String, Symbol)


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

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



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_hHash Also known as: to_hash

Coerce an event to a hash

Returns:

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

Parameters:

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


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.

Returns:

  • (Boolean)


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