Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (112 sloc) 3.253 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#import "ModuleFixtures.h"

BOOL gEagerSingletonHook = NO;

@implementation Car(Meta)

+ (id)manufacture {
  return [[[Car alloc] init] autorelease];
}

@end


@implementation AfterMarketGearBox
- (void)shiftUp {
  
}

- (void)shiftDown {
  
}
@end

@implementation EagerSingleton
objection_register_singleton(EagerSingleton)
- (void)awakeFromObjection {
  gEagerSingletonHook = YES;
}
@end


@implementation MyModule
@synthesize engine=_engine;
@synthesize gearBox=_gearBox;
@synthesize instrumentInvalidEagerSingleton=_instrumentInvalidEagerSingleton;
@synthesize instrumentInvalidMetaClass = _instrumentInvalidMetaClass;

- (id)initWithEngine:(Engine *)engine andGearBox:(id<GearBox>)gearBox {
  if ((self = [super init])) {
    _engine = [engine retain];
    _gearBox = [gearBox retain];
  }
  
  return self;
}

- (void)configure {
  [self bind:_engine toClass:[Engine class]];
  [self bind:_gearBox toProtocol:@protocol(GearBox)];
  [self bindClass:[VisaCCProcessor class] toProtocol:@protocol(CreditCardProcessor)];
  if (self.instrumentInvalidMetaClass) {
    [self bindMetaClass:(id)@"sneaky" toProtocol:@protocol(MetaCar)];
  } else {
    [self bindMetaClass:[Car class] toProtocol:@protocol(MetaCar)];
  }
  
  if (self.instrumentInvalidEagerSingleton) {
    [self registerEagerSingleton:[Car class]];
  } else {
    [self registerEagerSingleton:[EagerSingleton class]];
  }
  
}

- (void)dealloc {
  [_engine release];_engine = nil;
  [_gearBox release];_gearBox = nil;
  [super dealloc];
}

@end

@implementation CarProvider
- (id)createInstance:(JSObjectionInjector *)context
{
  Car *car = [context getObject:[ManualCar class]];
  car.engine = (id)@"my engine";
  return car;
}
@end

@implementation GearBoxProvider
- (id)createInstance:(JSObjectionInjector *)context
{
  return [[[AfterMarketGearBox alloc] init] autorelease];
}
@end


@implementation ProviderModule
- (void)configure
{
  [self bindProvider:[[[CarProvider alloc] init] autorelease] toClass:[Car class]];
  [self bindProvider:[[[GearBoxProvider alloc] init] autorelease] toProtocol:@protocol(GearBox)];
}
@end

@implementation BlockModule

- (void)configure
{
  NSString *myEngine = [NSString stringWithString:@"My Engine"];
  
  [self bindBlock:^(JSObjectionInjector *context) {
    Car *car = [context getObject:[ManualCar class]];
    car.engine = (id)myEngine;
    return (id)car;
  } toClass:[Car class]];
  
  AfterMarketGearBox *gearBox = [[[AfterMarketGearBox alloc] init] autorelease];
  [self bindBlock:^(JSObjectionInjector *context) {
    return (id)gearBox;
  } toProtocol:@protocol(GearBox)];
}

@end

@implementation CreditCardValidator
@end

@implementation VisaCCProcessor
objection_register_singleton(VisaCCProcessor)
objection_requires(@"validator")

@synthesize validator = _validator;

- (void)processNumber:(NSString *)number {
  
}

- (void)dealloc {
  [_validator release];
  [super dealloc];
}
@end

@implementation FirstModule
- (void)configure {
  [self bind:[[[ManualCar alloc] init] autorelease] toClass:[Car class]];
  [self registerEagerSingleton:[EagerSingleton class]];
}
@end

@implementation SecondModule
- (void)configure {
  [self bind:[[[AfterMarketGearBox alloc] init] autorelease] toProtocol:@protocol(GearBox)];
}
@end
Something went wrong with that request. Please try again.