Class: Bowline::Observable

Inherits:
Object show all
Defined in:
lib/bowline/observer.rb

Defined Under Namespace

Classes: Event

Instance Method Summary collapse

Constructor Details

#initializeObservable

Returns a new instance of Observable.



33
34
35
# File 'lib/bowline/observer.rb', line 33

def initialize
  @listeners = {}
end

Instance Method Details

#append(event, method = nil, &block) ⇒ Object



37
38
39
40
41
# File 'lib/bowline/observer.rb', line 37

def append(event, method = nil, &block)
  event = Event.new(self, event, method||block)
  (@listeners[event] ||= []) << event
  event
end

#call(event, *args) ⇒ Object



43
44
45
46
47
48
# File 'lib/bowline/observer.rb', line 43

def call(event, *args)
  return unless @listeners[event]
  @listeners[event].each do |callback|
    callback.call(*args)
  end
end

#clearObject



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

def clear
  @listeners = {}
end

#remove(event, value = nil) ⇒ Object



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

def remove(event, value=nil)
  return unless @listeners[event]
  if value
    @listeners[event].delete(value)
    if @listeners[event].empty?
      @listeners.delete(event)
    end
  else
    @listeners.delete(event)
  end
end