Skip to content
Browse files

Fixing a memory leak with set_observer

  • Loading branch information...
1 parent 854b74a commit bc5afc4acf98e5b47edd1bc023f86c8451b48ade @PlasticLizard committed Jan 13, 2011
Showing with 20 additions and 35 deletions.
  1. +0 −24 Gemfile.lock
  2. +19 −10 lib/observables/base.rb
  3. +1 −1 lib/observables/version.rb
View
24 Gemfile.lock
@@ -1,24 +0,0 @@
-PATH
- remote: .
- specs:
- observables (0.1.2)
- activesupport (~> 3.0.0)
- i18n
-
-GEM
- remote: http://rubygems.org/
- specs:
- activesupport (3.0.0)
- i18n (0.4.1)
- rake (0.8.7)
- shoulda (2.11.3)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- activesupport (~> 3.0.0)
- i18n
- observables!
- rake
- shoulda (~> 2.11)
View
29 lib/observables/base.rb
@@ -13,7 +13,8 @@ def notifier
end
def subscribe(pattern=nil,&block)
- notifier.subscribe(pattern,&block)
+ callback = block || self
+ notifier.subscribe(pattern,callback)
end
def unsubscribe(subscriber)
@@ -31,12 +32,20 @@ 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) do |*args|
- block ? block.call(self,*args) :
- (@_observer_owner.send(callback_method,self,*args) if @_observer_owner && @_observer_owner.respond_to?(callback_method))
- end
+ #callback_method = opts[:callback_method] || :child_changed
+ @_owner_subscription = subscribe(pattern)
+ end
+
+ 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
def clear_observer
@@ -85,10 +94,10 @@ def override_mutators(change_groups)
class_eval <<-EOS
def #{method}(*args,&block)
changes = changes_for(:#{change_type},:#{method},*args,&block)
- changing(:#{change_type},:trigger=>:#{method}, :changes=>changes){super}
- end
- EOS
- end
+ changing(:#{change_type},:trigger=>:#{method}, :changes=>changes){super}
+ end
+ EOS
+ end
end
end
end
View
2 lib/observables/version.rb
@@ -1,4 +1,4 @@
# encoding: UTF-8
module Observables
- Version = '0.1.2'
+ Version = '0.1.3'
end

0 comments on commit bc5afc4

Please sign in to comment.
Something went wrong with that request. Please try again.