From 51e8124101121ce8a863e68d597bab9ccd3628cc Mon Sep 17 00:00:00 2001 From: Mr Anonymous Date: Thu, 11 Feb 2016 13:49:24 -0800 Subject: [PATCH] Add scalarsWhenNonOptional TemplateVar If the scalarsWhenNonOptional template var is set to true, and an Attribute is not optional (requires fix for #286), then we'll generate accessor/mutator methods using the plain attribute name rather than with the Value suffix. --- templates/machine.h.motemplate | 8 ++++++++ templates/machine.m.motemplate | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index b5932bde..116d1b06 100644 --- a/templates/machine.h.motemplate +++ b/templates/machine.h.motemplate @@ -68,6 +68,13 @@ extern const struct <$managedObjectClassName$>UserInfo {<$foreach UserInfo userI */ <$endif$> <$if Attribute.hasDefinedAttributeType$> +<$if (Attribute.hasScalarAttributeType && !Attribute.optional && TemplateVar.scalarsWhenNonOptional)$> +<$if Attribute.isReadonly$> +@property (atomic, readonly) <$Attribute.scalarAttributeType$> <$Attribute.name$>; +<$else$> +@property (atomic) <$Attribute.scalarAttributeType$> <$Attribute.name$>; +<$endif$> +<$else$> <$if Attribute.isReadonly$> @property (nonatomic, strong, readonly) <$Attribute.objectAttributeType$> <$Attribute.name$>; <$else$> @@ -85,6 +92,7 @@ extern const struct <$managedObjectClassName$>UserInfo {<$foreach UserInfo userI <$endif$> //- (BOOL)validate<$Attribute.name.initialCapitalString$>:(id*)value_ error:(NSError**)error_; <$endif$> +<$endif$> <$endforeach do$> <$foreach Relationship noninheritedRelationships do$> <$if Relationship.userInfo.documentation$> diff --git a/templates/machine.m.motemplate b/templates/machine.m.motemplate index 62d2096e..414aea48 100644 --- a/templates/machine.m.motemplate +++ b/templates/machine.m.motemplate @@ -52,7 +52,7 @@ const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserIn + (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key { NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; - <$foreach Attribute noninheritedAttributes do$><$if Attribute.hasDefinedAttributeType$><$if Attribute.hasScalarAttributeType$> + <$foreach Attribute noninheritedAttributes do$><$if Attribute.hasDefinedAttributeType$><$if Attribute.hasScalarAttributeType && (Attribute.optional || !TemplateVar.scalarsWhenNonOptional)$> if ([key isEqualToString:@"<$Attribute.name$>Value"]) { NSSet *affectingKey = [NSSet setWithObject:@"<$Attribute.name$>"]; keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey]; @@ -68,6 +68,18 @@ const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserIn @dynamic <$Attribute.name$>; <$if Attribute.hasScalarAttributeType$> +<$if (!Attribute.optional && TemplateVar.scalarsWhenNonOptional)$> +- (<$Attribute.scalarAttributeType$>)<$Attribute.name$> { + NSNumber *result = [self primitive<$Attribute.name.initialCapitalString$>]; + return [result <$Attribute.scalarAccessorMethodName$>]; +} + +<$if ! Attribute.isReadonly$> +- (void)set<$Attribute.name.initialCapitalString$>:(<$Attribute.scalarAttributeType$>)value_ { + [self setPrimitive<$Attribute.name.initialCapitalString$>:@(value_)]; +} +<$endif$> +<$else$> - (<$Attribute.scalarAttributeType$>)<$Attribute.name$>Value { NSNumber *result = [self <$Attribute.name$>]; @@ -92,6 +104,7 @@ const struct <$managedObjectClassName$>UserInfo <$managedObjectClassName$>UserIn <$endif$> <$endif$> <$endif$> +<$endif$> <$endforeach do$> <$foreach Relationship noninheritedRelationships do$>