Skip to content

Commit

Permalink
GH-96 Add spec for corelated mapping for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Zen committed May 19, 2017
1 parent acb2a11 commit da552c9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
5 changes: 1 addition & 4 deletions FastEasyMapping/Source/Cache/FEMObjectCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ typedef _Nonnull id<NSFastEnumeration> (^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;
Expand Down
11 changes: 9 additions & 2 deletions FastEasyMapping/Source/Cache/FEMObjectCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#import "FEMMapping.h"
#import "FEMRepresentationUtility.h"

FEMObjectCacheSource FEMObjectCacheSourceStub = ^id<NSFastEnumeration> (FEMMapping *mapping) {
return @[];
};

@implementation FEMObjectCache {
NSMapTable<FEMMapping *, NSMutableDictionary<id, id> *> *_lookupObjectsMap;
FEMObjectCacheSource _source;
Expand All @@ -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];
Expand All @@ -27,6 +30,10 @@ - (instancetype)initWithSource:(FEMObjectCacheSource)source {
return self;
}

- (instancetype)init {
return [self initWithSource:nil];
}

#pragma mark - Inspection

- (NSMutableDictionary *)fetchExistingObjectsForMapping:(FEMMapping *)mapping {
Expand Down
30 changes: 30 additions & 0 deletions FastEasyMappingTests/Specs/FEMCacheSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit da552c9

Please sign in to comment.