Permalink
Browse files

Refactored entries to support arguments being passed in

  • Loading branch information...
1 parent 78ebafb commit b872a58e110c1d83005a54018facb34296f274c0 @thestoics thestoics committed Jun 11, 2012
@@ -10,7 +10,7 @@ - (id)initWithObject:(id)theObject {
return self;
}
-- (id)extractObject {
+- (id)extractObject:(NSArray *)arguments {
return _instance;
}
@@ -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
@@ -4,7 +4,7 @@ @implementation JSObjectionEntry
@synthesize injector = _injector;
@dynamic lifeCycle;
-- (id)extractObject {
+- (id)extractObject:(NSArray *)arguments {
return nil;
}
@@ -1,6 +1,7 @@
#import "JSObjectionInjector.h"
#import "JSObjectionEntry.h"
#import "JSObjectFactory.h"
+#import "JSObjectionUtils.h"
#import <pthread.h>
#import <objc/runtime.h>
@@ -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;
@@ -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];
}
@@ -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)];
}
@@ -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];
}
@@ -19,7 +19,7 @@ - (id)initWithBlock:(id(^)(JSObjectionInjector *context))theBlock {
return self;
}
-- (id)extractObject {
+- (id)extractObject:(NSArray *)arguments {
if (_block) {
return _block(self.injector);
}
@@ -1,6 +1,9 @@
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
+NSString *const JSObjectionInitializerKey;
+NSString *const JSObjectionDefaultArgumentsKey;
+
typedef enum {
JSObjectionTypeClass,
JSObjectionTypeProtocol
@@ -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;
@@ -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];
@@ -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) {
@@ -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.