Skip to content

Commit

Permalink
Add index option for initializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Blues committed Apr 15, 2013
1 parent aaeb9f3 commit 3fb2637
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Source/Component/Initializer/TyphoonInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typedef enum

- (void)injectParameterNamed:(NSString*)name withValueAsText:(NSString*)text requiredTypeOrNil:(id)classOrProtocol;

- (void)injectParameterAt:(NSUInteger)index withValueAsText:(NSString*)text requiredTypeOrNil:(id)requiredClass;
- (void)injectParameterAtIndex:(NSUInteger)index withValueAsText:(NSString*)text requiredTypeOrNil:(id)requiredClass;


/* ====================================================================================================================================== */
Expand Down
6 changes: 3 additions & 3 deletions Source/Component/Initializer/TyphoonInitializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ - (void)injectParameterAtIndex:(NSUInteger)index withReference:(NSString*)refere

- (void)injectParameterNamed:(NSString*)name withValueAsText:(NSString*)text requiredTypeOrNil:(id)classOrProtocol
{
[self injectParameterAt:[self indexOfParameter:name] withValueAsText:text requiredTypeOrNil:classOrProtocol];
[self injectParameterAtIndex:[self indexOfParameter:name] withValueAsText:text requiredTypeOrNil:classOrProtocol];
}

- (void)injectParameterAt:(NSUInteger)index withValueAsText:(NSString*)text requiredTypeOrNil:(id)requiredClass
- (void)injectParameterAtIndex:(NSUInteger)index withValueAsText:(NSString*)text requiredTypeOrNil:(id)requiredClass
{
if (index != NSUIntegerMax && index < [_parameterNames count])
{
Expand Down Expand Up @@ -97,7 +97,7 @@ - (void)injectWithText:(NSString*)text

- (void)injectWithText:(NSString*)text requiredTypeOrNil:(id)requiredTypeOrNil
{
[self injectParameterAt:[_injectedParameters count] withValueAsText:text requiredTypeOrNil:requiredTypeOrNil];
[self injectParameterAtIndex:[_injectedParameters count] withValueAsText:text requiredTypeOrNil:requiredTypeOrNil];
}

/* ====================================================================================================================================== */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ - (id)buildCollectionFor:(TyphoonPropertyInjectedAsCollection*)propertyInjectedA
[collection addObject:converted];
}
}
return propertyInjectedAsCollection.injectionType == TyphoonCollectionTypeNSMutableArray || TyphoonCollectionTypeNSMutableSet ?
[collection copy] : collection;
BOOL isMutable = propertyInjectedAsCollection.injectionType == TyphoonCollectionTypeNSMutableArray ||
propertyInjectedAsCollection.injectionType == TyphoonCollectionTypeNSMutableSet;
return propertyInjectedAsCollection.injectionType == isMutable ? [collection copy] : collection;
}

- (id)collectionFor:(TyphoonPropertyInjectedAsCollection*)propertyInjectedAsCollection
Expand Down
27 changes: 25 additions & 2 deletions Source/Factory/Xml/TyphoonRXMLElement+XmlComponentFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,27 @@ - (void)parseComponentDefinitionChildren:(TyphoonDefinition*)componentDefinition
- (void)setArgumentOnInitializer:(TyphoonInitializer*)initializer withChildTag:(TyphoonRXMLElement*)child
{
NSString* name = [child attribute:@"parameterName"];
NSString* index = [child attribute:@"index"];

if (name && index)
{
[NSException raise:NSInvalidArgumentException format:@"'parameterName' and 'index' cannot be used together"];
}

NSString* reference = [child attribute:@"ref"];
NSString* value = [child attribute:@"value"];

if (reference)
{
[initializer injectParameterNamed:name withReference:reference];
if (name)
{
[initializer injectParameterNamed:name withReference:reference];
}
else if (index)
{
[initializer injectParameterAtIndex:[index integerValue] withReference:reference];
}

}
else if (value)
{
Expand All @@ -204,7 +219,15 @@ - (void)setArgumentOnInitializer:(TyphoonInitializer*)initializer withChildTag:(
[NSException raise:NSInvalidArgumentException format:@"Class '%@' could not be resolved.", classAsString];
}
}
[initializer injectParameterNamed:name withValueAsText:value requiredTypeOrNil:clazz];
if (name)
{
[initializer injectParameterNamed:name withValueAsText:value requiredTypeOrNil:clazz];
}
else if (index)
{
[initializer injectParameterAtIndex:[index integerValue] withValueAsText:value requiredTypeOrNil:clazz];
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (void)test_mutates_initializer_values
TyphoonDefinition* knightDefinition = [[TyphoonDefinition alloc] initWithClass:[Knight class] key:@"knight"];
knightDefinition.initializer = [[TyphoonInitializer alloc] init];
knightDefinition.initializer.selector = @selector(initWithQuest:damselsRescued:);
[knightDefinition.initializer injectParameterAt:1 withValueAsText:@"${damsels.rescued}" requiredTypeOrNil:nil];
[knightDefinition.initializer injectParameterAtIndex:1 withValueAsText:@"${damsels.rescued}" requiredTypeOrNil:nil];
[factory register:knightDefinition];

[_configurer mutateComponentDefinitionsIfRequired:[factory registry]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (void)test_injects_required_initializer_dependencies_with_factory_method
{
TyphoonDefinition* urlDefinition = [[TyphoonDefinition alloc] initWithClass:[NSURL class] key:@"url"];
TyphoonInitializer* initializer = [[TyphoonInitializer alloc] initWithSelector:@selector(URLWithString:) isClassMethod:YES];
[initializer injectParameterAt:0 withValueAsText:@"http://www.appsquick.ly" requiredTypeOrNil:[NSString class]];
[initializer injectParameterAtIndex:0 withValueAsText:@"http://www.appsquick.ly" requiredTypeOrNil:[NSString class]];
[urlDefinition setInitializer:initializer];
[_componentFactory register:urlDefinition];

Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources/MiddleAgesAssembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</description>
<initializer selector="initWithQuest:hitRatio:">
<argument parameterName="quest" ref="quest"/>
<argument parameterName="hitRatio" value="13.75"/>
<argument index="1" value="13.75"/>
</initializer>
<property name="hasHorseWillTravel" value="yes">
<description>
Expand Down
9 changes: 8 additions & 1 deletion schema/assembly.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,20 @@
<xsd:any namespace="##other" processContents="strict"/>
</xsd:choice>
</xsd:sequence>
<xsd:attribute name="parameterName" type="xsd:string" use="required">
<xsd:attribute name="parameterName" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The name of the argument, following Objective-C naming conventions.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="index" type="xsd:integer" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[
The index of the argument.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="ref" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
Expand Down

0 comments on commit 3fb2637

Please sign in to comment.