Module: Observables::Base::InstanceMethods

Defined in:
lib/observables/base.rb

Instance Method Summary collapse

Instance Method Details

#clear_observerObject



42
43
44
45
# File 'lib/observables/base.rb', line 42

def clear_observer
  unsubscribe(@_owner_subscription) if @_owner_subscription
  @_owner_subscription = nil
end

#dupObject



23
24
25
26
27
28
# File 'lib/observables/base.rb', line 23

def dup
  #This check is necessary in case a proxy is made observable,
  #with an impementation of dup that returns its non-observable
  #target
  super.tap {|s| s.make_observable if s.respond_to?(:make_observable) }
end

#notifierObject



11
12
13
# File 'lib/observables/base.rb', line 11

def notifier
  @notifier ||= ActiveSupport::Notifications::Fanout.new
end

#set_observer(*args, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/observables/base.rb', line 30

def set_observer(*args,&block)
  clear_observer
  opts = args.extract_options!
  @_observer_owner = args.pop
  pattern = opts[:pattern] || /.*/
  callback_method = opts[:callback_method] || :child_changed
  @_owner_subscription = subscribe(pattern) do |*args|
    block ? block.call(self,*args) :
            (@_observer_owner.send(callback_method,self,*args) if @_observer_owner && @_observer_owner.respond_to?(callback_method))
  end
end

#subscribe(pattern = nil, &block) ⇒ Object



15
16
17
# File 'lib/observables/base.rb', line 15

def subscribe(pattern=nil,&block)
  notifier.subscribe(pattern,&block)
end

#unsubscribe(subscriber) ⇒ Object



19
20
21
# File 'lib/observables/base.rb', line 19

def unsubscribe(subscriber)
  notifier.unsubscribe(subscriber)
end