Class: Cordis::Service

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

Overview

Class-style plugin that provides itself as a service (upstream Service base class). Declare the name with provide :foo and dependencies with inject; the instance becomes the service value, and #init (if defined) runs as the plugin load body.

class AuditLog < Cordis::Service
provide :audit
inject :db
def init = ctx.on('request') { ... }
end
ctx.plugin(AuditLog)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx, config = nil) ⇒ Service

Returns a new instance of Service.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
# File 'lib/cordis/service.rb', line 34

def initialize(ctx, config = nil)
  name = self.class.provide
  raise ArgumentError, "#{self.class} must declare `provide :name`" unless name

  @ctx = ctx
  @config = config
  @name = name
  ctx.provide(name, self)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



32
33
34
# File 'lib/cordis/service.rb', line 32

def config
  @config
end

#ctxObject (readonly)

Returns the value of attribute ctx.



32
33
34
# File 'lib/cordis/service.rb', line 32

def ctx
  @ctx
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/cordis/service.rb', line 32

def name
  @name
end

Class Method Details

.inject(*names, **configs) ⇒ Object

inject :foo, :bar or inject foo: { config } (upstream static inject); called with no arguments it returns the accumulated map for the registry.



24
25
26
27
28
29
# File 'lib/cordis/service.rb', line 24

def inject(*names, **configs)
  @inject ||= {}
  names.each { |dep| @inject[dep.to_sym] = true }
  configs.each { |dep, cfg| @inject[dep.to_sym] = cfg }
  @inject
end

.provide(name = nil) ⇒ Object

DSL setter and registry-facing getter in one (upstream static provide).



17
18
19
20
# File 'lib/cordis/service.rb', line 17

def provide(name = nil)
  @service_name = name.to_sym if name
  @service_name
end

Instance Method Details

#initObject

Load-time hook: runs while the fiber is still loading, so blocking here keeps the service invisible to dependents (upstream's "pending inject" via Service.init). A callable return value is collected as a disposer.



47
# File 'lib/cordis/service.rb', line 47

def init; end