Class: Moleculer::Service::Base Abstract
- Inherits:
-
Object
- Object
- Moleculer::Service::Base
- Defined in:
- lib/moleculer/service/base.rb
Overview
subclass to define a local service.
Direct Known Subclasses
Class Attribute Summary collapse
- .broker ⇒ Object
-
.service_prefix ⇒ string
A prefix to add to the service name.
Class Method Summary collapse
-
.action(name, method, options = {}) ⇒ Object
Defines an action on the service.
- .action_name_for(name) ⇒ Object
- .actions ⇒ Object
-
.event(name, method, options = {}) ⇒ Object
Defines an event on the service.
- .events ⇒ Object
-
.full_name ⇒ String
Returns the full name of the service, including version and prefix.
- .node ⇒ Object
-
.service_name(name = nil) ⇒ Object
Set the service_name to the provided name.
-
.to_h ⇒ Object
rubocop:disable Metrics/AbcSize.
-
.version(ver = nil) ⇒ Object
Defines a version name or number on the service.
Instance Method Summary collapse
-
#actions ⇒ Object
returns the action defined on the service class.
-
#broker ⇒ Moleculer::Broker
The moleculer broker the service is attached to.
-
#events ⇒ Object
returns the events defined on the service class.
-
#initialize(broker) ⇒ Base
constructor
A new instance of Base.
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
.broker ⇒ Object
29 30 31 |
# File 'lib/moleculer/service/base.rb', line 29 def broker @broker || Moleculer.broker end |
.service_prefix ⇒ string
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.
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.
56 57 58 |
# File 'lib/moleculer/service/base.rb', line 56 def action(name, method, = {}) actions[action_name_for(name)] = Action.new(name, self, method, ) 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 |
.actions ⇒ Object
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.
67 68 69 |
# File 'lib/moleculer/service/base.rb', line 67 def event(name, method, = {}) events[name] = Event.new(name, self, method, ) end |
.events ⇒ Object
84 85 86 |
# File 'lib/moleculer/service/base.rb', line 84 def events @events ||= {} end |
.full_name ⇒ 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 |
.node ⇒ Object
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
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_h ⇒ Object
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.
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
#actions ⇒ Object
returns the action defined on the service class
118 119 120 |
# File 'lib/moleculer/service/base.rb', line 118 def actions self.class.actions end |
#broker ⇒ Moleculer::Broker
Returns the moleculer broker the service is attached to.
131 132 133 |
# File 'lib/moleculer/service/base.rb', line 131 def broker self.class.broker end |
#events ⇒ Object
returns the events defined on the service class
125 126 127 |
# File 'lib/moleculer/service/base.rb', line 125 def events self.class.events end |