Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'injectProperties' to allow injection of externally created objects. #15

Merged
merged 2 commits into from
May 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

#import <Foundation/Foundation.h>
#import "TyphoonComponentFactory.h"

#import "TyphoonIntrospectiveNSObject.h"
@interface TyphoonComponentFactory (InstanceBuilder)

- (id)buildInstanceWithDefinition:(TyphoonDefinition*)definition;

- (void)injectPropertyDependenciesOn:(id <TyphoonIntrospectiveNSObject>)instance withDefinition:(TyphoonDefinition*)definition;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ - (id)buildInstanceWithDefinition:(TyphoonDefinition*)definition
return instance;
}

- (void)injectPropertyDependenciesOn:(id <TyphoonIntrospectiveNSObject>)instance withDefinition:(TyphoonDefinition*)definition
{
[self doBeforePropertyInjectionOn:instance withDefinition:definition];

for (id <TyphoonInjectedProperty> property in [definition injectedProperties])
{
TyphoonTypeDescriptor* typeDescriptor = [instance typeForPropertyWithName:property.name];
if (typeDescriptor == nil)
{
[NSException raise:NSInvalidArgumentException
format:@"Tried to inject property '%@' on object of type '%@', but the instance has no setter for this property.",
property.name, [instance class]];
}
[self doPropertyInjection:instance property:property typeDescriptor:typeDescriptor];
}

[self doAfterPropertyInjectionOn:instance withDefinition:definition];
}


/* ============================================================ Private Methods ========================================================= */
- (id)invokeInitializerOn:(id)instanceOrClass withDefinition:(TyphoonDefinition*)definition
Expand Down Expand Up @@ -100,25 +119,6 @@ - (id)invokeInitializerOn:(id)instanceOrClass withDefinition:(TyphoonDefinition*
/* ====================================================================================================================================== */
#pragma mark - Property Injection

- (void)injectPropertyDependenciesOn:(id <TyphoonIntrospectiveNSObject>)instance withDefinition:(TyphoonDefinition*)definition
{
[self doBeforePropertyInjectionOn:instance withDefinition:definition];

for (id <TyphoonInjectedProperty> property in [definition injectedProperties])
{
TyphoonTypeDescriptor* typeDescriptor = [instance typeForPropertyWithName:property.name];
if (typeDescriptor == nil)
{
[NSException raise:NSInvalidArgumentException
format:@"Tried to inject property '%@' on object of type '%@', but the instance has no setter for this property.",
property.name, [instance class]];
}
[self doPropertyInjection:instance property:property typeDescriptor:typeDescriptor];
}

[self doAfterPropertyInjectionOn:instance withDefinition:definition];
}

- (void)doBeforePropertyInjectionOn:(id <TyphoonIntrospectiveNSObject>)instance withDefinition:(TyphoonDefinition*)definition
{
if ([instance conformsToProtocol:@protocol(TyphoonPropertyInjectionDelegate)])
Expand Down
5 changes: 5 additions & 0 deletions Source/Factory/TyphoonComponentFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@

- (void)attachMutator:(id<TyphoonComponentFactoryMutator>)mutator;

/**
* Injects the properties of an object
*/
- (void)injectProperties:(id)instance;

@end
12 changes: 12 additions & 0 deletions Source/Factory/TyphoonComponentFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ - (void)attachMutator:(id)mutator
[_mutators addObject:mutator];
}

- (void)injectProperties:(id)instance {
Class class = [instance class];
for (TyphoonDefinition* definition in _registry)
{
if(definition.type == class || [definition.type isSubclassOfClass:class])
{
[self injectPropertyDependenciesOn:instance withDefinition:definition];
}
}
}

/* ============================================================ Utility Methods ========================================================= */
- (NSString*)description
Expand Down Expand Up @@ -239,4 +249,6 @@ - (void)performMutationsIfRequired
}
}



@end
18 changes: 18 additions & 0 deletions Tests/Factory/TyphoonComponentFactoryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,22 @@ - (void)test_able_to_describe_itself
assertThat(description, equalTo(@"<TyphoonComponentFactory: _registry=(\n)>"));
}

/* ====================================================================================================================================== */
#pragma mark - Inject properties

- (void)test_injectProperties
{
[_componentFactory register:[TyphoonDefinition withClass:[Knight class] properties:^(TyphoonDefinition* definition)
{
[definition injectProperty:@selector(quest)];
}]];
[_componentFactory register:[TyphoonDefinition withClass:[CampaignQuest class] key:@"quest"]];

Knight* knight = [[Knight alloc] init];
[_componentFactory injectProperties:knight];

assertThat(knight.quest, notNilValue());

}

@end