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

Add support for factories #11

Closed
jgongo opened this issue Mar 2, 2013 · 4 comments
Closed

Add support for factories #11

jgongo opened this issue Mar 2, 2013 · 4 comments

Comments

@jgongo
Copy link
Contributor

jgongo commented Mar 2, 2013

I was trying to create a Core Data stack using Typhoon and faced the following problem: I had to create the managed object model injecting the URL of the model in the initializer. The URL of the model is typically constructed this way:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataTest" withExtension:@"momd"];

As far as I know (correct me if I'm wrong) there is no way to create / inject such an object using Typhoon. I could build the main bundle object using Typhoon, but then I have no way of invoking a method on that object to get another object that gets injected in an additional object.

I think the general problem to be solved is to add support for factories: in fact the main bundle is a URL factory, using the method shown above. So maybe we could add something like this to Typhoon:

return [TyphoonDefinition withFactory:[self mainBundle] creator:^(TyphoonCreator *creator) {
    creator.selector = @selector(URLForResource:withExtension:);
    [creator injectParameterAt:0 withValueAsText:@"CoreDataTest" requiredTypeOrNil:[NSString class]];
    [creator injectParameterAt:1 withValueAsText:@"momd" requiredTypeOrNil:[NSString class]];
}];

I've noticed I can have a workaround for this problem using type converters, creating a converter that converts the string "CoreDataTest.momd" to the required NSURL, but this would prevent the creation of a "natural" type converter from strings to NSURLs, as Typhoon only supports a converter for a given type.

What do you think? Am I missing anything?

@jasperblues
Copy link
Member

This is already supported. I'll update the documentation ASAP. Meantime take a look at:

  • (void)test_returns_component_from_factory_component
    {
    Sword* sword = [_componentFactory componentForKey:@"blueSword"];
    assertThat([sword description], equalTo(@"A bright blue sword with orange pom-poms at the hilt."));
    }

Which in the block style, is satisfied like so:

  • (id)swordFactory
    {
    return [TyphoonDefinition withClass:[SwordFactory class]];
    }
  • (id)blueSword
    {
    return [TyphoonDefinition withClass:[Sword class] initialization:^(TyphoonInitializer* initializer)
    {
    initializer.selector = @selector(swordWithSpecification:);
    [initializer injectParameterNamed:@"specification" withValueAsText:@"blue" requiredTypeOrNil:[NSString class]];
    } properties:^(TyphoonDefinition* definition)
    {
    //TODO: Fix this
    definition.factoryComponent = @"swordFactory";
    }];
    }

Ahhhhhhh. . . .. But you've just pointed out a TODO, that needs doing. You should be able to say fdefinition.factoryComponent = [self swordFactory] . . . .(currently it is the selector name in string form).

Will fix this today.

On Mar 3, 2013, at 1:47 AM, José González [email protected] wrote:

I was trying to create a Core Data stack using Typhoon and faced the following problem: I had to create the managed object model injecting the URL of the model in the initializer. The URL of the model is typically constructed this way:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataTest" withExtension:@"momd"];
As far as I know (correct me if I'm wrong) there is no way to create / inject such an object using Typhoon. I could build the main bundle object using Typhoon, but then I have no way of invoking a method on that object to get another object that gets injected in an additional object.

I think the general problem to be solved is to add support for factories: in fact the main bundle is a URL factory, using the method shown above. So maybe we could add something like this to Typhoon:

return [TyphoonDefinition withFactory:[self mainBundle] creator:^(TyphoonCreator *creator) {
creator.selector = @selector(URLForResource:withExtension:);
[creator injectParameterAt:0 withValueAsText:@"CoreDataTest" requiredTypeOrNil:[NSString class]];
[creator injectParameterAt:1 withValueAsText:@"momd" requiredTypeOrNil:[NSString class]];
}];
I've noticed I can have a workaround for this problem using type converters, creating a converter that converts the string "CoreDataTest.momd" to the required NSURL, but this would prevent the creation of a "natural" type converter from strings to NSURLs, as Typhoon only supports a converter for a given type.

What do you think? Am I missing anything?


Reply to this email directly or view it on GitHub.

@jasperblues
Copy link
Member

As of 1.1.2, you can now wire the factory with:

definition.factory = [self swordFactory];

@jasperblues
Copy link
Member

Updated the docs, but they need some more attention.

@jgongo
Copy link
Contributor Author

jgongo commented Mar 3, 2013

Great!!! Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants