- ADD PNObjectConfig Class

- ADD PNObjectSubclassing Protocol
This commit is contained in:
giuseppenuc committed 2016-01-08 20:21:22 +01:00
1 parent 568ad5c409
commit c3e77e7a09
22 files changed
+334 -109

No files matched your search

+4 -1
View File
@@ -7,8 +7,10 @@
//
#import <Foundation/Foundation.h>
#import "PNObjectSubclassing.h"
#import "PNObjectConfig.h"
@interface PNObject : NSObject
@interface PNObject : NSObject <PNObjectSubclassing>
@property (nonatomic, strong) NSString *objID;
@property (nonatomic, strong) NSDate *createdDate;
@@ -17,4 +19,5 @@
- (instancetype) initWithJSON:(NSDictionary*) JSON;
@end
+16 -82
View File
@@ -9,6 +9,7 @@
#import "PNObject.h"
#import <PNObjectProperty.h>
#import <NSDate_Utils/NSDate+NSDate_Util.h>
#import <AFNetworking/AFNetworking.h>
@interface PNObject()
@@ -24,8 +25,8 @@
self = [super init];
if (self) {
NSAssert(_objectMapping, @"You must create objectMapping");
}
return self;
}
@@ -33,6 +34,7 @@
- (instancetype) initWithJSON:(NSDictionary*) JSON {
self = [self init];
if (self) {
NSAssert(_objectMapping, @"You must create objectMapping");
_JSON = [[NSDictionary alloc] initWithDictionary:JSON];
[self populateObjectFromJSON:JSON];
@@ -247,88 +249,20 @@
NSLog(@"%@",errorStr);
}
})();
/*// char
if([propertyType isEqualToString:@"c"]) {
char val = [value charValue];
[self setValue:@(val) forKey:propertyName];
}
// double
else if([propertyType isEqualToString:@"d"]) {
double val = [value doubleValue];
[self setValue:@(val) forKey:propertyName];
}
// float
else if([propertyType isEqualToString:@"f"]) {
float val = [value floatValue];
[self setValue:@(val) forKey:propertyName];
}
// integer
else if([propertyType isEqualToString:@"i"]) {
int val = [value intValue];
[self setValue:@(val) forKey:propertyName];
}
// long
else if([propertyType isEqualToString:@"l"]) {
long val = [value longValue];
[self setValue:@(val) forKey:propertyName];
}
// short
else if([propertyType isEqualToString:@"s"]) {
short val = [value shortValue];
[self setValue:@(val) forKey:propertyName];
}
// NSString
else if([propertyType isEqualToString:@"NSString"]) {
NSString *val = [NSString stringWithFormat:@"%@", value];
[self setValue:val forKey:propertyName];
}
// NSNumber
else if([propertyType isEqualToString:@"NSNumber"]) {
NSInteger val = [value integerValue];
[self setValue:@(val) forKey:propertyName];
}
// NSDate
else if([propertyType isEqualToString:@"NSDate"]) {
NSString *str = [NSString stringWithFormat:@"%@", value];
NSDate *val = [NSDate dateFromString:str];
[self setValue:val forKey:propertyName];
}
// NSURL
else if([propertyType isEqualToString:@"NSURL"]) {
NSString *str = [NSString stringWithFormat:@"%@", value];
NSURL *val = [NSURL URLWithString:str];
[self setValue:val forKey:propertyName];
}
// NSArray, NSMutableArray
else if([propertyType isEqualToString:@"NSArray"] ||
[propertyType isEqualToString:@"NSMutableArray"]) {
NSMutableArray *arr = [NSMutableArray array];
for(id JSONObject in value) {
PNObject *val = [[NSClassFromString(mappedJSONType) alloc] initWithJSON:JSONObject];
[arr addObject:val];
}
[self setValue:arr forKey:propertyName];
}
// Other LLModel or an unidentified value
else {
BOOL isPNObjectSubclass = [NSClassFromString(propertyType) isSubclassOfClass:[PNObject class]];
if(isPNObjectSubclass) {
PNObject *val = [[NSClassFromString(propertyType) alloc] initWithJSON:value];
[self setValue:val forKey:propertyName];
}
else {
NSString *errorStr = [NSString stringWithFormat:@"Property '%@' could not be assigned any value.", propertyName];
NSLog(@"%@",errorStr);
}
}*/
}
}
#pragma mark PNObjectSubclassing
+ (NSString *) objectClassName {
}
+ (NSDictionary *) objcetMapping {
}
#pragma mark -
@end
+47
View File
@@ -0,0 +1,47 @@
//
// PNObjectConfig.h
// Pods
//
// Created by Giuseppe Nucifora on 08/01/16.
//
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, Environment) {
Development,
Stage,
Production
};
extern NSString* const EnvironmentProduction;
extern NSString* const EnvironmentStage;
extern NSString* const EnvironmentDevelopment;
@interface PNObjectConfig : NSObject
/**
* gets singleton object.
* @return singleton
*/
/**
*
*
* @param clientIdsForEnvironments
* For example,
* @{ PNObjectConfigDevelopment : @"https://development.it/api/v1",
* PNObjectConfigEnvStage : @"ttps://stage.it/api/v1",
* PNObjectConfigEnvProduction : @"ttps://production.it/api/v1"
* }
*
* @return singleton
*/
+ (instancetype) sharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments;
- (void) enableEnvironment:(Environment) env;
@end
+146
View File
@@ -0,0 +1,146 @@
//
// PNObjectConfig.m
// Pods
//
// Created by Giuseppe Nucifora on 08/01/16.
//
//
#import "PNObjectConfig.h"
NSString* const EnvironmentProduction = @"PNObjectConfigEnvProduction";
NSString* const EnvironmentStage = @"PNObjectConfigEnvStage";
NSString* const EnvironmentDevelopment = @"PNObjectConfigDevelopment";
@interface PNObjectConfig()
@property (nonatomic, strong) NSMutableDictionary *configuration;
@property (nonatomic) BOOL devEnabled;
@property (nonatomic) BOOL stageEnabled;
@property (nonatomic) BOOL productionEnabled;
@end
@implementation PNObjectConfig
static PNObjectConfig *SINGLETON = nil;
static bool isFirstAccess = YES;
#pragma mark - Public Method
+ (id)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isFirstAccess = NO;
SINGLETON = [[super allocWithZone:NULL] init];
});
return SINGLETON;
}
#pragma mark - Life Cycle
+ (instancetype) sharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments {
SINGLETON = [self sharedInstance];
if (SINGLETON) {
for (NSString *key in [endpointUrlsForEnvironments allKeys]) {
((void (^)())@{
EnvironmentDevelopment : ^{
NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]];
if (endpointUrl) {
[SINGLETON.configuration setValue:endpointUrl forKey:key];
SINGLETON.devEnabled = YES;
}
},
EnvironmentStage : ^{
NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]];
if (endpointUrl) {
[SINGLETON.configuration setValue:endpointUrl forKey:key];
SINGLETON.stageEnabled = YES;
}
},
EnvironmentProduction : ^{
NSURL * endpointUrl = [NSURL URLWithString:[endpointUrlsForEnvironments objectForKey:key]];
if (endpointUrl) {
[SINGLETON.configuration setValue:endpointUrl forKey:key];
SINGLETON.productionEnabled = YES;
}
}
}[key] ?: ^{
})();
}
NSAssert(SINGLETON.productionEnabled, @"EnvironmentProduction must be valid endpoint url");
NSLog(@"Config : %@",SINGLETON.configuration);
}
return SINGLETON;
}
+ (id) allocWithZone:(NSZone *)zone
{
return [self sharedInstance];
}
+ (id)copyWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone
{
return [self sharedInstance];
}
- (id)copy
{
return [[PNObjectConfig alloc] init];
}
- (id)mutableCopy
{
return [[PNObjectConfig alloc] init];
}
- (id) init
{
if(SINGLETON){
return SINGLETON;
}
if (isFirstAccess) {
[self doesNotRecognizeSelector:_cmd];
}
self = [super init];
if (self) {
_configuration = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void) enableEnvironment:(Environment) env {
switch (env) {
case Development:{
}
break;
case Stage:{
}
break;
case Production:
default:
break;
}
}
@end
+20
View File
@@ -0,0 +1,20 @@
//
// PNObjectSubclassing.h
// Pods
//
// Created by Giuseppe Nucifora on 08/01/16.
//
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol PNObjectSubclassing <NSObject>
@required
+ (NSString *) objectClassName;
+ (NSDictionary *) objcetMapping;
@end
+13
View File
@@ -0,0 +1,13 @@
//
// User.h
// Pods
//
// Created by Giuseppe Nucifora on 08/01/16.
//
//
#import <PNObject/PNObject.h>
@interface User : PNObject
@end
+22
View File
@@ -0,0 +1,22 @@
//
// User.m
// Pods
//
// Created by Giuseppe Nucifora on 08/01/16.
//
//
#import "User.h"
@implementation User
- (instancetype) initWithJSON:(NSDictionary *)JSON {
self = [super initWithJSON:JSON];
if (self) {
}
return self;
}
@end