Skip to content

Commit ed49afb

Browse files
committed
Merge pull request #6 from johankool/master
EFDataMappingKit rewrite
2 parents 3791eff + fbb27df commit ed49afb

20 files changed

+1254
-841
lines changed

EFDataMappingKit.podspec

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Pod::Spec.new do |s|
2+
s.name = "EFDataMappingKit"
3+
s.version = "0.1.0"
4+
s.summary = "EFDataMappingKit maps data such as those coming from JSON onto an instance using mappings. The mappings are also used to simplify implementing the NSCoding protocol for a class, and to create a dictionary representation of an instance."
5+
s.homepage = "https://github.com/Egeniq/EFDataMappingKit"
6+
s.license = { :type => "MIT", :file => "LICENSE" }
7+
s.author = { "Johan Kool" => "[email protected]" }
8+
s.platform = :ios
9+
s.ios.deployment_target = "5.1.1"
10+
s.source = { :git => "https://github.com/Egeniq/EFDataMappingKit.git", :tag => s.version.to_s }
11+
s.public_header_files = 'EFMapping/*.h'
12+
s.source_files = 'EFMapping/*.{h,m}'
13+
s.requires_arc = true
14+
end

EFMapping.xcodeproj/project.pbxproj renamed to EFDataMappingKit.xcodeproj/project.pbxproj

+75-45
Large diffs are not rendered by default.

EFMapping.xcodeproj/project.xcworkspace/contents.xcworkspacedata renamed to EFDataMappingKit.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EFMapping.podspec

-14
This file was deleted.

EFMapping/EFDataMappingKit.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// EFDataMappingKit.h
3+
// EFDataMappingKit
4+
//
5+
// Created by Johan Kool on 23/4/2014.
6+
// Copyright (c) 2014 Egeniq. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
#import "EFRequires.h"
12+
#import "EFMapper.h"
13+
#import "EFMapping.h"
14+
#import "EFMappingError.h"
15+

EFMapping/EFMapper.h

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//
2+
// EFMapper.h
3+
// EFDataMappingKit
4+
//
5+
// Created by Johan Kool on 23/4/2014.
6+
// Copyright (c) 2014 Egeniq. All rights reserved.
7+
//
8+
9+
/**
10+
* Block called when the mapper needs to instantiate a class
11+
*
12+
* Note that the initializer may be called for subclasses of the class under which you registered it too!
13+
*
14+
* @param aClass Requested class
15+
* @param values Values to be applied to the instance
16+
*
17+
* @return A class instance
18+
*/
19+
typedef id (^EFMappingInitializerBlock)(Class aClass, NSDictionary *values);
20+
21+
/**
22+
* EFMapper maps data such as those coming from JSON onto an instance using mappings. The mappings are also used to simplify implementing the NSCoding protocol for a class, and to create a dictionary representation of an instance.
23+
*/
24+
@interface EFMapper : NSObject
25+
26+
#pragma mark - Singleton
27+
28+
/**
29+
* Convenience singleton
30+
*
31+
* You don't have to use EFMapper as a singleton, but it can often be convenient.
32+
*
33+
* @return A EFMapper instance
34+
*/
35+
+ (instancetype)sharedInstance;
36+
37+
#pragma mark - Mappings
38+
39+
/**
40+
* Mappings to be used for setting values on instances of a class
41+
*
42+
* Mappings are also used for subclasses of the class, unless more specific mappings for that subclass are registered.
43+
*
44+
* @param mappings Array of EFMapping instances
45+
* @param aClass Class for which the mappings should be used
46+
*/
47+
- (void)registerMappings:(NSArray *)mappings forClass:(Class)aClass;
48+
49+
#pragma mark - Initializers
50+
51+
/**
52+
* Register initializer to be called when the mapper needs to instantiate a class
53+
*
54+
* The initializer is also used for subclasses of the class, unless a more initializer mappings for that subclass is registered. If no initializer is registed, alloc and init will be called on the class.
55+
*
56+
* @param initializerBlock Block returning class instance
57+
* @param aClass Class for which the initializer should be used
58+
*/
59+
- (void)registerInitializer:(EFMappingInitializerBlock)initializerBlock forClass:(Class)aClass;
60+
61+
#pragma mark - Validating and applying values
62+
63+
/**
64+
* Validate values to be applied to an instance of a class
65+
*
66+
* Verifies wether the values in a dictionary can be successfully applied on the instance of a class using its mappings. This basically means that values get checked on wether they are of the right type.
67+
*
68+
* @param values The values to be validated
69+
* @param aClass Class of object
70+
* @param error Error when invalid values are encountered
71+
*
72+
* @return YES if all values are valid, NO otherwise
73+
*/
74+
- (BOOL)validateValues:(NSDictionary *)values forClass:(Class)aClass error:(NSError **)error;
75+
76+
/**
77+
* Validate values to be applied to an instance
78+
*
79+
* Verifies wether the values in a dictionary can be successfully applied on the instance of a class using its mappings. This basically means that values get checked on wether they are of the right type. In this method instances also get a chance to validate a value using the standard KVO validation. To use this implement -[validate<Key>:error:] method in your class.
80+
*
81+
* @param values The values to be validated
82+
* @param onObject The object
83+
* @param error Error when invalid values are encountered
84+
*
85+
* @return YES if all values are valid, NO otherwise
86+
*/
87+
- (BOOL)validateValues:(NSDictionary *)values onObject:(id)object error:(NSError **)error;
88+
89+
/**
90+
* Apply values to an instance
91+
*
92+
* First validates the values, and if all are found to be valid applies them on the instance of a class using its mappings.
93+
*
94+
* @param values The values to be applied
95+
* @param object The object
96+
* @param error Error when invalid values are encountered
97+
*
98+
* @return YES if all values are valid, NO otherwise
99+
*/
100+
- (BOOL)setValues:(NSDictionary *)values onObject:(id)object error:(NSError **)error;
101+
102+
/**
103+
* Initializes an object and applies values
104+
*
105+
* @param aClass Class of object
106+
* @param values The values to be applied
107+
* @param error Error when invalid values are encountered
108+
*
109+
* @return New object if all values are valid, nil otherwise
110+
*/
111+
- (id)objectOfClass:(Class)aClass withValues:(NSDictionary *)values error:(NSError **)error;
112+
113+
#pragma mark - NSCoding support
114+
115+
/**
116+
* Encodes an instance using mappings
117+
*
118+
* @param object The object
119+
* @param aCoder The NSCoder object from the -[encodeWithCoder:] method
120+
*/
121+
- (void)encodeObject:(id)object withCoder:(NSCoder *)aCoder;
122+
123+
/**
124+
* Decodes an instance using mappings
125+
*
126+
* The coding confirms to NSSecureCoding, so you may wish to return YES from -[ requiresSecureCoding] in your subclass.
127+
*
128+
* @param object The object
129+
* @param aDecoder The NSCoder object from the -[initWithCoder:] method
130+
*/
131+
- (void)decodeObject:(id)object withCoder:(NSCoder *)aDecoder;
132+
133+
#pragma mark - Dictionary representation
134+
135+
/**
136+
* Registers the keys that should be included in a dictionary representation
137+
*
138+
* By default all external keys defined in the mappings for the class are included.
139+
*
140+
* @param keys Array of NSString keys
141+
* @param aClass Class of object
142+
*/
143+
- (void)registerDictionaryRepresentationKeys:(NSArray *)keys forClass:(Class)aClass;
144+
145+
/**
146+
* Creates a dictionary representation
147+
*
148+
* The dictionary is created using the passed in array of keys. For classes without any mappings, the object is included as is. If a value is nil, or its reverse transformation fails, the key will be set to NSNull.
149+
*
150+
* @param object The object
151+
* @param keys The keys to include in the dictionary, pass nil to include all
152+
*
153+
* @return Dictionary representation of the object
154+
*/
155+
- (id)dictionaryRepresentationOfObject:(id)object forKeys:(NSArray *)keys;
156+
157+
/**
158+
* Creates a dictionary representation
159+
*
160+
* The dictionary is created using the external keys as registered with the mapper, or if none registered those registered for the mappings of the class. For classes without any mappings, the object is included as is. If a value is nil, or its reverse transformation fails, the key will be set to NSNull.
161+
*
162+
* @return Dictionary representation of the object
163+
*/
164+
- (id)dictionaryRepresentationOfObject:(id)object;
165+
166+
@end

0 commit comments

Comments
 (0)