Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Allow NSManagedObject formatter to be changed #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Classes/NSManagedObject+ActiveRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@

+ (NSString *)entityName;

#pragma mark - Date Formatting

+ (void)setDateFormat:(NSString *)dateFormat;

@end
24 changes: 17 additions & 7 deletions Classes/NSManagedObject+ActiveRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ - (void)setSafeValue:(id)value forKey:(id)key {
value = [NSNumber numberWithDouble:[value doubleValue]];

else if (attributeType == NSDateAttributeType)
value = [self.defaultFormatter dateFromString:value];
value = [self.dateFormatter dateFromString:value];
}

[self setValue:value forKey:key];
Expand All @@ -234,15 +234,25 @@ - (BOOL)isIntegerAttributeType:(NSAttributeType)attributeType {

#pragma mark - Date Formatting

- (NSDateFormatter *)defaultFormatter {
static NSDateFormatter *sharedFormatter;
static dispatch_once_t singletonToken;
static NSDateFormatter *sharedFormatter;
static dispatch_once_t singletonToken;

+(NSDateFormatter *)sharedFormatter{
dispatch_once(&singletonToken, ^{
sharedFormatter = [[NSDateFormatter alloc] init];
[sharedFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
if(!sharedFormatter){
sharedFormatter = [[NSDateFormatter alloc] init];
[sharedFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
}
});

return sharedFormatter;
}

- (NSDateFormatter *)dateFormatter {
return [NSManagedObject sharedFormatter];
}

+ (void)setDateFormat:(NSString *)dateFormat {
[[NSManagedObject sharedFormatter] setDateFormat:dateFormat];
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about name convenience?

- (void)setDateFormat:(NSString *)dateFormat {
    [sharedFormatter setDateFormat:dateFormat];
}

@end
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Kiwi (2.1)
- ObjectiveRecord (1.3.1):
- ObjectiveRecord (1.4.0):
- ObjectiveSugar
- ObjectiveSugar (0.8)

Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Kiwi: 054ef97d0e50c279b21113a0880a906fa153a7fd
ObjectiveRecord: 3a196d1c853a315919951905babdb073df915cd6
ObjectiveRecord: 109a6c583284c2c9af1dc0d6ccc1dfc84e826c45
ObjectiveSugar: 101df0252a4a9b7eab2e20bf1cd444bfaec2a02b

COCOAPODS: 0.26.2
12 changes: 12 additions & 0 deletions Example/SampleProjectTests/FindersAndCreatorsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ void createSomePeople(NSArray *names, NSArray *surnames, NSManagedObjectContext
Person *person = [Person create:@{ @"anniversary": [formatta stringFromDate:date] }];
[[@([date timeIntervalSinceDate:person.anniversary]) should] beLessThan:@1];
});

it(@"converts string from specified date format", ^{
[NSManagedObject setFormatter:@"HH:mm:ss"];
Person *person = [Person create:@{ @"anniversary": @"12:24:05" }];
[person.anniversary shouldNotBeNil];
});

it(@"can't convert string from specified date format", ^{
[NSManagedObject setFormatter:@"yyyy HH:mm:ss"];
Person *person = [Person create:@{ @"anniversary": @"12:24:05" }];
[person.anniversary shouldBeNil];
});

it(@"doesn't update with nulls", ^{
Person *person = fetchUniquePerson();
Expand Down