Module: ObservablePool

Included in:
Daemon
Defined in:
lib/observable_pool.rb

Defined Under Namespace

Classes: Observer

Instance Method Summary collapse

Instance Method Details

#add_global_observer(observer) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/observable_pool.rb', line 124

def add_global_observer(observer)
  if defined? @observable_peers
    @observable_peers.each do |observable, obsv|
      obsv.add_observer(observer) unless observable.observer?(observer)
    end
  end
  @global_observer_peers = [] unless defined? @global_observer_peers
  @global_observer_peers << observer
end

#add_global_observers(*observers) ⇒ Object



134
135
136
# File 'lib/observable_pool.rb', line 134

def add_global_observers(*observers)
  observers.each { |observer| add_global_observer(observer) }
end

#add_observable(observable) ⇒ Object

class Observer



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/observable_pool.rb', line 39

def add_observable(observable)
  unless observable.is_a?(Observable)
    raise(ArgumentError, "observable object must be Observale")
  end
  @observable_peer_mutex = Mutex.new unless defined? @observable_peer_mutex
  @observable_peers = {} unless defined? @observable_peers
  sync_observer = Observer.new(observable, @observable_peer_mutex)
  @observable_peers[observable] = sync_observer
  @global_observer_peers = [] unless defined? @global_observer_peers
  @global_observer_peers.each do |observer|
    sync_observer.add_observer(observer)
  end
  sync_observer
end

#add_observables(*observables) ⇒ Object



54
55
56
# File 'lib/observable_pool.rb', line 54

def add_observables(*observables)
  observables.each { |observable| add_observable(observable) }
end

#count_all_observersObject



120
121
122
# File 'lib/observable_pool.rb', line 120

def count_all_observers
  list_observers.size
end

#count_global_observersObject



163
164
165
166
167
168
169
# File 'lib/observable_pool.rb', line 163

def count_global_observers
  if defined? @global_observer_peers
    @globale.observer_peers.size
  else
    0
  end
end

#count_observablesObject



94
95
96
97
98
99
100
# File 'lib/observable_pool.rb', line 94

def count_observables
  if defined? @observable_peers
    @observable_peers.size
  else
    0
  end
end

#delete_global_observer(observer) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/observable_pool.rb', line 138

def delete_global_observer(observer)
  if defined? @observable_peers
    @observable_peers.each do |observable, obsv|
      obsv.delete_observer(observer)
    end
  end
  @global_observer_peers.delete(observer) if defined? @global_observer_peers
end

#delete_global_observersObject



147
148
149
150
151
152
153
# File 'lib/observable_pool.rb', line 147

def delete_global_observers
  if defined? @global_observer_peers
    @global_observer_peers.dup.each do |global_observer|
      delete_global_observer(global_observer)
    end
  end
end

#delete_observable(observable) ⇒ Object



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

def delete_observable(observable)
  @observable_peers.delete(observable) if defined? @observable_peers
end

#delete_observablesObject



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

def delete_observables
  @observable_peers.clear if defined? @observable_peers
end

#each_observables(&block) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/observable_pool.rb', line 102

def each_observables(&block)
  if defined? @observable_peers
    @observable_peers.each do |observable, observer|
      block[observable, observer]
    end
  end
end

#global_observer?(observer) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
175
176
177
# File 'lib/observable_pool.rb', line 171

def global_observer?(observer)
  if defined? @global_observer_peers
    @global_observer_peers.include?(observer)
  else
    false
  end
end

#list_all_observersObject



110
111
112
113
114
115
116
117
118
# File 'lib/observable_pool.rb', line 110

def list_all_observers
  observers = []
  if defined? @observable_peers
    @observable_peers.each do |observable, observer|
      observers += observer.list_observers
    end
  end
  observers.uniq
end

#list_global_observersObject



155
156
157
158
159
160
161
# File 'lib/observable_pool.rb', line 155

def list_global_observers
  if defined? @global_observer_peers
    @global_observer_peers.dup
  else
    []
  end
end

#list_observablesObject



74
75
76
77
78
79
80
# File 'lib/observable_pool.rb', line 74

def list_observables
  if defined? @observable_peers
    @observable_peers.keys
  else
    []
  end
end

#list_observables_observed(observer) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/observable_pool.rb', line 82

def list_observables_observed(observer)
  observables = []
  if defined? @observable_peers
    @observable_peers.each do |observable, obsv|
      if obsv.list_observers.include?(observer)
        observables << observable
      end
    end
  end
  observables
end

#observable(observable) ⇒ Object



66
67
68
# File 'lib/observable_pool.rb', line 66

def observable(observable)
  @observable_peers[observable] if defined? @observable_peers
end

#observable?(observable) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/observable_pool.rb', line 70

def observable?(observable)
  @observable_peers.has_key?(observable) if defined? @observable_peers
end