Skip to content

Commit

Permalink
Added nib resolver for view controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Sundin committed Sep 14, 2013
1 parent 27943e6 commit b32e214
Show file tree
Hide file tree
Showing 14 changed files with 12,808 additions and 3,383 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2013, Jasper Blues & Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

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

/**
Post-Processor that completes the initializer of definitions with type UIViewController.
If the definition already has a TyphoonIntializer set, the processor will ignore the component.
*/
@interface TyphoonViewControllerNibResolver : NSObject<TyphoonComponentFactoryPostProcessor>

/**
Resolves the nib name for a view controller class.
Defaults to the same name as the class by NSStringFromClass.
@param viewControllerClass The class.
@return The nib name.
*/
- (NSString *)resolveNibNameForClass:(Class)viewControllerClass;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2013, Jasper Blues & Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

#import "TyphoonViewControllerNibResolver.h"
#import "TyphoonDefinition.h"
#import "TyphoonComponentFactory.h"
#import "TyphoonInitializer.h"
#import "TyphoonInitializer+InstanceBuilder.h"
#import <UIKit/UIKit.h>

@implementation TyphoonViewControllerNibResolver

#pragma mark - Protocol methods

- (void)postProcessComponentFactory:(TyphoonComponentFactory *)factory
{
for (TyphoonDefinition *definition in [factory registry])
{
if ([self shouldProcessDefinition:definition])
{
[self processViewControllerDefinition:definition];
}
}
}

#pragma mark - Interface methods

- (NSString *)resolveNibNameForClass:(Class)viewControllerClass
{
return NSStringFromClass(viewControllerClass);
}

/* ====================================================================================================================================== */
#pragma mark - Private Methods

- (void)processViewControllerDefinition:(TyphoonDefinition *)definition
{
TyphoonInitializer *initializer = [[TyphoonInitializer alloc] initWithSelector:@selector(initWithNibName:bundle:)];
[initializer injectWithValueAsText:[self resolveNibNameForClass:definition.type] requiredTypeOrNil:[NSString class]];
[initializer injectWithObject:[NSBundle mainBundle]];
definition.initializer = initializer;
}

- (BOOL)shouldProcessDefinition:(TyphoonDefinition *)definition
{
return [definition.type isSubclassOfClass:[UIViewController class]] && definition.initializer == nil;
}

@end
Empty file added Source/osx/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2013, Jasper Blues & Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

#import <SenTestingKit/SenTestingKit.h>
#import <TyphoonViewControllerNibResolver.h>
#import <UIKit/UIKit.h>
#import <TyphoonDefinition.h>
#import <TyphoonDefinition+InstanceBuilder.h>
#import <TyphoonComponentFactory.h>
#import <TyphoonInitializer.h>
#import <TyphoonInitializer+InstanceBuilder.h>

@interface TyphoonViewControllerNibResolverTests : SenTestCase
{
TyphoonViewControllerNibResolver *_nibResolver;
}

@end

@implementation TyphoonViewControllerNibResolverTests

- (void)setUp {
[super setUp];
_nibResolver = [[TyphoonViewControllerNibResolver alloc] init];
}

- (void)test_process_view_controller_without_initializer
{
TyphoonDefinition *definition = [TyphoonDefinition withClass:[UIViewController class]];
TyphoonComponentFactory *factory = mock([TyphoonComponentFactory class]);
[given([factory registry]) willReturn:@[definition]];

[_nibResolver postProcessComponentFactory:factory];

assertThat(definition.initializer, notNilValue());
assertThat(NSStringFromSelector(definition.initializer.selector), equalTo(NSStringFromSelector(@selector(initWithNibName:bundle:))));
assertThatInt([[definition.initializer injectedParameters] count], equalToInt(2));

}

- (void)test_skips_view_controller_with_initializer
{
TyphoonDefinition *definition = [TyphoonDefinition withClass:[UIViewController class]];
TyphoonInitializer *initializer = [[TyphoonInitializer alloc] init];
definition.initializer = initializer;

TyphoonComponentFactory *factory = mock([TyphoonComponentFactory class]);
[given([factory registry]) willReturn:@[definition]];

[_nibResolver postProcessComponentFactory:factory];
assertThat(NSStringFromSelector(initializer.selector), equalTo(NSStringFromSelector(@selector(init))));
assertThat(definition.initializer, equalTo(initializer));
}


- (void)test_resolves_nib_name_from_class
{
Class clazz = [UIViewController class];
NSString *nibName = [_nibResolver resolveNibNameForClass:clazz];
assertThat(nibName, equalTo(NSStringFromClass(clazz)));
}

@end
1 change: 0 additions & 1 deletion Tests/Factory/TyphoonComponentFactoryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ - (void)test_post_processors_applied

}


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

Expand Down
2 changes: 1 addition & 1 deletion Tests/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
OCHamcrest: f8393efd5a49d91879be573635d6183effacc0ab
OCMockito: 01560fd24319b85c223e10f52e2e41fbe2c6d434
Typhoon: 2347ab6a5749527cbda2e81aafcede3d3f6dd35a
Typhoon: b3e24ac096f2a7233503a2b2f58aa9c7c583c7dc

COCOAPODS: 0.24.0

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Tests/Pods/Local Podspecs/Typhoon.podspec

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tests/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b32e214

Please sign in to comment.