NSValueTransformer+MTLPredefinedTransformerAdditions.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // NSValueTransformer+MTLPredefinedTransformerAdditions.h
  3. // Mantle
  4. //
  5. // Created by Justin Spahr-Summers on 2012-09-27.
  6. // Copyright (c) 2012 GitHub. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "MTLTransformerErrorHandling.h"
  10. /// The name for a value transformer that converts strings into URLs and back.
  11. extern NSString * const MTLURLValueTransformerName;
  12. /// Ensure an NSNumber is backed by __NSCFBoolean/CFBooleanRef
  13. ///
  14. /// NSJSONSerialization, and likely other serialization libraries, ordinarily
  15. /// serialize NSNumbers as numbers, and thus booleans would be serialized as
  16. /// 0/1. The exception is when the NSNumber is backed by __NSCFBoolean, which,
  17. /// though very much an implementation detail, is detected and serialized as a
  18. /// proper boolean.
  19. extern NSString * const MTLBooleanValueTransformerName;
  20. @interface NSValueTransformer (MTLPredefinedTransformerAdditions)
  21. /// An optionally reversible transformer which applies the given transformer to
  22. /// each element of an array.
  23. ///
  24. /// transformer - The transformer to apply to each element. If the transformer
  25. /// is reversible, the transformer returned by this method will be
  26. /// reversible. This argument must not be nil.
  27. ///
  28. /// Returns a transformer which applies a transformation to each element of an
  29. /// array.
  30. + (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_arrayMappingTransformerWithTransformer:(NSValueTransformer *)transformer;
  31. /// A reversible value transformer to transform between the keys and objects of a
  32. /// dictionary.
  33. ///
  34. /// dictionary - The dictionary whose keys and values should be
  35. /// transformed between. This argument must not be nil.
  36. /// defaultValue - The result to fall back to, in case no key matching the
  37. /// input value was found during a forward transformation.
  38. /// reverseDefaultValue - The result to fall back to, in case no value matching
  39. /// the input value was found during a reverse
  40. /// transformation.
  41. ///
  42. /// Can for example be used for transforming between enum values and their string
  43. /// representation.
  44. ///
  45. /// NSValueTransformer *valueTransformer = [NSValueTransformer mtl_valueMappingTransformerWithDictionary:@{
  46. /// @"foo": @(EnumDataTypeFoo),
  47. /// @"bar": @(EnumDataTypeBar),
  48. /// } defaultValue: @(EnumDataTypeUndefined) reverseDefaultValue: @"undefined"];
  49. ///
  50. /// Returns a transformer which will map from keys to objects for forward
  51. /// transformations, and from objects to keys for reverse transformations.
  52. + (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_valueMappingTransformerWithDictionary:(NSDictionary *)dictionary defaultValue:(id)defaultValue reverseDefaultValue:(id)reverseDefaultValue;
  53. /// Returns a value transformer created by calling
  54. /// `+mtl_valueMappingTransformerWithDictionary:defaultValue:reverseDefaultValue:`
  55. /// with a default value of `nil` and a reverse default value of `nil`.
  56. + (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_valueMappingTransformerWithDictionary:(NSDictionary *)dictionary;
  57. /// A value transformer that errors if the transformed value are not of the given
  58. /// class.
  59. ///
  60. /// class - The expected class. This argument must not be nil.
  61. ///
  62. /// Returns a transformer which will return an error if the transformed in value
  63. /// is not a member of class. Otherwise, the value is simply passed through.
  64. + (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_validatingTransformerForClass:(Class)modelClass;
  65. + (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONDictionaryTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +[MTLJSONAdapter dictionaryTransformerWithModelClass:]")));
  66. + (NSValueTransformer<MTLTransformerErrorHandling> *)mtl_JSONArrayTransformerWithModelClass:(Class)modelClass __attribute__((deprecated("Replaced by +[MTLJSONAdapter arrayTransformerWithModelClass:]")));
  67. @end