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



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

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

  @name             = name
  @handler          = handler
  @handler_method   = opts.delete(:handler_method)
  @prefetch_payload = opts.delete(:prefetch_payload)
  @payload_method   = opts.delete(:payload_method) || "#{name}_payload"
  @options          = opts
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

#optionsObject (readonly)

Attributes



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

def options
  @options
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



44
45
46
47
48
# File 'lib/event_router/destination.rb', line 44

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

  event.send(payload_method)
end

#prefetch_payload?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/event_router/destination.rb', line 40

def prefetch_payload?
  @prefetch_payload
end

#process(event, payload) ⇒ Object



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

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