Skip to content
Browse files

Overridable partials implementation cleanup

  • Loading branch information...
1 parent d733cda commit 291f25468fccf5455bb44dc3aad35395d317fa8b @groue committed
View
3 src/classes/GRMustacheRenderingElement_private.h
@@ -86,7 +86,8 @@
* _element_ parameter.
*
* All classes conforming to the GRMustacheRenderingElement protocol return
- * _element_, but GRMustacheSectionElement and GRMustacheTemplateOverride.
+ * _element_, but GRMustacheSectionElement, GRMustacheTemplateOverride, and
+ * GRMustacheTemplate.
*
* @param element A rendering element
*
View
61 src/classes/GRMustacheRuntime.m
@@ -33,6 +33,7 @@
#import "GRMustacheNSUndefinedKeyExceptionGuard_private.h"
#import "GRMustacheFilterLibrary_private.h"
#import "GRMustacheError.h"
+#import "GRMustacheTemplateOverride_private.h"
#if !defined(NS_BLOCK_ASSERTIONS)
BOOL GRMustacheRuntimeDidCatchNSUndefinedKeyException;
@@ -43,12 +44,12 @@
@interface GRMustacheRuntime()
+ (BOOL)objectIsFoundationCollectionWhoseImplementationOfValueForKeyReturnsAnotherCollection:(id)object;
- (id)initWithTemplate:(GRMustacheTemplate *)template contextObject:(id)contextObject;
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride templateDelegate:(id<GRMustacheTemplateDelegate>)templateDelegate;
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride contextObject:(id)contextObject;
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride filterObject:(id)filterObject;
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride renderingOverride:(id<GRMustacheRenderingOverride>)renderingOverride;
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride templateDelegate:(id<GRMustacheTemplateDelegate>)templateDelegate;
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride contextObject:(id)contextObject;
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride filterObject:(id)filterObject;
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride templateOverride:(GRMustacheTemplateOverride *)templateOverride;
- (id<GRMustacheRenderingElement>)resolveOverridableRenderingElement:(id<GRMustacheRenderingElement>)element;
-- (void)assertAcyclicRenderingOverride:(id<GRMustacheRenderingOverride>)renderingOverride;
+- (void)assertAcyclicTemplateOverride:(GRMustacheTemplateOverride *)templateOverride;
@end
@implementation GRMustacheRuntime
@@ -88,7 +89,7 @@ - (GRMustacheRuntime *)runtimeByAddingTemplateDelegate:(id<GRMustacheTemplateDel
parentHasContext:_contextObject || _parentHasContext
parentHasFilter:_filterObject || _parentHasFilter
parentHasTemplateDelegate:_templateDelegate || _parentHasTemplateDelegate
- parentHasRenderingOverride:_renderingOverride || _parentHasRenderingOverride
+ parentHasTemplateOverride:_templateOverride || _parentHasTemplateOverride
templateDelegate:templateDelegate] autorelease];
}
@@ -103,7 +104,7 @@ - (GRMustacheRuntime *)runtimeByAddingContextObject:(id)contextObject
parentHasContext:_contextObject || _parentHasContext
parentHasFilter:_filterObject || _parentHasFilter
parentHasTemplateDelegate:_templateDelegate || _parentHasTemplateDelegate
- parentHasRenderingOverride:_renderingOverride || _parentHasRenderingOverride
+ parentHasTemplateOverride:_templateOverride || _parentHasTemplateOverride
contextObject:contextObject] autorelease];
}
@@ -118,25 +119,25 @@ - (GRMustacheRuntime *)runtimeByAddingFilterObject:(id)filterObject;
parentHasContext:_contextObject || _parentHasContext
parentHasFilter:_filterObject || _parentHasFilter
parentHasTemplateDelegate:_templateDelegate || _parentHasTemplateDelegate
- parentHasRenderingOverride:_renderingOverride || _parentHasRenderingOverride
+ parentHasTemplateOverride:_templateOverride || _parentHasTemplateOverride
filterObject:filterObject] autorelease];
}
-- (GRMustacheRuntime *)runtimeByAddingRenderingOverride:(id<GRMustacheRenderingOverride>)renderingOverride
+- (GRMustacheRuntime *)runtimeByAddingTemplateOverride:(GRMustacheTemplateOverride *)templateOverride
{
- if (renderingOverride == nil) {
+ if (templateOverride == nil) {
return self;
}
- [self assertAcyclicRenderingOverride:renderingOverride];
+ [self assertAcyclicTemplateOverride:templateOverride];
return [[[GRMustacheRuntime alloc] initWithTemplate:_template
parent:self
parentHasContext:_contextObject || _parentHasContext
parentHasFilter:_filterObject || _parentHasFilter
parentHasTemplateDelegate:_templateDelegate || _parentHasTemplateDelegate
- parentHasRenderingOverride:_renderingOverride || _parentHasRenderingOverride
- renderingOverride:renderingOverride] autorelease];
+ parentHasTemplateOverride:_templateOverride || _parentHasTemplateOverride
+ templateOverride:templateOverride] autorelease];
}
- (void)dealloc
@@ -228,24 +229,24 @@ - (void)delegateValue:(id)value interpretation:(GRMustacheInterpretation)interpr
- (id<GRMustacheRenderingElement>)resolveOverridableRenderingElement:(id<GRMustacheRenderingElement>)element
{
- if (_renderingOverride) {
- element = [_renderingOverride resolveOverridableRenderingElement:element];
+ if (_templateOverride) {
+ element = [_templateOverride resolveOverridableRenderingElement:element];
}
- if (_parentHasRenderingOverride) {
+ if (_parentHasTemplateOverride) {
element = [_parent resolveOverridableRenderingElement:element];
}
return element;
}
-- (void)assertAcyclicRenderingOverride:(id<GRMustacheRenderingOverride>)renderingOverride
+- (void)assertAcyclicTemplateOverride:(GRMustacheTemplateOverride *)templateOverride
{
- if (_renderingOverride) {
- if ([_renderingOverride isEqual:renderingOverride]) {
+ if (_templateOverride) {
+ if (_templateOverride.template == templateOverride.template) {
[NSException raise:GRMustacheRenderingException format:@"Override cycle"];
}
}
- if (_parentHasRenderingOverride) {
- [_parent assertAcyclicRenderingOverride:renderingOverride];
+ if (_parentHasTemplateOverride) {
+ [_parent assertAcyclicTemplateOverride:templateOverride];
}
}
@@ -306,7 +307,7 @@ - (id)initWithTemplate:(GRMustacheTemplate *)template contextObject:(id)contextO
return self;
}
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride templateDelegate:(id<GRMustacheTemplateDelegate>)templateDelegate
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride templateDelegate:(id<GRMustacheTemplateDelegate>)templateDelegate
{
self = [super init];
if (self) {
@@ -316,12 +317,12 @@ - (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime
_parentHasContext = parentHasContext;
_parentHasFilter = parentHasFilter;
_parentHasTemplateDelegate = parentHasTemplateDelegate;
- _parentHasRenderingOverride = parentHasRenderingOverride;
+ _parentHasTemplateOverride = parentHasTemplateOverride;
}
return self;
}
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride contextObject:(id)contextObject
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride contextObject:(id)contextObject
{
self = [super init];
if (self) {
@@ -331,12 +332,12 @@ - (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime
_parentHasContext = parentHasContext;
_parentHasFilter = parentHasFilter;
_parentHasTemplateDelegate = parentHasTemplateDelegate;
- _parentHasRenderingOverride = parentHasRenderingOverride;
+ _parentHasTemplateOverride = parentHasTemplateOverride;
}
return self;
}
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride filterObject:(id)filterObject
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride filterObject:(id)filterObject
{
self = [super init];
if (self) {
@@ -346,22 +347,22 @@ - (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime
_parentHasContext = parentHasContext;
_parentHasFilter = parentHasFilter;
_parentHasTemplateDelegate = parentHasTemplateDelegate;
- _parentHasRenderingOverride = parentHasRenderingOverride;
+ _parentHasTemplateOverride = parentHasTemplateOverride;
}
return self;
}
-- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasRenderingOverride:(BOOL)parentHasRenderingOverride renderingOverride:(id<GRMustacheRenderingOverride>)renderingOverride
+- (id)initWithTemplate:(GRMustacheTemplate *)template parent:(GRMustacheRuntime *)parent parentHasContext:(BOOL)parentHasContext parentHasFilter:(BOOL)parentHasFilter parentHasTemplateDelegate:(BOOL)parentHasTemplateDelegate parentHasTemplateOverride:(BOOL)parentHasTemplateOverride templateOverride:(GRMustacheTemplateOverride *)templateOverride
{
self = [super init];
if (self) {
_template = [template retain];
_parent = [parent retain];
- _renderingOverride = [renderingOverride retain];
+ _templateOverride = [templateOverride retain];
_parentHasContext = parentHasContext;
_parentHasFilter = parentHasFilter;
_parentHasTemplateDelegate = parentHasTemplateDelegate;
- _parentHasRenderingOverride = parentHasRenderingOverride;
+ _parentHasTemplateOverride = parentHasTemplateOverride;
}
return self;
}
View
23 src/classes/GRMustacheRuntime_private.h
@@ -28,22 +28,7 @@
@protocol GRMustacheRenderingElement;
@class GRMustacheTemplate;
@class GRMustacheToken;
-
-/**
- * TODO
- */
-@protocol GRMustacheRenderingOverride <NSObject>
-@required
-/**
- * TODO
- */
-- (id<GRMustacheRenderingElement>)resolveOverridableRenderingElement:(id<GRMustacheRenderingElement>)element GRMUSTACHE_API_INTERNAL;
-
-/**
- * TODO
- */
-- (BOOL)isEqual:(id)anObject;
-@end
+@class GRMustacheTemplateOverride;
#if !defined(NS_BLOCK_ASSERTIONS)
/**
@@ -71,11 +56,11 @@ extern BOOL GRMustacheRuntimeDidCatchNSUndefinedKeyException;
BOOL _parentHasContext;
BOOL _parentHasFilter;
BOOL _parentHasTemplateDelegate;
- BOOL _parentHasRenderingOverride;
+ BOOL _parentHasTemplateOverride;
GRMustacheRuntime *_parent;
GRMustacheTemplate *_template;
id<GRMustacheTemplateDelegate> _templateDelegate;
- id<GRMustacheRenderingOverride> _renderingOverride;
+ GRMustacheTemplateOverride *_templateOverride;
id _contextObject;
id _filterObject;
}
@@ -192,7 +177,7 @@ extern BOOL GRMustacheRuntimeDidCatchNSUndefinedKeyException;
/**
* TODO
*/
-- (GRMustacheRuntime *)runtimeByAddingRenderingOverride:(id<GRMustacheRenderingOverride>)renderingOverride GRMUSTACHE_API_INTERNAL;
+- (GRMustacheRuntime *)runtimeByAddingTemplateOverride:(GRMustacheTemplateOverride *)templateOverride GRMUSTACHE_API_INTERNAL;
/**
* Performs a key lookup in the receiver's context stack, and returns the found
View
35 src/classes/GRMustacheTemplateOverride.m
@@ -22,9 +22,9 @@
#import "GRMustacheTemplateOverride_private.h"
#import "GRMustacheTemplate_private.h"
+#import "GRMustacheRuntime_private.h"
@interface GRMustacheTemplateOverride()
-@property (nonatomic, retain, readonly) GRMustacheTemplate *template;
- (id)initWithTemplate:(GRMustacheTemplate *)template innerElements:(NSArray *)innerElements;
@end
@@ -43,34 +43,11 @@ - (void)dealloc
[super dealloc];
}
-
-#pragma mark - GRMustacheRenderingOverride + GRMustacheRenderingElement
-
-- (id<GRMustacheRenderingElement>)resolveOverridableRenderingElement:(id<GRMustacheRenderingElement>)element
-{
- for (id<GRMustacheRenderingElement> innerElement in _innerElements) {
- element = [innerElement resolveOverridableRenderingElement:element];
- }
- return element;
-}
-
-
-#pragma mark - GRMustacheRenderingOverride
-
-- (BOOL)isEqual:(id)anObject
-{
- if (![anObject isKindOfClass:[GRMustacheTemplateOverride class]]) {
- return NO;
- }
-
- return (((GRMustacheTemplateOverride *)anObject).template == _template);
-}
-
#pragma mark - GRMustacheRenderingElement
- (void)renderInBuffer:(NSMutableString *)buffer withRuntime:(GRMustacheRuntime *)runtime
{
- runtime = [runtime runtimeByAddingRenderingOverride:self];
+ runtime = [runtime runtimeByAddingTemplateOverride:self];
[_template renderInBuffer:buffer withRuntime:runtime];
}
@@ -79,6 +56,14 @@ - (BOOL)isOverridable
return NO;
}
+- (id<GRMustacheRenderingElement>)resolveOverridableRenderingElement:(id<GRMustacheRenderingElement>)element
+{
+ for (id<GRMustacheRenderingElement> innerElement in _innerElements) {
+ element = [innerElement resolveOverridableRenderingElement:element];
+ }
+ return element;
+}
+
#pragma mark - Private
View
16 src/classes/GRMustacheTemplateOverride_private.h
@@ -23,7 +23,6 @@
#import <Foundation/Foundation.h>
#import "GRMustacheAvailabilityMacros_private.h"
#import "GRMustacheRenderingElement_private.h"
-#import "GRMustacheRuntime_private.h"
@class GRMustacheTemplate;
@@ -31,20 +30,27 @@
* A GRMustacheTemplateOverride is a rendering element that renders overridable
* partials as `{{<name}}...{{/name}}`.
*
- * It conforms to the `GRMustacheRenderingOverride` protocol, so that it
- * collaborates with runtimes for the resolving of rendering elements in the
+ * It collaborates with runtimes for the resolving of rendering elements in the
* context of Mustache overridable partials.
*
* @see GRMustacheRenderingElement
- * @see GRMustacheRenderingOverride
* @see GRMustacheRuntime
*/
-@interface GRMustacheTemplateOverride : NSObject<GRMustacheRenderingElement, GRMustacheRenderingOverride> {
+@interface GRMustacheTemplateOverride : NSObject<GRMustacheRenderingElement> {
GRMustacheTemplate *_template;
NSArray *_innerElements;
}
/**
+ * The overridable partial template.
+ *
+ * This property is used by [GRMustacheRuntime assertAcyclicTemplateOverride:].
+ *
+ * @see GRMustacheRuntime
+ */
+@property (nonatomic, retain, readonly) GRMustacheTemplate *template GRMUSTACHE_API_INTERNAL;
+
+/**
* Builds a GRMustacheTemplateOverride.
*
* @param template The partial template that is overriden

0 comments on commit 291f254

Please sign in to comment.
Something went wrong with that request. Please try again.