Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to execute the binding immediately #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions KVOBlockBinding/NSObject+KVOBlockBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ typedef void (^WSObservationBlock)(id observed, NSDictionary *change);
* @return An array containing the binding and it's reverse, if specified. This does NOT need to be retained
*/
- (NSMutableArray *) bind:(id)source keyPath:(NSString *)sourcePath to:(id) target keyPath:(NSString *)targetPath addReverseBinding:(BOOL)addReverseBinding;
/**
* Same as above except that it will perform an immediate update of the target if executeBinding is YES
*
* @param source The source object to observe
* @param sourcePath The keypath of the property to observe on the source using KVO
* @param target The target object to observe
* @param targetPath The keypath of the property to observe on the target using KVO
* @param executeBinding update the target keypath immediately if YES
* @return An array containing the binding and it's reverse, if specified. This does NOT need to be retained
*/
- (NSMutableArray *) bind:(id)source keyPath:(NSString *)sourcePath to:(id) target keyPath:(NSString *)targetPath addReverseBinding:(BOOL)addReverseBinding executeBinding:(BOOL)executeBinding;
@end

@interface NSObject (KVOBlockBinding)
Expand Down
12 changes: 12 additions & 0 deletions KVOBlockBinding/NSObject+KVOBlockBinding.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ - (NSMutableArray *) bind:(id)source keyPath:(NSString *)sourcePath to:(id) targ

return bindings;
}
- (NSArray *) bind:(id)source keyPath:(NSString *)sourcePath to:(id) target keyPath:(NSString *)targetPath addReverseBinding:(BOOL)addReverseBinding executeBinding:(BOOL)executeBinding {
NSArray *bindings = [self bind:source keyPath:sourcePath to: target keyPath:targetPath addReverseBinding:addReverseBinding];

if (executeBinding) {
id value = [source valueForKey:sourcePath];
if (value == [NSNull null]) {
value = nil;
}
[target setValue: value forKey:targetPath];
}
return bindings;
}

@end

Expand Down
14 changes: 9 additions & 5 deletions KVOBlockBindingTests/KVOBlockBindingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ - (void)testShouldNotCallBlockWhenAnotherKeyPathPropertyChanged

- (void) testTwoWayBinding
{
NSMutableArray *bindings = [self bind:self.model keyPath:@"exampleValue1" to:self.model keyPath:@"exampleValue2" addReverseBinding:YES];
[self bind:self.model keyPath:@"exampleValue1" to:self.model keyPath:@"exampleValue2" addReverseBinding:YES];

self.model.exampleValue1 = 1; // no change
STAssertFalse(self.model.exampleValue2 == self.model.exampleValue1, @"Identical changes should not trigger an update of the target object");
Expand All @@ -106,10 +106,14 @@ - (void) testTwoWayBinding
self.model.exampleValue2 = 20;
STAssertTrue(self.model.exampleValue1 == self.model.exampleValue2, @"Target/Source binding did not product expected result. Expected: %d, Actual: %d",self.model.exampleValue2,self.model.exampleValue1);

[self removeAllObservationsOn:self.model];
}
- (void) testExecuteBinding
{
[self bind:self.model keyPath:@"exampleValue1" to:self.model keyPath:@"exampleValue2" addReverseBinding:YES executeBinding:YES];

STAssertTrue(self.model.exampleValue1 == self.model.exampleValue2, @"Execute binding did not product expected result. Expected: %d, Actual: %d",self.model.exampleValue1,self.model.exampleValue2);

for (WSObservationBinding *binder in bindings) {
[binding invalidate];
[[self allBlockBasedObservations] removeObject:binder];
}
[self removeAllObservationsOn:self.model];
}
@end