forked from atomicobject/objection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,61 @@ | ||
#import "ObjectionInjector.h" | ||
#import "ObjectionInstanceEntry.h" | ||
#import <pthread.h> | ||
|
||
static NSMutableDictionary *gAOContext; | ||
static NSMutableDictionary *gObjectionContext; | ||
static pthread_mutex_t gObjectionMutex; | ||
|
||
@implementation ObjectionInjector | ||
|
||
+ (void)initialize { | ||
if (self == [ObjectionInjector class]) { | ||
gAOContext = [[NSMutableDictionary alloc] init]; | ||
gObjectionContext = [[NSMutableDictionary alloc] init]; | ||
pthread_mutexattr_t mutexattr; | ||
pthread_mutexattr_init(&mutexattr); | ||
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE); | ||
pthread_mutex_init(&gObjectionMutex, &mutexattr); | ||
pthread_mutexattr_destroy(&mutexattr); | ||
} | ||
} | ||
|
||
+ (void) registerObject:(id)theObject forClass:(Class)theClass { | ||
[gAOContext setObject:[[[ObjectionInstanceEntry alloc] initWithObject:theObject] autorelease] forKey:NSStringFromClass(theClass)]; | ||
pthread_mutex_lock(&gObjectionMutex); | ||
[gObjectionContext setObject:[[[ObjectionInstanceEntry alloc] initWithObject:theObject] autorelease] forKey:NSStringFromClass(theClass)]; | ||
pthread_mutex_unlock(&gObjectionMutex); | ||
} | ||
|
||
+ (void) registerClass:(Class)theClass lifeCycle:(ObjectionInstantiationRule)lifeCycle { | ||
pthread_mutex_lock(&gObjectionMutex); | ||
if (lifeCycle != ObjectionInstantiationRule_Singleton && lifeCycle != ObjectionInstantiationRule_Everytime) { | ||
@throw [NSException exceptionWithName:@"ObjectionInjectorException" reason:@"Invalid Instantiation Rule" userInfo:nil]; | ||
} | ||
|
||
if (theClass && [gAOContext objectForKey:NSStringFromClass(theClass)] == nil) { | ||
[gAOContext setObject:[ObjectionEntry withClass:theClass lifeCycle:lifeCycle andContext:self] forKey:NSStringFromClass(theClass)]; | ||
if (theClass && [gObjectionContext objectForKey:NSStringFromClass(theClass)] == nil) { | ||
[gObjectionContext setObject:[ObjectionEntry withClass:theClass lifeCycle:lifeCycle andContext:self] forKey:NSStringFromClass(theClass)]; | ||
} | ||
pthread_mutex_unlock(&gObjectionMutex); | ||
} | ||
|
||
+ (id)getObject:(Class)theClass { | ||
NSString *key = NSStringFromClass(theClass); | ||
ObjectionEntry *entry = [gAOContext objectForKey:key]; | ||
if (theClass && entry) { | ||
return [entry extractObject]; | ||
} | ||
pthread_mutex_lock(&gObjectionMutex); | ||
@try { | ||
NSString *key = NSStringFromClass(theClass); | ||
ObjectionEntry *entry = [gObjectionContext objectForKey:key]; | ||
if (theClass && entry) { | ||
return [entry extractObject]; | ||
} | ||
} | ||
@finally { | ||
pthread_mutex_unlock(&gObjectionMutex); | ||
} | ||
|
||
return nil; | ||
|
||
} | ||
|
||
+ (void) reset { | ||
[gAOContext removeAllObjects]; | ||
pthread_mutex_lock(&gObjectionMutex); | ||
[gObjectionContext removeAllObjects]; | ||
pthread_mutex_unlock(&gObjectionMutex); | ||
} | ||
@end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#import "SpecHelper.h" | ||
|
||
@interface Person : NSObject | ||
{ | ||
NSDictionary *_attributes; | ||
} | ||
|
||
@property (nonatomic, retain) NSDictionary *attributes; | ||
@end | ||
|
||
@implementation Person | ||
objection_register(@"Person") | ||
objection_requires(@"attributes") | ||
@synthesize attributes=_attributes; | ||
@end | ||
|
||
@interface Programmer : Person | ||
{ | ||
NSDictionary *_favoriteLanguages; | ||
} | ||
@property (nonatomic, retain) NSDictionary *favoriteLanguages; | ||
@end | ||
|
||
@implementation Programmer | ||
objection_register(@"Programmer") | ||
objection_requires(@"favoriteLanguages") | ||
@synthesize favoriteLanguages=_favoriteLanguages; | ||
|
||
@end | ||
|
||
@interface NoInheritence : NSObject | ||
{ | ||
NSString *_something; | ||
} | ||
|
||
@property (nonatomic, retain) NSString *something; | ||
|
||
@end | ||
|
||
@implementation NoInheritence | ||
objection_register(@"NoInheritence") | ||
objection_requires(@"something") | ||
|
||
@synthesize something=_something; | ||
|
||
@end | ||
|
||
|
||
|
||
|
||
|
||
SPEC_BEGIN(InheritenceSpecs) | ||
it(@"coalesces dependencies from parent to child", ^{ | ||
Programmer *programmer = [ObjectionInjector getObject:[Programmer class]]; | ||
assertThat(programmer.attributes, is(notNilValue())); | ||
assertThat(programmer.favoriteLanguages, is(notNilValue())); | ||
}); | ||
|
||
it(@"does not throw a fit if the base class does not implement super", ^{ | ||
NoInheritence *noParentObjectWithRequires = [ObjectionInjector getObject:[NoInheritence class]]; | ||
assertThat(noParentObjectWithRequires.something, is(notNilValue())); | ||
}); | ||
SPEC_END |
This file contains 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