Class: Moleculer::Service::Base Abstract

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

Overview

This class is abstract.

subclass to define a local service.

Direct Known Subclasses

Remote

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(broker) ⇒ Base

Returns a new instance of Base.



111
112
113
# File 'lib/moleculer/service/base.rb', line 111

def initialize(broker)
  @broker = broker
end

Class Attribute Details

.brokerObject



29
30
31
# File 'lib/moleculer/service/base.rb', line 29

def broker
  @broker || Moleculer.broker
end

.service_prefixstring

Returns a prefix to add to the service name. The service_prefix is inherited from the parent class if it is not already defined in the current class.

Returns:

  • (string)

    a prefix to add to the service name. The service_prefix is inherited from the parent class if it is not already defined in the current class.



15
# File 'lib/moleculer/service/base.rb', line 15

attr_writer :service_prefix

Class Method Details

.action(name, method, options = {}) ⇒ Object

Defines an action on the service.

options will reflect the hash. provided type.

Parameters:

  • name (String|Symbol)

    the name of the action.

  • method (Symbol)

    the method to which the action maps.

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

    the action options.

Options Hash (options):

  • :cache (Boolean|Hash)

    if true, will use default caching options, if a hash is provided caching

  • params (Hash)

    list of param and param types. Can be used to coerce specific params to the



56
57
58
# File 'lib/moleculer/service/base.rb', line 56

def action(name, method, options = {})
  actions[action_name_for(name)] = Action.new(name, self, method, options)
end

.action_name_for(name) ⇒ Object



106
107
108
# File 'lib/moleculer/service/base.rb', line 106

def action_name_for(name)
  "#{full_name}.#{name}"
end

.actionsObject



80
81
82
# File 'lib/moleculer/service/base.rb', line 80

def actions
  @actions ||= {}
end

.event(name, method, options = {}) ⇒ Object

Defines an event on the service.

Parameters:

  • name (String|Symbol)

    the name of the event.

  • method (Symbol)

    the method to which the event maps.

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

    event options.

Options Hash (options):

  • :group (Hash)

    the group in which the event should belong, defaults to the service_name



67
68
69
# File 'lib/moleculer/service/base.rb', line 67

def event(name, method, options = {})
  events[name] = Event.new(name, self, method, options)
end

.eventsObject



84
85
86
# File 'lib/moleculer/service/base.rb', line 84

def events
  @events ||= {}
end

.full_nameString

Returns the full name of the service, including version and prefix

Returns:

  • (String)

    returns the full name of the service, including version and prefix



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/moleculer/service/base.rb', line 90

def full_name
  return service_name unless @version

  @full_name = service_name.dup
  version    = @version.to_s
  version.prepend("v") if @version.is_a? Numeric

  if @full_name.include?(".")
    @full_name.sub!(".", ".#{version}.")
  elsif version
    @full_name.prepend("#{version}.")
  end

  @full_name
end

.nodeObject



25
26
27
# File 'lib/moleculer/service/base.rb', line 25

def node
  broker.local_node
end

.service_name(name = nil) ⇒ Object

Set the service_name to the provided name. If the node is local it will prefix the service name with the service prefix

Parameters:

  • name (String) (defaults to: nil)

    the name to which the service_name should be set



38
39
40
41
42
43
44
# File 'lib/moleculer/service/base.rb', line 38

def service_name(name = nil)
  @service_name = name if name

  return "#{broker.service_prefix}.#{@service_name}" unless broker.service_prefix.nil?

  @service_name
end

.to_hObject

rubocop:disable Metrics/AbcSize



135
136
137
138
139
140
141
142
143
144
# File 'lib/moleculer/service/base.rb', line 135

def self.to_h # rubocop:disable Metrics/AbcSize
  {
    name:     full_name,
    version:  version,
    settings: {},
    metadata: {},
    actions:  Hash[actions.values.map { |a| [a.name.to_sym, a.to_h] }],
    events:   Hash[events.values.map { |e| [e.name.to_sym, e.to_h] }],
  }
end

.version(ver = nil) ⇒ Object

Defines a version name or number on the service.

Parameters:

  • ver (String|Number) (defaults to: nil)

    the version of the service.



75
76
77
78
# File 'lib/moleculer/service/base.rb', line 75

def version(ver = nil)
  @version = ver if ver
  @version
end

Instance Method Details

#actionsObject

returns the action defined on the service class

See Also:



118
119
120
# File 'lib/moleculer/service/base.rb', line 118

def actions
  self.class.actions
end

#brokerMoleculer::Broker

Returns the moleculer broker the service is attached to.

Returns:



131
132
133
# File 'lib/moleculer/service/base.rb', line 131

def broker
  self.class.broker
end

#eventsObject

returns the events defined on the service class

See Also:



125
126
127
# File 'lib/moleculer/service/base.rb', line 125

def events
  self.class.events
end