class Dry::Events::Event

Event object

@api public

Constants

DOT
InvalidEventNameError
UNDERSCORE

Attributes

id[R]

@!attribute [r] id

@return [Symbol, String] The event identifier

Public Class Methods

new(id, payload = EMPTY_HASH) click to toggle source

@api private

Calls superclass method
# File lib/dry/events/event.rb, line 29
def self.new(id, payload = EMPTY_HASH)
  return super(id, payload) if (id.is_a?(String) || id.is_a?(Symbol)) && !id.empty?

  raise InvalidEventNameError.new
end
new(id, payload) click to toggle source

Initialize a new event

@param [Symbol, String] id The event identifier @param [Hash] payload

@return [Event]

@api private

# File lib/dry/events/event.rb, line 43
def initialize(id, payload)
  @id = id
  @payload = payload
end

Public Instance Methods

[](name) click to toggle source

Get data from the payload

@param [String,Symbol] name

@api public

# File lib/dry/events/event.rb, line 53
def [](name)
  @payload.fetch(name)
end
listener_method() click to toggle source

@api private

# File lib/dry/events/event.rb, line 86
def listener_method
  @listener_method ||= :"on_#{id.to_s.gsub(DOT, UNDERSCORE)}"
end
payload(data = nil) click to toggle source

Get or set a payload

@overload

@return [Hash] payload

@overload payload(data)

@param [Hash] data A new payload
@return [Event] A copy of the event with the provided payload

@api public

# File lib/dry/events/event.rb, line 77
def payload(data = nil)
  if data
    self.class.new(id, @payload.merge(data))
  else
    @payload
  end
end
to_h() click to toggle source

Coerce an event to a hash

@return [Hash]

@api public

# File lib/dry/events/event.rb, line 62
def to_h
  @payload
end
Also aliased as: to_hash
to_hash()
Alias for: to_h