Module: Twterm::Subscriber

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



27
28
29
30
31
# File 'lib/twterm/subscriber.rb', line 27

def self.included(base)
  base.instance_eval do
    private :subscribe, :unsubscribe
  end
end

Instance Method Details

#subscribe(event, callback = nil, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/twterm/subscriber.rb', line 5

def subscribe(event, callback = nil, &block)
  cb = if callback.is_a?(Proc)
         callback
       elsif callback.is_a?(Symbol)
         if self.respond_to?(callback, true)
           self.method(callback)
         else
           callback.to_proc
         end
       elsif callback.nil?
         block
       end

  EventDispatcher.instance.register_subscription(object_id, event, cb)
end

#unsubscribe(event = nil) ⇒ Object



21
22
23
24
25
# File 'lib/twterm/subscriber.rb', line 21

def unsubscribe(event = nil)
  EventDispatcher.instance.unregister_subscription(object_id, event)

  self
end