Module: Observables::Base::InstanceMethods

Defined in:
lib/observables/base.rb

Instance Method Summary collapse

Instance Method Details

#call(*args) ⇒ Object



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

def call(*args)
  block = @_observer_owner_block
  callback_method = @_observer_owner_callback_method

  block ? block.call(self,*args) :
    (@_observer_owner.send(callback_method,self,*args) if @_observer_owner && @_observer_owner.respond_to?(callback_method))
end

#clear_observerObject



51
52
53
54
# File 'lib/observables/base.rb', line 51

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

#dupObject



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

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



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

def set_observer(*args,&block)
  clear_observer
  opts = args.extract_options!
  @_observer_owner = args.pop
  @_observer_owner_callback_method = opts[:callback_method] || :child_changed
  @_observer_owner_block = block

  pattern = opts[:pattern] || /.*/
  #callback_method = opts[:callback_method] || :child_changed
  @_owner_subscription = subscribe(pattern)
end

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



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

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

#unsubscribe(subscriber) ⇒ Object



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

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