Created
July 11, 2012 14:58
-
-
Save cowboyd/3090953 to your computer and use it in GitHub Desktop.
To leverage baked in KVO for collections, you need to update collection properties via a proxy (or some other KVC compliant way)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'an object with an observed to-many relationship' do | |
before do | |
@object = Class.new.class_eval do | |
attr_accessor :collection | |
def initialize | |
@collection = [] | |
end | |
new | |
end | |
@observer = Class.new.class_eval do | |
attr_reader :observation | |
def observeValueForKeyPath(path, ofObject:target, change:change, context:context) | |
@observation = change | |
end | |
new | |
end | |
@object.addObserver(@observer, forKeyPath:"collection", options:0, context:nil) | |
end | |
describe '. Just adding an object to the collection' do | |
before do | |
@object.collection << Object.new | |
end | |
it 'does not send KVO notifications' do | |
@observation.should == nil | |
end | |
end | |
describe '. Adding an object to the collection via a proxy' do | |
before do | |
@object.mutableArrayValueForKey("collection") << Object.new | |
end | |
it '*does* send KVO notifications' do | |
@observer.observation.should.not == nil | |
end | |
end | |
describe '. A BubbleWrap observer' do | |
before do | |
@bubble = Object.new | |
@bubble.extend BW::KVO | |
@bubble.observe(@object, 'collection') do |old, new, indexes| | |
@old, @new, @indexes = old, new, indexes | |
end | |
end | |
it 'is also notified of collection changes when an object is added' do | |
@object.mutableArrayValueForKey("collection") << Object.new | |
@new.should.not == nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment