Class: Moleculer::Service::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/moleculer/service/event.rb

Overview

Represents a service event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, service, method, options = {}) ⇒ Event

TODO: add ability to group events

Parameters:

  • name (String)

    the name of the action

  • service (Moleculer::Service)

    the service to which the action belongs

  • method (Symbol)

    the method which the event calls when executed

  • options (Hash) (defaults to: {})

    the method options



20
21
22
23
24
25
26
# File 'lib/moleculer/service/event.rb', line 20

def initialize(name, service, method, options = {})
  @name    = name
  @service = service
  @method  = method
  @service = service
  @options = options
end

Instance Attribute Details

#nameString (readonly)

Returns the name of the action.

Returns:

  • (String)

    the name of the action



12
13
14
# File 'lib/moleculer/service/event.rb', line 12

def name
  @name
end

#serviceObject (readonly)

Returns the value of attribute service.



12
# File 'lib/moleculer/service/event.rb', line 12

attr_reader :name, :service

Instance Method Details

#execute(data, broker) ⇒ Object

Executes the event

Parameters:



32
33
34
35
36
# File 'lib/moleculer/service/event.rb', line 32

def execute(data, broker)
  @service.new(broker).public_send(@method, data)
rescue StandardError => e
  broker.config.handle_error(e)
end

#nodeMoleculer::Node

Returns the node of the service this event is tied to.

Returns:



40
41
42
# File 'lib/moleculer/service/event.rb', line 40

def node
  @service.node
end

#to_hHash

Returns a hash representing this event as it would be in JSON.

Returns:

  • (Hash)

    a hash representing this event as it would be in JSON



46
47
48
49
50
# File 'lib/moleculer/service/event.rb', line 46

def to_h
  {
    name: name,
  }
end