Class: Async::Service::Generic

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

Overview

Captures the stateful behaviour of a specific service. Specifies the interfaces required by derived classes.

Designed to be invoked within an Controller::Container.

Direct Known Subclasses

Managed::Service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, evaluator = environment.evaluator) ⇒ Generic

Initialize the service from the given environment.



35
36
37
38
# File 'lib/async/service/generic.rb', line 35

def initialize(environment, evaluator = environment.evaluator)
	@environment = environment
	@evaluator = evaluator
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



41
42
43
# File 'lib/async/service/generic.rb', line 41

def environment
  @environment
end

#The environment which is used to configure the service.(environmentwhichisusedtoconfiguretheservice.) ⇒ Object (readonly)



41
# File 'lib/async/service/generic.rb', line 41

attr :environment

Class Method Details

.wrap(environment) ⇒ Object

Convert the given environment into a service if possible.

If the evaluator responds to ‘make_service`, it is called with the environment and its return value is used as the service. This allows environments to compose child environments and return a concrete service without a dedicated service class.

Otherwise, the evaluator’s ‘service_class` is instantiated with the environment and evaluator as arguments.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/async/service/generic.rb', line 21

def self.wrap(environment)
	evaluator = environment.evaluator
	
	if evaluator.respond_to?(:make_service)
		return evaluator.make_service(environment)
	elsif evaluator.key?(:service_class)
		if service_class = evaluator.service_class
			return service_class.new(environment, evaluator)
		end
	end
end

Instance Method Details

#nameObject

The name of the service - used for informational purposes like logging. e.g. ‘myapp.com`.



51
52
53
# File 'lib/async/service/generic.rb', line 51

def name
	@evaluator.name
end

#setup(container) ⇒ Object

Setup the service into the specified container.



62
63
64
# File 'lib/async/service/generic.rb', line 62

def setup(container)
	Console.debug(self){"Setting up service #{self.name}..."}
end

#startObject

Start the service. Called before the container setup.



56
57
58
# File 'lib/async/service/generic.rb', line 56

def start
	Console.debug(self){"Starting service #{self.name}..."}
end

#stop(graceful = true) ⇒ Object

Stop the service. Called after the container is stopped.



67
68
69
# File 'lib/async/service/generic.rb', line 67

def stop(graceful = true)
	Console.debug(self){"Stopping service #{self.name}..."}
end

#to_hObject

Convert the service evaluator to a hash.



45
46
47
# File 'lib/async/service/generic.rb', line 45

def to_h
	@evaluator.to_h
end