Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Basic support for initializers has been added.

  • Loading branch information...
commit 8e04ce769610b6511d46f0e592ee2194515def9d 1 parent d2d2876
@dewind dewind authored
View
24 Source/JSObjectionInjectorEntry.m
@@ -4,7 +4,9 @@
@interface JSObjectionInjectorEntry()
- (void)notifyObjectThatItIsReady: (id)object;
-- (id)buildObject;
+- (id)buildObject:(NSArray *)arguments;
+- (id)argumentsForObject:(NSArray *)givenArguments;
+- (SEL)initializerForObject;
@end
@@ -28,7 +30,7 @@ - (id)initWithClass:(Class)theClass lifeCycle:(JSObjectionInstantiationRule)theL
- (id)extractObject:(NSArray *)arguments {
if (self.lifeCycle == JSObjectionInstantiationRuleNormal || !_storageCache) {
- return [self buildObject];
+ return [self buildObject:arguments];
}
return _storageCache;
@@ -50,8 +52,14 @@ - (void)notifyObjectThatItIsReady:(id)object {
}
}
-- (id)buildObject {
- id objectUnderConstruction = [[[self.classEntry alloc] init] autorelease];
+- (id)buildObject:(NSArray *)arguments {
+
+ id objectUnderConstruction = nil;
+ if ([self.classEntry respondsToSelector:@selector(objectionInitializer)]) {
+ objectUnderConstruction = JSObjectionUtils.buildObjectWithInitializer(self.classEntry, [self initializerForObject], [self argumentsForObject:arguments]);
+ } else {
+ objectUnderConstruction = [[[self.classEntry alloc] init] autorelease];
+ }
if (self.lifeCycle == JSObjectionInstantiationRuleSingleton) {
_storageCache = [objectUnderConstruction retain];
@@ -93,6 +101,14 @@ - (id)buildObject {
return objectUnderConstruction;
}
+- (SEL)initializerForObject {
+ return NSSelectorFromString([[self.classEntry performSelector:@selector(objectionInitializer)] objectForKey:JSObjectionInitializerKey]);
+}
+
+- (NSArray *)argumentsForObject:(NSArray *)givenArguments {
+ return givenArguments.count > 0 ? givenArguments : [[self.classEntry performSelector:@selector(objectionInitializer)] objectForKey:JSObjectionDefaultArgumentsKey];
+}
+
#pragma mark Class Methods
#pragma mark -
View
1  Source/JSObjectionUtils.h
@@ -20,4 +20,5 @@ extern const struct JSObjectionUtils {
NSSet* (*buildDependenciesForClass)(Class klass, NSSet *requirements);
NSDictionary* (*buildInitializer)(SEL selector, NSArray *arguments);
NSArray* (*transformVariadicArgsToArray)(va_list va_arguments);
+ id (*buildObjectWithInitializer)(Class klass, SEL initializer, NSArray *arguments);
} JSObjectionUtils;
View
19 Source/JSObjectionUtils.m
@@ -81,10 +81,27 @@ static objc_property_t GetProperty(Class klass, NSString *propertyName) {
return property;
}
+
+static id BuildObjectWithInitializer(Class klass, SEL initializer, NSArray *arguments) {
+ id instance = [klass alloc];
+ NSMethodSignature *signature = [klass instanceMethodSignatureForSelector:initializer];
+ NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
+ [invocation setTarget:instance];
+ [invocation setSelector:initializer];
+ for (int i = 0; i < arguments.count; i++) {
+ id argument = [arguments objectAtIndex:i];
+ [invocation setArgument:&argument atIndex:i + 2];
+ }
+ [invocation invoke];
+ [invocation getReturnValue:&instance];
+ return [instance autorelease];
+}
+
const struct JSObjectionUtils JSObjectionUtils = {
.findClassOrProtocolForProperty = FindClassOrProtocolForProperty,
.propertyForClass = GetProperty,
.buildDependenciesForClass = BuildDependenciesForClass,
.buildInitializer = BuildInitializer,
- .transformVariadicArgsToArray = TransformVariadicArgsToArray
+ .transformVariadicArgsToArray = TransformVariadicArgsToArray,
+ .buildObjectWithInitializer = BuildObjectWithInitializer
};
View
2  Specs/InitializerSpecs.m
@@ -19,7 +19,7 @@
it(@"will override the default arguments if arguments are passed to the injector", ^{
ViewController *controller = [injector getObjectWithArgs:[ViewController class], @"AnotherNib", @"pretendBundle", nil];
- [[controller.nibName should] equal:@"MyNib"];
+ [[controller.nibName should] equal:@"AnotherNib"];
[[controller.bundle should] equal:@"pretendBundle"];
[[controller.car should] beMemberOfClass:[Car class]];
});

0 comments on commit 8e04ce7

Please sign in to comment.
Something went wrong with that request. Please try again.