Class: EventRouter::Destination

Inherits:
Object
  • Object
show all
Defined in:
lib/event_router/destination.rb

Constant Summary collapse

DEFAULT_ATTRIBUTES =

Constants

{
  # Defaults to the event name, e.g "order_placed"
  handler_method: nil,
  # Defaults to not fetch before scheduling the job.
  prefetch_payload: false,
  # Defaults to destination name, e.g "#{name}_payload"
  payload_method: nil
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, handler:, **opts) ⇒ Destination

Methods



20
21
22
23
24
25
26
27
28
# File 'lib/event_router/destination.rb', line 20

def initialize(name, handler:, **opts)
  opts = DEFAULT_ATTRIBUTES.merge(opts)

  @name             = name
  @handler          = handler
  @handler_method   = opts[:handler_method]
  @prefetch_payload = opts[:prefetch_payload]
  @payload_method   = opts[:payload_method] || "#{name}_payload"
end

Instance Attribute Details

#handlerObject (readonly)

Attributes



6
7
8
# File 'lib/event_router/destination.rb', line 6

def handler
  @handler
end

#handler_methodObject (readonly)

Attributes



6
7
8
# File 'lib/event_router/destination.rb', line 6

def handler_method
  @handler_method
end

#nameObject (readonly)

Attributes



6
7
8
# File 'lib/event_router/destination.rb', line 6

def name
  @name
end

#payload_methodObject (readonly)

Attributes



6
7
8
# File 'lib/event_router/destination.rb', line 6

def payload_method
  @payload_method
end

#prefetch_payloadObject (readonly)

Attributes



6
7
8
# File 'lib/event_router/destination.rb', line 6

def prefetch_payload
  @prefetch_payload
end

Instance Method Details

#extra_payload(event) ⇒ Object



42
43
44
45
46
# File 'lib/event_router/destination.rb', line 42

def extra_payload(event)
  return nil unless event.respond_to?(payload_method)

  event.send(payload_method)
end

#prefetch_payload?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/event_router/destination.rb', line 38

def prefetch_payload?
  @prefetch_payload
end

#process(event, payload) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/event_router/destination.rb', line 30

def process(event, payload)
  handler.send(
    handler_method || event.name,
    event: event,
    payload: payload
  )
end