From 23e15ea42c7f1d4125c3eeaffa1fac2dae87abf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 11 Dec 2012 07:51:15 +0100 Subject: [PATCH] Simplify GRMustacheExpression API --- src/classes/GRMustacheContext.m | 10 +++---- src/classes/GRMustacheContext_private.h | 12 ++++---- src/classes/GRMustacheExpression.m | 4 +-- src/classes/GRMustacheExpression_private.h | 6 ++-- src/classes/GRMustacheFilteredExpression.m | 26 ++++++---------- src/classes/GRMustacheIdentifierExpression.m | 5 ++-- .../GRMustacheImplicitIteratorExpression.m | 9 +++--- src/classes/GRMustacheScopedExpression.m | 11 ++++--- src/classes/GRMustacheTag.m | 9 ++---- src/tests/Private/GRMustacheContextTest.m | 30 +++++++++---------- 10 files changed, 53 insertions(+), 69 deletions(-) diff --git a/src/classes/GRMustacheContext.m b/src/classes/GRMustacheContext.m index 1d6dd064..5c31e781 100644 --- a/src/classes/GRMustacheContext.m +++ b/src/classes/GRMustacheContext.m @@ -254,14 +254,14 @@ - (id)currentContextValue return [[_contextObject retain] autorelease]; } -- (id)contextValueForKey:(NSString *)key isProtected:(BOOL *)isProtected +- (id)contextValueForKey:(NSString *)key protected:(BOOL *)protected { if (_protectedContextObject) { for (GRMustacheContext *context = self; context; context = context.protectedContextParent) { id value = [GRMustacheContext valueForKey:key inObject:context.protectedContextObject]; if (value != nil) { - if (isProtected) { - *isProtected = YES; + if (protected != NULL) { + *protected = YES; } return value; } @@ -283,8 +283,8 @@ - (id)contextValueForKey:(NSString *)key isProtected:(BOOL *)isProtected if (hidden) { continue; } id value = [GRMustacheContext valueForKey:key inObject:contextObject]; if (value != nil) { - if (isProtected) { - *isProtected = NO; + if (protected != NULL) { + *protected = NO; } return value; } diff --git a/src/classes/GRMustacheContext_private.h b/src/classes/GRMustacheContext_private.h index 449bbdc9..85b2b9d3 100644 --- a/src/classes/GRMustacheContext_private.h +++ b/src/classes/GRMustacheContext_private.h @@ -130,22 +130,22 @@ extern BOOL GRMustacheContextDidCatchNSUndefinedKeyException; * Performs a key lookup in the receiver's context stack, and returns the found * value. * - * @param key The searched key. - * @param isProtected Upon return, is YES if the value comes from the protected - * context stack. + * @param key The searched key. + * @param protected Upon return, is YES if the value comes from the protected + * context stack. * * @return The value found in the context stack. * - * @see -[GRMustacheIdentifierExpression evaluateInContext:] + * @see -[GRMustacheIdentifierExpression valueWithContext:] */ -- (id)contextValueForKey:(NSString *)key isProtected:(BOOL *)isProtected GRMUSTACHE_API_INTERNAL; +- (id)contextValueForKey:(NSString *)key protected:(BOOL *)protected GRMUSTACHE_API_INTERNAL; /** * Returns the top object of the receiver's context stack. * * @return The top object of the receiver's context stack. * - * @see -[GRMustacheImplicitIteratorExpression evaluateInContext:] + * @see -[GRMustacheImplicitIteratorExpression valueWithContext:] */ - (id)currentContextValue GRMUSTACHE_API_INTERNAL; diff --git a/src/classes/GRMustacheExpression.m b/src/classes/GRMustacheExpression.m index 87f63dfc..d8a2f989 100644 --- a/src/classes/GRMustacheExpression.m +++ b/src/classes/GRMustacheExpression.m @@ -35,10 +35,10 @@ - (void)dealloc [super dealloc]; } -- (BOOL)evaluateInContext:(GRMustacheContext *)context value:(id *)value isProtected:(BOOL *)isProtected error:(NSError **)error +- (id)valueWithContext:(GRMustacheContext *)context protected:(BOOL *)protected { NSAssert(NO, @"Subclasses must override"); - return NO; + return nil; } - (BOOL)isEqual:(id)anObject diff --git a/src/classes/GRMustacheExpression_private.h b/src/classes/GRMustacheExpression_private.h index 8605fdce..9660e11e 100644 --- a/src/classes/GRMustacheExpression_private.h +++ b/src/classes/GRMustacheExpression_private.h @@ -54,19 +54,17 @@ * Evaluates an expression against a rendering context, and return the value. * * @param context A Mustache rendering context - * @param value TODO * @param protected TODO - * @param error TODO * * @return TODO */ -- (BOOL)evaluateInContext:(GRMustacheContext *)context value:(id *)value isProtected:(BOOL *)isProtected error:(NSError **)error GRMUSTACHE_API_INTERNAL; +- (id)valueWithContext:(GRMustacheContext *)context protected:(BOOL *)protected GRMUSTACHE_API_INTERNAL; /** * Returns a Boolean value that indicates whether the receiver and a given * object are equal. * - * Expressions are equal if and only if the result of their `evaluateInContext:` + * Expressions are equal if and only if the result of their `valueWithContext:` * implementation would return the same value in a given rendering context. * * Default implementation is NSObject's one: subclasses must override. diff --git a/src/classes/GRMustacheFilteredExpression.m b/src/classes/GRMustacheFilteredExpression.m index c0eaa306..a768540f 100644 --- a/src/classes/GRMustacheFilteredExpression.m +++ b/src/classes/GRMustacheFilteredExpression.m @@ -86,17 +86,10 @@ - (BOOL)isEqual:(id)expression #pragma mark GRMustacheExpression -- (BOOL)evaluateInContext:(GRMustacheContext *)context value:(id *)value isProtected:(BOOL *)isProtected error:(NSError **)error +- (id)valueWithContext:(GRMustacheContext *)context protected:(BOOL *)protected { - id argument; - id filter; - - if (![_argumentExpression evaluateInContext:context value:&argument isProtected:NULL error:error]) { - return NO; - } - if (![_filterExpression evaluateInContext:context value:&filter isProtected:NULL error:error]) { - return NO; - } + id argument = [_argumentExpression valueWithContext:context protected:NULL]; + id filter = [_filterExpression valueWithContext:context protected:NULL]; if (filter == nil) { GRMustacheToken *token = self.token; @@ -116,16 +109,15 @@ - (BOOL)evaluateInContext:(GRMustacheContext *)context value:(id *)value isProte } } - if (_curry && [filter respondsToSelector:@selector(curryArgument:)]) { - *value = [(id)filter curryArgument:argument]; - } else { - *value = [(id)filter transformedValue:argument]; + if (protected != NULL) { + *protected = NO; } - if (isProtected) { - *isProtected = NO; + if (_curry && [filter respondsToSelector:@selector(curryArgument:)]) { + return [(id)filter curryArgument:argument]; + } else { + return [(id)filter transformedValue:argument]; } - return YES; } @end diff --git a/src/classes/GRMustacheIdentifierExpression.m b/src/classes/GRMustacheIdentifierExpression.m index 232a2b2e..6ed071b7 100644 --- a/src/classes/GRMustacheIdentifierExpression.m +++ b/src/classes/GRMustacheIdentifierExpression.m @@ -63,10 +63,9 @@ - (BOOL)isEqual:(id)expression #pragma mark - GRMustacheExpression -- (BOOL)evaluateInContext:(GRMustacheContext *)context value:(id *)value isProtected:(BOOL *)isProtected error:(NSError **)error +- (id)valueWithContext:(GRMustacheContext *)context protected:(BOOL *)protected { - *value = [context contextValueForKey:_identifier isProtected:isProtected]; - return YES; + return [context contextValueForKey:_identifier protected:protected]; } @end diff --git a/src/classes/GRMustacheImplicitIteratorExpression.m b/src/classes/GRMustacheImplicitIteratorExpression.m index 08c6a6d7..d55db394 100644 --- a/src/classes/GRMustacheImplicitIteratorExpression.m +++ b/src/classes/GRMustacheImplicitIteratorExpression.m @@ -38,13 +38,12 @@ - (BOOL)isEqual:(id)expression #pragma mark - GRMustacheExpression -- (BOOL)evaluateInContext:(GRMustacheContext *)context value:(id *)value isProtected:(BOOL *)isProtected error:(NSError **)error +- (id)valueWithContext:(GRMustacheContext *)context protected:(BOOL *)protected { - *value = [context currentContextValue]; - if (isProtected) { - *isProtected = NO; + if (protected != NULL) { + *protected = NO; } - return YES; + return [context currentContextValue]; } @end diff --git a/src/classes/GRMustacheScopedExpression.m b/src/classes/GRMustacheScopedExpression.m index 33793932..0ad1de97 100644 --- a/src/classes/GRMustacheScopedExpression.m +++ b/src/classes/GRMustacheScopedExpression.m @@ -77,14 +77,13 @@ - (BOOL)isEqual:(id)expression #pragma mark - GRMustacheExpression -- (BOOL)evaluateInContext:(GRMustacheContext *)context value:(id *)value isProtected:(BOOL *)isProtected error:(NSError **)error +- (id)valueWithContext:(GRMustacheContext *)context protected:(BOOL *)protected { - id scopedValue; - if (![_baseExpression evaluateInContext:context value:&scopedValue isProtected:isProtected error:error]) { - return NO; + id scopedValue = [_baseExpression valueWithContext:context protected:protected]; + if (protected != NULL) { + *protected = NO; } - *value = [GRMustacheContext valueForKey:_scopeIdentifier inObject:scopedValue]; - return YES; + return [GRMustacheContext valueForKey:_scopeIdentifier inObject:scopedValue]; } @end diff --git a/src/classes/GRMustacheTag.m b/src/classes/GRMustacheTag.m index c4b93ac6..f3b397cc 100644 --- a/src/classes/GRMustacheTag.m +++ b/src/classes/GRMustacheTag.m @@ -100,16 +100,13 @@ - (BOOL)renderInBuffer:(NSMutableString *)buffer withContext:(GRMustacheContext // Evaluate expression - __block id object; - BOOL isProtected; - if (![_expression evaluateInContext:context value:&object isProtected:&isProtected error:error]) { - return NO; - } + BOOL protected; + __block id object = [_expression valueWithContext:context protected:&protected]; // Hide object if it is protected - if (isProtected) { + if (protected) { // Object is protected: it may enter the context stack, and provide // value for `.` and `.name`. However, it must not expose its keys. // diff --git a/src/tests/Private/GRMustacheContextTest.m b/src/tests/Private/GRMustacheContextTest.m index dff3fa31..652a9e7c 100644 --- a/src/tests/Private/GRMustacheContextTest.m +++ b/src/tests/Private/GRMustacheContextTest.m @@ -125,7 +125,7 @@ - (void)testOneDepthRuntimeForwardsValueForKeyToItsObject GRKVCRecorder *recorder = [GRKVCRecorder recorderWithRecognizedKey:@"foo"]; GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingObject:recorder]; - [context contextValueForKey:@"foo" isProtected:NULL]; + [context contextValueForKey:@"foo" protected:NULL]; STAssertEqualObjects(recorder.lastAccessedKey, @"foo", nil); } @@ -136,7 +136,7 @@ - (void)testTwoDepthRuntimeForwardsValueForKeyToTopObjectOnlyIfTopObjectHasKey GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingObject:rootRecorder]; context = [context contextByAddingObject:topRecorder]; - STAssertEqualObjects([context contextValueForKey:@"top" isProtected:NULL], @"top", nil); + STAssertEqualObjects([context contextValueForKey:@"top" protected:NULL], @"top", nil); STAssertEqualObjects(topRecorder.lastAccessedKey, @"top", nil); STAssertNil(rootRecorder.lastAccessedKey, nil); } @@ -148,7 +148,7 @@ - (void)testTwoDepthRuntimeForwardsValueForKeyToBothObjectIfTopObjectMisses GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingObject:rootRecorder]; context = [context contextByAddingObject:topRecorder]; - STAssertEqualObjects([context contextValueForKey:@"root" isProtected:NULL], @"root", nil); + STAssertEqualObjects([context contextValueForKey:@"root" protected:NULL], @"root", nil); STAssertEqualObjects(topRecorder.lastAccessedKey, @"root", nil); STAssertEqualObjects(rootRecorder.lastAccessedKey, @"root", nil); } @@ -160,7 +160,7 @@ - (void)testTwoDepthRuntimeMissesIfBothObjectMisses GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingObject:rootRecorder]; context = [context contextByAddingObject:topRecorder]; - STAssertNil([context contextValueForKey:@"foo" isProtected:NULL], nil); + STAssertNil([context contextValueForKey:@"foo" protected:NULL], nil); STAssertEqualObjects(topRecorder.lastAccessedKey, @"foo", nil); STAssertEqualObjects(rootRecorder.lastAccessedKey, @"foo", nil); } @@ -172,7 +172,7 @@ - (void)testNilDoesNotStopsExploration context = [context contextByAddingObject:dictionary]; dictionary = [NSDictionary dictionary]; context = [context contextByAddingObject:dictionary]; - STAssertEqualObjects([context contextValueForKey:@"key" isProtected:NULL], @"foo", nil); + STAssertEqualObjects([context contextValueForKey:@"key" protected:NULL], @"foo", nil); } - (void)testNSNullDoesStopExploration @@ -182,7 +182,7 @@ - (void)testNSNullDoesStopExploration context = [context contextByAddingObject:dictionary]; dictionary = [NSDictionary dictionaryWithObject:[NSNull null] forKey:@"key"]; context = [context contextByAddingObject:dictionary]; - STAssertEqualObjects([context contextValueForKey:@"key" isProtected:NULL], [NSNull null], nil); + STAssertEqualObjects([context contextValueForKey:@"key" protected:NULL], [NSNull null], nil); } - (void)testNSNumberWithBoolNODoesStopExploration @@ -192,7 +192,7 @@ - (void)testNSNumberWithBoolNODoesStopExploration context = [context contextByAddingObject:dictionary]; dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"key"]; context = [context contextByAddingObject:dictionary]; - STAssertEqualObjects([context contextValueForKey:@"key" isProtected:NULL], [NSNumber numberWithBool:NO], nil); + STAssertEqualObjects([context contextValueForKey:@"key" protected:NULL], [NSNumber numberWithBool:NO], nil); } - (void)testOneDepthRuntimeTemplate @@ -222,7 +222,7 @@ - (void)testRuntimeRethrowsNonNSUndefinedKeyException ThrowingObject *throwingObject = [[[ThrowingObject alloc] init] autorelease]; GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingObject:throwingObject]; - STAssertThrows([context contextValueForKey:@"NonNSUndefinedKeyException" isProtected:NULL], nil); + STAssertThrows([context contextValueForKey:@"NonNSUndefinedKeyException" protected:NULL], nil); } - (void)testRuntimeSwallowsNonSelfNSUndefinedKeyException @@ -231,7 +231,7 @@ - (void)testRuntimeSwallowsNonSelfNSUndefinedKeyException ThrowingObject *throwingObject = [[[ThrowingObject alloc] init] autorelease]; GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingObject:throwingObject]; - STAssertNoThrow([context contextValueForKey:@"NonSelfNSUndefinedKeyException" isProtected:NULL], nil); + STAssertNoThrow([context contextValueForKey:@"NonSelfNSUndefinedKeyException" protected:NULL], nil); } - (void)testRuntimeSwallowsSelfNSUndefinedKeyException @@ -239,20 +239,20 @@ - (void)testRuntimeSwallowsSelfNSUndefinedKeyException ThrowingObject *throwingObject = [[[ThrowingObject alloc] init] autorelease]; GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingObject:throwingObject]; - STAssertNoThrow([context contextValueForKey:@"SelfNSUndefinedKeyException" isProtected:NULL], nil); + STAssertNoThrow([context contextValueForKey:@"SelfNSUndefinedKeyException" protected:NULL], nil); } - (void)testContextByAddingProtectedObject { GRMustacheContext *context = [GRMustacheContext context]; context = [context contextByAddingProtectedObject:@{ @"safe": @"important" }]; - STAssertEqualObjects([context contextValueForKey:@"safe" isProtected:NULL], @"important", @""); + STAssertEqualObjects([context contextValueForKey:@"safe" protected:NULL], @"important", @""); context = [context contextByAddingObject:@{ @"safe": @"hack", @"fragile": @"A" }]; - STAssertEqualObjects([context contextValueForKey:@"safe" isProtected:NULL], @"important", @""); - STAssertEqualObjects([context contextValueForKey:@"fragile" isProtected:NULL], @"A", @""); + STAssertEqualObjects([context contextValueForKey:@"safe" protected:NULL], @"important", @""); + STAssertEqualObjects([context contextValueForKey:@"fragile" protected:NULL], @"A", @""); context = [context contextByAddingObject:@{ @"safe": @"hack", @"fragile": @"B" }]; - STAssertEqualObjects([context contextValueForKey:@"safe" isProtected:NULL], @"important", @""); - STAssertEqualObjects([context contextValueForKey:@"fragile" isProtected:NULL], @"B", @""); + STAssertEqualObjects([context contextValueForKey:@"safe" protected:NULL], @"important", @""); + STAssertEqualObjects([context contextValueForKey:@"fragile" protected:NULL], @"B", @""); } @end