Class: BackgroundServices::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, implementation) ⇒ Service

Returns a new instance of Service.



6
7
8
# File 'lib/background_services/service.rb', line 6

def initialize(name, implementation)
  @name, @implementation = name, implementation
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/background_services/service.rb', line 4

def name
  @name
end

Instance Method Details

#process_job(worker, job) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/background_services/service.rb', line 50

def process_job(worker, job)
  case @implementation
    when Class
      raise "must respond to #process_job" unless worker.implementation.respond_to? :process_job
      worker.implementation.send :process_job, job
    when Module
      raise "must respond to #process_job" unless worker.implementation.respond_to? :process_job
      worker.implementation.send :process_job, job
    when Proc
      worker.implementation.call job
  end
end

#startObject



10
11
12
13
14
15
# File 'lib/background_services/service.rb', line 10

def start
  case @implementation
    when Class  then @implementation.send :start if @implementation.respond_to? :start
    when Module then @implementation.send :start if @implementation.respond_to? :start
  end
end

#start_worker(worker) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/background_services/service.rb', line 24

def start_worker(worker)
  case @implementation
    when Class
      worker.implementation = @implementation.new
      worker.implementation.send :start_worker if worker.implementation.respond_to? :start_worker
    when Module
      worker.implementation = @implementation
      worker.implementation.send :start_worker if worker.implementation.respond_to? :start_worker
    when Proc
      worker.implementation = @implementation
  end
end

#stopObject



17
18
19
20
21
22
# File 'lib/background_services/service.rb', line 17

def stop
  case @implementation
    when Class  then @implementation.send :stop if @implementation.respond_to? :stop
    when Module then @implementation.send :stop if @implementation.respond_to? :stop
  end
end

#stop_worker(worker) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/background_services/service.rb', line 37

def stop_worker(worker)
  case @implementation
    when Class
      worker.implementation.send :stop_worker if worker.implementation.respond_to? :stop_worker
      worker.implementation = nil
    when Module
      worker.implementation.send :stop_worker if worker.implementation.respond_to? :stop_worker
      worker.implementation = nil
    when Proc
      worker.implementation = nil
  end
end