Skip to content

Commit

Permalink
#439 Add NSURL support to the CBLModel property
Browse files Browse the repository at this point in the history
Implement NSURL support to the CBLModel property and update unit tests accordingly.
  • Loading branch information
pasin committed Sep 9, 2014
1 parent f78152f commit 8502443
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Source/API/CBLModel+Properties.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ static ValueConverter valueConverterToClass(Class toClass) {
return [NSDecimalNumber decimalNumberWithString: rawValue];
return nil;
};
} else if (toClass == [NSURL class]) {
return ^id(id rawValue, CBLModel* self, NSString* property) {
if ([rawValue isKindOfClass: [NSString class]])
return [NSURL URLWithString: rawValue];
return nil;
};
} else if ([toClass conformsToProtocol: @protocol(CBLJSONEncoding)]) {
return ^id(id rawValue, CBLModel* self, NSString* property) {
if (!rawValue)
Expand Down
2 changes: 2 additions & 0 deletions Source/API/CBLModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ - (id) externalizePropertyValue: (id)value {
value = [CBLJSON JSONObjectWithDate: value];
else if ([value isKindOfClass: [NSDecimalNumber class]])
value = [value stringValue];
else if ([value isKindOfClass: [NSURL class]])
value = [value absoluteString];
else if ([value isKindOfClass: [CBLModel class]])
value = ((CBLModel*)value).document.documentID;
else if ([value isKindOfClass: [NSArray class]]) {
Expand Down
15 changes: 13 additions & 2 deletions Source/API/ModelTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ @interface CBL_TestModel : CBLModel
@property NSData* data;
@property NSDate* date;
@property NSDecimalNumber* decimal;
@property NSURL* url;
@property CBL_TestModel* other;
@property NSArray* strings;
@property NSArray* dates;
Expand All @@ -130,7 +131,7 @@ @implementation CBL_TestModel

@dynamic number, uInt, sInt16, uInt16, sInt8, uInt8, nsInt, nsUInt, sInt32, uInt32;
@dynamic sInt64, uInt64, boolean, boolObjC, floaty, doubly, dict;
@dynamic str, data, date, decimal, other, strings, dates, others, Capitalized;
@dynamic str, data, date, decimal, url, other, strings, dates, others, Capitalized;
@dynamic subModel, subModels, mutableSubModel;
@synthesize reloadCount;

Expand Down Expand Up @@ -219,6 +220,10 @@ + (Class) subModelsItemClass {
CAssertEqual(model.strings, strings);
CAssertEqual(model.data, data);

NSURL* url = [NSURL URLWithString: @"http://bogus"];
model.url = url;
CAssertEqual(model.url, url);

Log(@"Model: %@", [CBLJSON stringWithJSONObject: model.propertiesToSave options: 0 error: NULL]);
[db _close];
}
Expand Down Expand Up @@ -334,6 +339,7 @@ + (Class) subModelsItemClass {
NSDate* date = [NSDate dateWithTimeIntervalSinceReferenceDate: 392773252];
NSArray* dates = @[date, [NSDate dateWithTimeIntervalSinceReferenceDate: 392837521]];
NSDecimalNumber* decimal = [NSDecimalNumber decimalNumberWithString: @"12345.6789"];
NSURL* url = [NSURL URLWithString: @"http://bogus"];

CBLDatabase* db = createEmptyDB();
NSString* modelID, *model2ID, *model3ID;
Expand All @@ -353,6 +359,7 @@ + (Class) subModelsItemClass {
model.date = date;
model.dates = dates;
model.decimal = decimal;
model.url = url;

CAssert(model.isNew);
CAssert(model.needsSave);
Expand All @@ -364,7 +371,8 @@ + (Class) subModelsItemClass {
@"date": @"2013-06-12T23:40:52.000Z",
@"dates": @[@"2013-06-12T23:40:52.000Z",
@"2013-06-13T17:32:01.000Z"],
@"decimal": @"12345.6789"}));
@"decimal": @"12345.6789",
@"url": @"http://bogus"}));

CBL_TestModel* model2 = [[CBL_TestModel alloc] initWithNewDocumentInDatabase: db];
model2ID = model2.document.documentID;
Expand All @@ -382,6 +390,7 @@ + (Class) subModelsItemClass {
CAssertEqual(model.date, [NSDate dateWithTimeIntervalSinceReferenceDate: 392773252]);
CAssertEqual(model.dates, dates);
CAssertEqual(model.decimal, decimal);
CAssertEqual(model.url, url);
CAssertEq(model.other, model3);
CAssertEqual(model.others, (@[model2, model3]));

Expand All @@ -400,6 +409,7 @@ + (Class) subModelsItemClass {
@"date": @"2013-06-12T23:40:52.000Z",
@"dates": @[@"2013-06-12T23:40:52.000Z", @"2013-06-13T17:32:01.000Z"],
@"decimal": @"12345.6789",
@"url": @"http://bogus",
@"other": model3.document.documentID,
@"others": @[model2.document.documentID, model3.document.documentID],
@"_id": props[@"_id"],
Expand Down Expand Up @@ -443,6 +453,7 @@ + (Class) subModelsItemClass {
CAssertEqual(modelAgain.date, [NSDate dateWithTimeIntervalSinceReferenceDate: 392773252]);
CAssertEqual(modelAgain.dates, dates);
CAssertEqual(modelAgain.decimal, decimal);
CAssertEqual(modelAgain.url, url);

CBL_TestModel *other = modelAgain.other;
CAssertEqual(modelAgain.other.document.documentID, model3ID);
Expand Down

0 comments on commit 8502443

Please sign in to comment.