diff --git a/ConciseKit.podspec b/ConciseKit.podspec index f8c45c5..2967e18 100644 --- a/ConciseKit.podspec +++ b/ConciseKit.podspec @@ -1,16 +1,16 @@ Pod::Spec.new do |s| s.name = 'ConciseKit' - s.version = '0.1.2' + s.version = '0.1.3' s.license = 'MIT' s.summary = 'A set of Objective-C additions and macros that lets you write code more quickly.' s.homepage = 'http://github.com/petejkim/ConciseKit' s.author = { 'Peter Jihoon Kim' => 'raingrove@gmail.com' } - s.source = { :git => 'http://github.com/petejkim/ConciseKit.git', :tag => 'v0.1.2' } + s.source = { :git => 'http://github.com/goodtohear/ConciseKit.git' } s.source_files = 'src/**/*.{h,m}' - s.clean_paths = 'ConciseKitSpecs' + s.preserve_paths = 'ConciseKitSpecs' s.frameworks = 'Foundation' end diff --git a/ConciseKitSpecs/ConciseKitSpecs.xcodeproj/project.pbxproj b/ConciseKitSpecs/ConciseKitSpecs.xcodeproj/project.pbxproj index 34f4d51..1ce7483 100644 --- a/ConciseKitSpecs/ConciseKitSpecs.xcodeproj/project.pbxproj +++ b/ConciseKitSpecs/ConciseKitSpecs.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 0924B4D71562944D008176FF /* Listener.m in Sources */ = {isa = PBXBuildFile; fileRef = 0924B4D61562944D008176FF /* Listener.m */; }; CB6C70DD14E21B3A0016D5D7 /* NSSetConciseKitSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = CB6C70DC14E21B3A0016D5D7 /* NSSetConciseKitSpec.m */; }; E90DAB9D12A66B1D000A36AE /* Foo.m in Sources */ = {isa = PBXBuildFile; fileRef = E90DAB9C12A66B1D000A36AE /* Foo.m */; }; E90DADE612A6A07A000A36AE /* CKMocks.m in Sources */ = {isa = PBXBuildFile; fileRef = E90DADE512A6A07A000A36AE /* CKMocks.m */; }; @@ -49,6 +50,9 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 0924B4D51562944D008176FF /* Listener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Listener.h; sourceTree = ""; }; + 0924B4D61562944D008176FF /* Listener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Listener.m; sourceTree = ""; }; + 0924B4D91562976A008176FF /* CKNotifications.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKNotifications.h; sourceTree = ""; }; 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = ""; }; 13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = ""; }; 29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = ""; }; @@ -145,6 +149,8 @@ E90DAB9C12A66B1D000A36AE /* Foo.m */, E90DADE412A6A07A000A36AE /* CKMocks.h */, E90DADE512A6A07A000A36AE /* CKMocks.m */, + 0924B4D51562944D008176FF /* Listener.h */, + 0924B4D61562944D008176FF /* Listener.m */, ); path = Fixtures; sourceTree = ""; @@ -182,6 +188,7 @@ children = ( E95368A51521CA5A00AA3B81 /* CKAdditions.h */, E95368A61521CA5A00AA3B81 /* CKMacros.h */, + 0924B4D91562976A008176FF /* CKNotifications.h */, E95368A71521CA5A00AA3B81 /* CKSingleton.h */, E95368A81521CA5A00AA3B81 /* ConciseKit.h */, E95368A91521CA5A00AA3B81 /* ConciseKit.m */, @@ -276,6 +283,7 @@ E95368B11521CA5A00AA3B81 /* NSArray+ConciseKit.m in Sources */, E95368B21521CA5A00AA3B81 /* NSDictionary+ConciseKit.m in Sources */, E95368B31521CA5A00AA3B81 /* NSString+ConciseKit.m in Sources */, + 0924B4D71562944D008176FF /* Listener.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/ConciseKitSpecs/Spec/ConciseKitSpec.m b/ConciseKitSpecs/Spec/ConciseKitSpec.m index a08233d..8eb1786 100644 --- a/ConciseKitSpecs/Spec/ConciseKitSpec.m +++ b/ConciseKitSpecs/Spec/ConciseKitSpec.m @@ -2,6 +2,7 @@ #import "ConciseKit.h" #import "Foo.h" #import "CKMocks.h" +#import "Listener.h" #if __has_feature(objc_arc) #define ARCLESS(block) @@ -189,5 +190,30 @@ NSString *foo = @"foo"; assertThat($safe(foo), equalTo(@"foo")); }); + + + describe(@"$listen", ^{ + NSString * notificationName = @"foo"; + __block Listener * listener; + beforeEach(^{ + listener = [[Listener alloc] init]; + }); + it(@"supports plain $notify", ^{ + assertThatBool(listener.notificationWasReceived, equalToBool(NO)); + $listen(notificationName, listener, @selector(receiveNotification)); + $notify(notificationName); + assertThatBool(listener.notificationWasReceived, equalToBool(YES)); + }); + it(@"supports $notifyWithObject:", ^{ + NSObject * foo = [[NSObject alloc] init]; + assertThatBool(listener.notificationWasReceived, equalToBool(NO)); + assertThat(listener.objectReceivedViaNotification, equalTo(nil)); + $listen(notificationName, listener, @selector(receiveNotification:)); + $notifyWithObject(notificationName,foo); + assertThatBool(listener.notificationWasReceived, equalToBool(YES)); + assertThat(listener.objectReceivedViaNotification,is(foo)); + }); + }); + } DESCRIBE_END \ No newline at end of file diff --git a/ConciseKitSpecs/Spec/Fixtures/Listener.h b/ConciseKitSpecs/Spec/Fixtures/Listener.h new file mode 100644 index 0000000..0add183 --- /dev/null +++ b/ConciseKitSpecs/Spec/Fixtures/Listener.h @@ -0,0 +1,9 @@ +#import + +@interface Listener : NSObject +@property (readwrite) BOOL notificationWasReceived; +@property (nonatomic, strong) NSObject * objectReceivedViaNotification; + +-(void)receiveNotification; +-(void)receiveNotification:(NSNotification*)notification; +@end diff --git a/ConciseKitSpecs/Spec/Fixtures/Listener.m b/ConciseKitSpecs/Spec/Fixtures/Listener.m new file mode 100644 index 0000000..7c29c0a --- /dev/null +++ b/ConciseKitSpecs/Spec/Fixtures/Listener.m @@ -0,0 +1,12 @@ +#import "Listener.h" + +@implementation Listener +@synthesize notificationWasReceived,objectReceivedViaNotification; +-(void)receiveNotification{ + self.notificationWasReceived = YES; +} +-(void)receiveNotification:(NSNotification *)notification{ + [self receiveNotification]; + self.objectReceivedViaNotification = notification.object; +} +@end diff --git a/README.md b/README.md index 70e0020..4074ead 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,11 @@ Useful when writing tests for asynchronous tasks. Default timeout is 10 seconds, ### NSValue shorthands $nonretained(), $pointer(), $point(), $range(), $rect(), $size() +### NSNotificationCenter shorthands + $listen(@"foo", self, @selector(handleFoo:)) => [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleFoo:) name:@"foo" object:nil] + $notify(@"foo") => [[NSNotificationCenter defaultCenter] postNotificationName:@"foo" object:nil] + $notifyWithObject(@"foo", bar) => [[NSNotificationCenter defaultCenter] postNotificationName:@"foo" object:bar] + ## Additions ### NSArray @@ -226,11 +231,13 @@ Useful when writing tests for asynchronous tasks. Default timeout is 10 seconds, [string $insert:@"foo" at:1] => [string insertString:@"foo" atIndex:1] (+ returns self) [string $set:@"foo"] => [string setString:@"foo"] (+ returns self) + ## Contributors * [nolanw](http://github.com/nolanw) * [listrophy](https://github.com/listrophy) * [gerry3](https://github.com/gerry3) @ [Inigral](https://github.com/inigral) +* [michaelforrest](https://github.com/michaelforrest) @ [Good To Hear](https://github.com/goodtohear) ## License diff --git a/src/CKNotifications.h b/src/CKNotifications.h new file mode 100644 index 0000000..479a3d0 --- /dev/null +++ b/src/CKNotifications.h @@ -0,0 +1,16 @@ +#import + +static __inline__ void +$listen(NSString * eventName, NSObject * target, SEL action) +{ + [[NSNotificationCenter defaultCenter] addObserver:target selector:action name:eventName object:nil]; +} + +static __inline__ void +$notify(NSString * eventName){ + [[NSNotificationCenter defaultCenter] postNotificationName:eventName object:nil]; +} +static __inline__ void +$notifyWithObject(NSString * eventName, __strong NSObject * object){ + [[NSNotificationCenter defaultCenter] postNotificationName:eventName object:object]; +} \ No newline at end of file diff --git a/src/ConciseKit.h b/src/ConciseKit.h index 0f4238f..f6e46c6 100644 --- a/src/ConciseKit.h +++ b/src/ConciseKit.h @@ -2,6 +2,7 @@ #import "CKMacros.h" #import "CKAdditions.h" #import "CKSingleton.h" +#import "CKNotifications.h" @interface ConciseKit : NSObject {}