FEMAttribute.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // For License please refer to LICENSE file in the root of FastEasyMapping project
  2. #import "FEMAttribute.h"
  3. @implementation FEMAttribute {
  4. FEMMapBlock _map;
  5. FEMMapBlock _reverseMap;
  6. }
  7. @synthesize property = _property;
  8. @synthesize keyPath = _keyPath;
  9. #pragma mark - Init
  10. - (id)initWithProperty:(NSString *)property keyPath:(NSString *)keyPath map:(FEMMapBlock)map reverseMap:(FEMMapBlock)reverseMap {
  11. NSParameterAssert(property.length > 0);
  12. self = [super init];
  13. if (self) {
  14. self.property = property;
  15. self.keyPath = keyPath;
  16. FEMMapBlock passthroughMap = ^(id value) {
  17. return value;
  18. };
  19. _map = map ?: passthroughMap;
  20. _reverseMap = reverseMap ?: passthroughMap;
  21. }
  22. return self;
  23. }
  24. + (instancetype)mappingOfProperty:(NSString *)property toKeyPath:(NSString *)keyPath map:(FEMMapBlock)map reverseMap:(FEMMapBlock)reverseMap {
  25. return [[self alloc] initWithProperty:property keyPath:keyPath map:map reverseMap:reverseMap];
  26. }
  27. #pragma mark - Description
  28. - (NSString *)description {
  29. return [NSString stringWithFormat:
  30. @"<%@ %p> property:%@ keyPath:%@",
  31. NSStringFromClass(self.class),
  32. (__bridge void *) self,
  33. self.property,
  34. self.keyPath
  35. ];
  36. }
  37. #pragma mark - Mapping
  38. - (id)mapValue:(id)value {
  39. return _map(value);
  40. }
  41. - (id)reverseMapValue:(id)value {
  42. return _reverseMap(value);
  43. }
  44. @end
  45. @implementation FEMAttribute (Shortcut)
  46. + (instancetype)mappingOfProperty:(NSString *)property toKeyPath:(NSString *)keyPath map:(FEMMapBlock)map {
  47. return [self mappingOfProperty:property toKeyPath:keyPath map:map reverseMap:NULL];
  48. }
  49. + (instancetype)mappingOfProperty:(NSString *)property toKeyPath:(NSString *)keyPath {
  50. return [self mappingOfProperty:property toKeyPath:keyPath map:NULL reverseMap:NULL];
  51. }
  52. + (instancetype)mappingOfProperty:(NSString *)property {
  53. return [self mappingOfProperty:property toKeyPath:nil map:NULL reverseMap:NULL];
  54. }
  55. + (instancetype)mappingOfProperty:(NSString *)property map:(FEMMapBlock)map {
  56. return [self mappingOfProperty:property toKeyPath:nil map:map reverseMap:NULL];
  57. }
  58. + (nonnull instancetype)mappingOfProperty:(nonnull NSString *)property reverseMap:(nonnull FEMMapBlock)reverseMap {
  59. return [self mappingOfProperty:property toKeyPath:nil map:NULL reverseMap:reverseMap];
  60. }
  61. + (instancetype)mappingOfProperty:(NSString *)property toKeyPath:(NSString *)keyPath dateFormat:(NSString *)dateFormat {
  62. NSDateFormatter *formatter = [NSDateFormatter new];
  63. [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
  64. [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
  65. [formatter setDateFormat:dateFormat];
  66. return [self mappingOfProperty:property toKeyPath:keyPath map:^id(id value) {
  67. return [value isKindOfClass:[NSString class]] ? [formatter dateFromString:value] : NSNull.null;
  68. } reverseMap:^id(id value) {
  69. return [formatter stringFromDate:value];
  70. }];
  71. }
  72. + (instancetype)mappingOfURLProperty:(NSString *)property toKeyPath:(NSString *)keyPath {
  73. return [FEMAttribute mappingOfProperty:property toKeyPath:keyPath map:^id(id value) {
  74. return [value isKindOfClass:NSString.class] ? [NSURL URLWithString:value] : NSNull.null;
  75. } reverseMap:^id(NSURL *value) {
  76. return [value absoluteString];
  77. }];
  78. }
  79. @end