diff --git a/FastEasyMapping/Source/Cache/FEMObjectCache.h b/FastEasyMapping/Source/Cache/FEMObjectCache.h index d7e0868..7967557 100644 --- a/FastEasyMapping/Source/Cache/FEMObjectCache.h +++ b/FastEasyMapping/Source/Cache/FEMObjectCache.h @@ -10,10 +10,7 @@ typedef _Nonnull id (^FEMObjectCacheSource)(FEMMapping *mappi @interface FEMObjectCache : NSObject -- (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; - -- (instancetype)initWithSource:(FEMObjectCacheSource)source NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithSource:(nullable FEMObjectCacheSource)source NS_DESIGNATED_INITIALIZER; - (nullable id)objectForKey:(id)key mapping:(FEMMapping *)mapping; - (void)setObject:(id)object forKey:(id)key mapping:(FEMMapping *)mapping; diff --git a/FastEasyMapping/Source/Cache/FEMObjectCache.m b/FastEasyMapping/Source/Cache/FEMObjectCache.m index 50c1d59..7b7eb92 100644 --- a/FastEasyMapping/Source/Cache/FEMObjectCache.m +++ b/FastEasyMapping/Source/Cache/FEMObjectCache.m @@ -7,6 +7,10 @@ #import "FEMMapping.h" #import "FEMRepresentationUtility.h" +FEMObjectCacheSource FEMObjectCacheSourceStub = ^id (FEMMapping *mapping) { + return @[]; +}; + @implementation FEMObjectCache { NSMapTable *> *_lookupObjectsMap; FEMObjectCacheSource _source; @@ -15,10 +19,9 @@ @implementation FEMObjectCache { #pragma mark - Init - (instancetype)initWithSource:(FEMObjectCacheSource)source { - NSParameterAssert(source != NULL); self = [super init]; if (self) { - _source = [source copy]; + _source = source ?: FEMObjectCacheSourceStub; NSPointerFunctionsOptions options = NSPointerFunctionsObjectPointerPersonality | NSPointerFunctionsStrongMemory; _lookupObjectsMap = [[NSMapTable alloc] initWithKeyOptions:options valueOptions:options capacity:0]; @@ -27,6 +30,10 @@ - (instancetype)initWithSource:(FEMObjectCacheSource)source { return self; } +- (instancetype)init { + return [self initWithSource:nil]; +} + #pragma mark - Inspection - (NSMutableDictionary *)fetchExistingObjectsForMapping:(FEMMapping *)mapping { diff --git a/FastEasyMappingTests/Specs/FEMCacheSpec.m b/FastEasyMappingTests/Specs/FEMCacheSpec.m index bd6fd6d..fa4fd06 100644 --- a/FastEasyMappingTests/Specs/FEMCacheSpec.m +++ b/FastEasyMappingTests/Specs/FEMCacheSpec.m @@ -132,4 +132,34 @@ }); }); + describe(@"indirect relationship", ^{ + __block FEMMapping *mappingA; + __block FEMMapping *mappingB; + __block FEMObjectCache *cache; + + beforeEach(^{ + mappingA = [[FEMMapping alloc] initWithObjectClass:[NSObject class]]; + mappingA.primaryKey = @"a"; + + mappingB = [[FEMMapping alloc] initWithObjectClass:[NSObject class]]; + mappingB.primaryKey = @"b"; + + cache = [[FEMObjectCache alloc] init]; + }); + + it(@"should combine objects from similar mappings", ^{ + NSObject *a = [NSObject new]; + NSObject *b = [NSObject new]; + + [cache setObject:a forKey:@1 mapping:mappingA]; + [cache setObject:b forKey:@2 mapping:mappingB]; + + [[[cache objectForKey:@1 mapping:mappingA] should] equal:a]; + [[[cache objectForKey:@1 mapping:mappingB] should] equal:a]; + + [[[cache objectForKey:@2 mapping:mappingA] should] equal:b]; + [[[cache objectForKey:@2 mapping:mappingB] should] equal:b]; + }); + }); + SPEC_END