Skip to content

Commit

Permalink
Refactored entries to support arguments being passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewind committed Jun 11, 2012
1 parent 78ebafb commit b872a58
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Source/JSObjectionBindingEntry.m
Expand Up @@ -10,7 +10,7 @@ - (id)initWithObject:(id)theObject {
return self;
}

- (id)extractObject {
- (id)extractObject:(NSArray *)arguments {
return _instance;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/JSObjectionEntry.h
Expand Up @@ -12,7 +12,7 @@ typedef enum {
@protocol JSObjectionEntry<NSObject>
@property (nonatomic, readonly) JSObjectionInstantiationRule lifeCycle;
@property (nonatomic, assign) JSObjectionInjector *injector;
- (id)extractObject;
- (id)extractObject:(NSArray *)arguments;
+ (id)entryWithEntry:(JSObjectionEntry *)entry;
@end

Expand Down
2 changes: 1 addition & 1 deletion Source/JSObjectionEntry.m
Expand Up @@ -4,7 +4,7 @@ @implementation JSObjectionEntry
@synthesize injector = _injector;
@dynamic lifeCycle;

- (id)extractObject {
- (id)extractObject:(NSArray *)arguments {
return nil;
}

Expand Down
7 changes: 6 additions & 1 deletion Source/JSObjectionInjector.m
@@ -1,6 +1,7 @@
#import "JSObjectionInjector.h"
#import "JSObjectionEntry.h"
#import "JSObjectFactory.h"
#import "JSObjectionUtils.h"

#import <pthread.h>
#import <objc/runtime.h>
Expand Down Expand Up @@ -95,7 +96,11 @@ - (id)getObject:(id)classOrProtocol, ... {
}

if (classOrProtocol && injectorEntry) {
return [injectorEntry extractObject];
va_list va_arguments;
va_start(va_arguments, classOrProtocol);
NSArray *arguments = JSObjectionUtils.transformVariadicArgsToArray(va_arguments);
va_end(va_arguments);
return [injectorEntry extractObject:arguments];
}

return nil;
Expand Down
8 changes: 3 additions & 5 deletions Source/JSObjectionInjectorEntry.m
Expand Up @@ -26,8 +26,7 @@ - (id)initWithClass:(Class)theClass lifeCycle:(JSObjectionInstantiationRule)theL
return self;
}

- (id)extractObject
{
- (id)extractObject:(NSArray *)arguments {
if (self.lifeCycle == JSObjectionInstantiationRuleNormal || !_storageCache) {
return [self buildObject];
}
Expand All @@ -45,8 +44,7 @@ - (void)dealloc
#pragma mark -
#pragma mark Private Methods

- (void)notifyObjectThatItIsReady: (id) object
{
- (void)notifyObjectThatItIsReady:(id)object {
if([object respondsToSelector:@selector(awakeFromObjection)]) {
[object performSelector:@selector(awakeFromObjection)];
}
Expand All @@ -55,7 +53,7 @@ - (void)notifyObjectThatItIsReady: (id) object
- (id)buildObject {
id objectUnderConstruction = [[[self.classEntry alloc] init] autorelease];

if (self.lifeCycle != JSObjectionInstantiationRuleNormal) {
if (self.lifeCycle == JSObjectionInstantiationRuleSingleton) {
_storageCache = [objectUnderConstruction retain];
}

Expand Down
2 changes: 1 addition & 1 deletion Source/JSObjectionProviderEntry.m
Expand Up @@ -19,7 +19,7 @@ - (id)initWithBlock:(id(^)(JSObjectionInjector *context))theBlock {
return self;
}

- (id)extractObject {
- (id)extractObject:(NSArray *)arguments {
if (_block) {
return _block(self.injector);
}
Expand Down
4 changes: 4 additions & 0 deletions Source/JSObjectionUtils.h
@@ -1,6 +1,9 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

NSString *const JSObjectionInitializerKey;
NSString *const JSObjectionDefaultArgumentsKey;

typedef enum {
JSObjectionTypeClass,
JSObjectionTypeProtocol
Expand All @@ -16,4 +19,5 @@ extern const struct JSObjectionUtils {
objc_property_t (*propertyForClass)(Class klass, NSString *propertyName);
NSSet* (*buildDependenciesForClass)(Class klass, NSSet *requirements);
NSDictionary* (*buildInitializer)(SEL selector, NSArray *arguments);
NSArray* (*transformVariadicArgsToArray)(va_list va_arguments);
} JSObjectionUtils;
21 changes: 19 additions & 2 deletions Source/JSObjectionUtils.m
Expand Up @@ -3,6 +3,9 @@

static NSString *const JSObjectionException = @"JSObjectionException";

NSString *const JSObjectionInitializerKey = @"initializer";
NSString *const JSObjectionDefaultArgumentsKey = @"arguments";

static JSObjectionPropertyInfo FindClassOrProtocolForProperty(objc_property_t property) {
NSString *attributes = [NSString stringWithCString: property_getAttributes(property) encoding: NSASCIIStringEncoding];
NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSASCIIStringEncoding];
Expand Down Expand Up @@ -53,7 +56,20 @@ static JSObjectionPropertyInfo FindClassOrProtocolForProperty(objc_property_t pr
}

static NSDictionary* BuildInitializer(SEL selector, NSArray *defaultArguments) {
return nil;
return [NSDictionary dictionaryWithObjectsAndKeys:
NSStringFromSelector(selector), JSObjectionInitializerKey,
defaultArguments, JSObjectionDefaultArgumentsKey
, nil];
}

static NSArray* TransformVariadicArgsToArray(va_list va_arguments) {
NSMutableArray *arguments = [NSMutableArray array];
id object;
while ((object = va_arg( va_arguments, id ))) {
[arguments addObject:object];
}

return [[arguments copy] autorelease];
}

static objc_property_t GetProperty(Class klass, NSString *propertyName) {
Expand All @@ -68,5 +84,6 @@ static objc_property_t GetProperty(Class klass, NSString *propertyName) {
.findClassOrProtocolForProperty = FindClassOrProtocolForProperty,
.propertyForClass = GetProperty,
.buildDependenciesForClass = BuildDependenciesForClass,
.buildInitializer = BuildInitializer
.buildInitializer = BuildInitializer,
.transformVariadicArgsToArray = TransformVariadicArgsToArray
};

0 comments on commit b872a58

Please sign in to comment.