Skip to content

Commit 2905239

Browse files
author
Shan Ul Haq
committed
updated the date methods in SHRealmObject
1 parent 8445189 commit 2905239

File tree

4 files changed

+200
-5
lines changed

4 files changed

+200
-5
lines changed

Podfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
PODS:
2-
- Realm (0.92.3):
3-
- Realm/Headers (= 0.92.3)
4-
- Realm/Headers (0.92.3)
2+
- Realm (0.93.2):
3+
- Realm/Headers (= 0.93.2)
4+
- Realm/Headers (0.93.2)
55

66
DEPENDENCIES:
77
- Realm
88

99
SPEC CHECKSUMS:
10-
Realm: bf604805d5e897850da488260f93889f510924b3
10+
Realm: 2fdd0ae74f730e190384e0338d8a0d3654df8220
1111

12-
COCOAPODS: 0.37.1
12+
COCOAPODS: 0.37.2

SHModelObject/SHModelObject/SHRealmObject.h

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,41 @@
5656
inputDateType:(kInputDateFormat)inputDateType
5757
mappings:(NSDictionary *)mapping;
5858

59+
/**
60+
* class initializer
61+
*
62+
* @param dictionary dictionary containing key/value pairs for the object
63+
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date
64+
*type.
65+
* @param inputDateFormat NSDateFormatter format specifier which will be used to format the input date. this will use
66+
*the systemTimeZone to create the formatter
67+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
68+
*
69+
* @return object of type instancetype populated with the values from `dictionary`
70+
*/
71+
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
72+
dateConversionOption:(kDateConversionOption)option
73+
inputDateFormat:(NSString *)inputDateFormat
74+
mappings:(NSDictionary *)mapping;
75+
76+
/**
77+
* class initializer
78+
*
79+
* @param dictionary dictionary containing key/value pairs for the object
80+
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date
81+
*type.
82+
* @param inputDateFormatter NSDateFormatter object which will be used to format the input date.
83+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
84+
*
85+
* @return object of type instancetype populated with the values from `dictionary`
86+
*/
87+
88+
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
89+
dateConversionOption:(kDateConversionOption)option
90+
inputDateFormatter:(NSDateFormatter *)inputDateFormatter
91+
mappings:(NSDictionary *)mapping;
92+
93+
5994
/**
6095
* static variant of class initializer with nil check for the dictionary passed
6196
*
@@ -93,6 +128,40 @@
93128
inputDateType:(kInputDateFormat)inputDateType
94129
mappings:(NSDictionary *)mapping;
95130

131+
/**
132+
* static variant of class initializer with nil check for the dictionary passed
133+
*
134+
* @param dictionary dictionary containing key/value pairs for the object
135+
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date
136+
*type.
137+
* @param inputDateFormat NSDateFormatter format specifier which will be used to format the input date. this will use
138+
*the systemTimeZone to create the formatter
139+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
140+
*
141+
* @return object of type instancetype populated with the values from `dictionary`
142+
*/
143+
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary
144+
dateConversionOption:(kDateConversionOption)option
145+
inputDateFormat:(NSString *)inputDateFormat
146+
mappings:(NSDictionary *)mapping;
147+
148+
/**
149+
* static variant of class initializer with nil check for the dictionary passed
150+
*
151+
* @param dictionary dictionary containing key/value pairs for the object
152+
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date
153+
*type.
154+
* @param inputDateFormatter NSDateFormatter object which will be used to format the input date.
155+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
156+
*
157+
* @return object of type instancetype populated with the values from `dictionary`
158+
*/
159+
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary
160+
dateConversionOption:(kDateConversionOption)option
161+
inputDateFormatter:(NSDateFormatter *)inputDateFormatter
162+
mappings:(NSDictionary *)mapping;
163+
164+
96165
/**
97166
* update the instance with new dictionary values
98167
*
@@ -117,6 +186,40 @@
117186
dateConversionOption:(kDateConversionOption)option
118187
inputDateType:(kInputDateFormat)inputDateType
119188
mappings:(NSDictionary *)mapping;
189+
190+
/**
191+
* update the instance with new dictionary values
192+
*
193+
* @param dictionary dictionary containing key/value pairs for the object
194+
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date
195+
*type.
196+
* @param inputDateFormat NSDateFormatter format specifier which will be used to format the input date. this will use
197+
*the systemTimeZone to create the formatter
198+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
199+
*
200+
* @return object of type instancetype populated with the values from `dictionary`
201+
*/
202+
- (instancetype)updateWithDictionary:(NSDictionary *)dictionary
203+
dateConversionOption:(kDateConversionOption)option
204+
inputDateFormat:(NSString *)inputDateFormat
205+
mappings:(NSDictionary *)mapping;
206+
207+
/**
208+
* update the instance with new dictionary values
209+
*
210+
* @param dictionary dictionary containing key/value pairs for the object
211+
* @param option `kDateConverstionOption` to determine what to do when the value from dictionary is a DOT.NET Date
212+
*type.
213+
* @param inputDateFormatter NSDateFormatter object which will be used to format the input date.
214+
* @param mapping dictionary to define the mappings for date conversion and array <-> object.
215+
*
216+
* @return object of type instancetype populated with the values from `dictionary`
217+
*/
218+
- (instancetype)updateWithDictionary:(NSDictionary *)dictionary
219+
dateConversionOption:(kDateConversionOption)option
220+
inputDateFormatter:(NSDateFormatter *)inputDateFormatter
221+
mappings:(NSDictionary *)mapping;
222+
120223
@end
121224

122225
@interface NSString (Additions)

SHModelObject/SHModelObject/SHRealmObject.m

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@
4848

4949
static NSDateFormatter *_simpleDateFormatter;
5050
static NSDateFormatter *_timezoneDateFormatter;
51+
static NSDateFormatter *_customFormatter;
5152

5253
@implementation SHRealmObject {
5354
Ivar *_ivars;
5455
unsigned int _outCount;
5556
kDateConversionOption _converstionOption;
5657
kInputDateFormat _inputDateFormat;
5758
NSDictionary *_mappings;
59+
NSString *_customInputDateFormatString;
5860
}
5961

6062
#pragma mark - Factory methods for object creation
@@ -101,6 +103,41 @@ + (instancetype)objectWithDictionary:(NSDictionary *)dictionary
101103
mappings:mapping];
102104
}
103105

106+
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary
107+
dateConversionOption:(kDateConversionOption)option
108+
inputDateFormat:(NSString *)inputDateFormat
109+
mappings:(NSDictionary *)mapping {
110+
if (nil == dictionary || ![dictionary isKindOfClass:[NSDictionary class]]) {
111+
return nil;
112+
}
113+
114+
if ([dictionary isKindOfClass:[NSNull class]]) {
115+
return nil;
116+
}
117+
return [[[self class] alloc] initWithDictionary:dictionary
118+
dateConversionOption:option
119+
inputDateFormat:inputDateFormat
120+
mappings:mapping];
121+
}
122+
123+
+ (instancetype)objectWithDictionary:(NSDictionary *)dictionary
124+
dateConversionOption:(kDateConversionOption)option
125+
inputDateFormatter:(NSDateFormatter *)inputDateFormatter
126+
mappings:(NSDictionary *)mapping {
127+
if (nil == dictionary || ![dictionary isKindOfClass:[NSDictionary class]]) {
128+
return nil;
129+
}
130+
131+
if ([dictionary isKindOfClass:[NSNull class]]) {
132+
return nil;
133+
}
134+
return [[[self class] alloc] initWithDictionary:dictionary
135+
dateConversionOption:option
136+
inputDateFormatter:inputDateFormatter
137+
mappings:mapping];
138+
}
139+
140+
104141
//
105142
- (instancetype)initWithDictionary:(NSDictionary *)dictionary;
106143
{
@@ -127,6 +164,33 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary
127164
return self;
128165
}
129166

167+
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
168+
dateConversionOption:(kDateConversionOption)option
169+
inputDateFormat:(NSString *)inputDateFormat
170+
mappings:(NSDictionary *)mapping {
171+
if ((self = [super init])) {
172+
return [self updateWithDictionary:dictionary
173+
dateConversionOption:option
174+
inputDateFormat:inputDateFormat
175+
mappings:mapping];
176+
}
177+
return self;
178+
}
179+
180+
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
181+
dateConversionOption:(kDateConversionOption)option
182+
inputDateFormatter:(NSDateFormatter *)inputDateFormatter
183+
mappings:(NSDictionary *)mapping {
184+
if ((self = [super init])) {
185+
return [self updateWithDictionary:dictionary
186+
dateConversionOption:option
187+
inputDateFormatter:inputDateFormatter
188+
mappings:mapping];
189+
}
190+
return self;
191+
}
192+
193+
130194
// NSCoding
131195
- (NSArray *)propertyNames {
132196
NSMutableArray *array = [NSMutableArray array];
@@ -214,6 +278,34 @@ - (instancetype)updateWithDictionary:(NSDictionary *)dictionary
214278
return [self updateWithDictionary:dictionary];
215279
}
216280

281+
- (instancetype)updateWithDictionary:(NSDictionary *)dictionary
282+
dateConversionOption:(kDateConversionOption)option
283+
inputDateFormat:(NSString *)inputDateFormat
284+
mappings:(NSDictionary *)mapping {
285+
_converstionOption = option;
286+
_inputDateFormat = kInputDateFormatCustom;
287+
_customInputDateFormatString = inputDateFormat;
288+
if (_customInputDateFormatString && !_customFormatter) {
289+
_customFormatter = [[NSDateFormatter alloc] init];
290+
[_customFormatter setDateFormat:_customInputDateFormatString];
291+
}
292+
_mappings = mapping;
293+
294+
return [self updateWithDictionary:dictionary];
295+
}
296+
297+
- (instancetype)updateWithDictionary:(NSDictionary *)dictionary
298+
dateConversionOption:(kDateConversionOption)option
299+
inputDateFormatter:(NSDateFormatter *)inputDateFormatter
300+
mappings:(NSDictionary *)mapping {
301+
_converstionOption = option;
302+
_inputDateFormat = kInputDateFormatCustom;
303+
_customFormatter = inputDateFormatter;
304+
_mappings = mapping;
305+
306+
return [self updateWithDictionary:dictionary];
307+
}
308+
217309
#pragma mark - SHModalSerialization protocol methods
218310

219311
- (void)serializeValue:(id)value withKey:(id)key {

0 commit comments

Comments
 (0)