Last active
November 17, 2015 08:25
-
-
Save onmyway133/c43e03583ab507f73de1 to your computer and use it in GitHub Desktop.
BaseModel Model -> SQLite statement using property inspection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <Mantle.h> | |
#import <FTGPropertyMaestro.h> | |
@interface BaseModel : MTLModel <MTLJSONSerializing> | |
@property (nonatomic) int64_t modelID; | |
@property (nonatomic) NSTimeInterval CreatedUtc; | |
@property (nonatomic) NSTimeInterval ModifiedUtc; | |
@property (nonatomic) BOOL IsDeleted; | |
+ (NSString *)className; | |
+ (NSArray<FTGProperty *> *)properties; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "BaseModel.h" | |
#import <ObjectiveSugar.h> | |
#import "DateFormatterManager.h" | |
@implementation BaseModel | |
+ (NSDictionary *)JSONKeyPathsByPropertyKey { | |
return @{@"modelID": @"Id", | |
@"CreatedUtc": @"CreatedUtc", | |
@"ModifiedUtc": @"ModifiedUtc", | |
@"IsDeleted": @"IsDeleted", | |
}; | |
} | |
+ (NSValueTransformer *)CreatedUtcJSONTransformer { | |
return [MTLValueTransformer | |
transformerUsingForwardBlock:^id(NSString *string, BOOL *success, NSError *__autoreleasing *error) { | |
return @([[DateFormatterManager sharedManager] timestampFromString:string]); | |
}]; | |
} | |
+ (NSValueTransformer *)ModifiedUtcJSONTransformer { | |
return [MTLValueTransformer | |
transformerUsingForwardBlock:^id(NSString *string, BOOL *success, NSError *__autoreleasing *error) { | |
return @([[DateFormatterManager sharedManager] timestampFromString:string]); | |
}]; | |
} | |
+ (NSValueTransformer *)IsDeletedJSONTransformer { | |
return [NSValueTransformer valueTransformerForName:MTLBooleanValueTransformerName]; | |
} | |
+ (NSString *)className { | |
return NSStringFromClass(self); | |
} | |
#pragma mark - Properties | |
+ (NSArray<FTGProperty *> *)properties { | |
NSMutableArray *properties = [NSMutableArray array]; | |
// XPBaseModel is the superclass, it has id, createdUtc, modifiedUtc | |
[properties addObjectsFromArray:[FTGPropertyMaestro propertiesForClass:[self superclass]]]; | |
[properties addObjectsFromArray:[FTGPropertyMaestro propertiesForClass:self]]; | |
// Only interested in our declared properties | |
NSArray *keys = [self JSONKeyPathsByPropertyKey].allKeys; | |
return [properties select:^BOOL(FTGProperty *property) { | |
return [keys containsObject:property.name]; | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment