Class: Orchestra::Conductor

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestra/conductor.rb

Defined Under Namespace

Classes: ServiceRecorder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services = {}) ⇒ Conductor

Returns a new instance of Conductor.



5
6
7
8
9
10
# File 'lib/orchestra/conductor.rb', line 5

def initialize services = {}
  @services = services
  @thread_pool = ThreadPool.new
  @observers = Set.new
  self.thread_count = Configuration.thread_count
end

Instance Attribute Details

#observersObject (readonly)

Returns the value of attribute observers.



3
4
5
# File 'lib/orchestra/conductor.rb', line 3

def observers
  @observers
end

#servicesObject (readonly)

Returns the value of attribute services.



3
4
5
# File 'lib/orchestra/conductor.rb', line 3

def services
  @services
end

#thread_poolObject (readonly)

Returns the value of attribute thread_pool.



3
4
5
# File 'lib/orchestra/conductor.rb', line 3

def thread_pool
  @thread_pool
end

Instance Method Details

#add_observer(observer) ⇒ Object



30
31
32
# File 'lib/orchestra/conductor.rb', line 30

def add_observer observer
  observers << observer
end

#build_registry(observable) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/orchestra/conductor.rb', line 43

def build_registry observable
  hsh = { :conductor => self }
  services.each_with_object hsh do |(service_name, _), hsh|
    service = resolve_service observable, service_name
    hsh[service_name] = service if service
  end
end

#copy_observers(observable) ⇒ Object



38
39
40
41
# File 'lib/orchestra/conductor.rb', line 38

def copy_observers observable
  add_observer = observable.method :add_observer
  observers.each &add_observer
end

#delete_observer(observer) ⇒ Object



34
35
36
# File 'lib/orchestra/conductor.rb', line 34

def delete_observer observer
  observers.delete observer
end

#perform(operation, input = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/orchestra/conductor.rb', line 12

def perform operation, input = {}
  operation.perform self, input do |performance|
    copy_observers performance
    yield performance if block_given?
  end
end

#record(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/orchestra/conductor.rb', line 19

def record *args
  recording = Recording.new
  add_observer recording
  perform *args do |performance|
    performance.add_observer recording
  end
  recording
ensure
  delete_observer recording
end

#resolve_service(observable, service_name) ⇒ Object



51
52
53
54
55
56
# File 'lib/orchestra/conductor.rb', line 51

def resolve_service observable, service_name
  return nil unless services.has_key? service_name
  service = Util.to_lazy_thunk services[service_name]
  recording = ServiceRecorder.new observable, service_name
  recording.wrap service.call self
end

#thread_countObject



58
59
60
# File 'lib/orchestra/conductor.rb', line 58

def thread_count
  @thread_pool.count
end

#thread_count=(new_count) ⇒ Object



62
63
64
# File 'lib/orchestra/conductor.rb', line 62

def thread_count= new_count
  @thread_pool.count = new_count
end