Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Introduce GRMustacheNumberFormatterHelper

  • Loading branch information...
commit 32931033c83f4323ae9a090c1bbacf30098b008b 1 parent 8a1dce8
@groue authored
View
1  Classes/GRMustache.h
@@ -165,3 +165,4 @@ typedef struct {
#import "GRMustacheEnvironment.h"
#import "GRMustacheError.h"
#import "GRMustacheVersion.h"
+#import "GRMustacheNumberFormatterHelper.h"
View
33 Classes/GRMustacheNumberFormatterHelper.h
@@ -0,0 +1,33 @@
+// The MIT License
+//
+// Copyright (c) 2010 Gwendal Roué
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import <Foundation/Foundation.h>
+#import "GRMustacheAvailabilityMacros.h"
+#import "GRMustacheLambda.h"
+
+@interface GRMustacheNumberFormatterHelper : NSObject<GRMustacheHelper> {
+@private
+ NSNumberFormatter *numberFormatter;
+}
+@property (nonatomic, readonly, retain) NSNumberFormatter *numberFormatter AVAILABLE_GRMUSTACHE_VERSION_1_9_AND_LATER;
++ (id)helperWithNumberFormatter:(NSNumberFormatter *)numberFormatter AVAILABLE_GRMUSTACHE_VERSION_1_9_AND_LATER;
+@end
View
104 Classes/GRMustacheNumberFormatterHelper.m
@@ -0,0 +1,104 @@
+// The MIT License
+//
+// Copyright (c) 2010 Gwendal Roué
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import "GRMustache.h"
+#import "GRMustacheNumberFormatterHelper.h"
+
+@interface GRNumberFormatterContext : NSObject {
+@private
+ NSNumberFormatter *numberFormatter;
+ id wrappedContext;
+}
+@property (nonatomic, retain) NSNumberFormatter *numberFormatter;
+@property (nonatomic, retain) id wrappedContext;
+@end
+
+@interface GRMustacheNumberFormatterHelper()
+@property (nonatomic, retain) NSNumberFormatter *numberFormatter;
+@end
+
+@implementation GRMustacheNumberFormatterHelper
+@synthesize numberFormatter;
+
+- (void)dealloc
+{
+ self.numberFormatter = nil;
+ [super dealloc];
+}
+
++ (id)helperWithNumberFormatter:(NSNumberFormatter *)numberFormatter
+{
+ GRMustacheNumberFormatterHelper *helper = [[[GRMustacheNumberFormatterHelper alloc] init] autorelease];
+ helper.numberFormatter = numberFormatter;
+ return helper;
+}
+
+- (NSString *)renderSection:(GRMustacheSection *)section withContext:(id)context
+{
+ if (numberFormatter == nil) {
+ return [section renderObject:context];
+ }
+
+ // Let's replace the current context with a GRNumberFormatterContext
+ // that will output formatted numbers instead of raw numbers.
+ GRNumberFormatterContext *numberFormatterContext = [[GRNumberFormatterContext alloc] init];
+ numberFormatterContext.wrappedContext = context;
+ numberFormatterContext.numberFormatter = numberFormatter;
+ NSString *string = [section renderObject:numberFormatterContext];
+ [numberFormatterContext release];
+ return string;
+}
+
+@end
+
+@implementation GRNumberFormatterContext
+@synthesize numberFormatter;
+@synthesize wrappedContext;
+
+- (void)dealloc
+{
+ self.numberFormatter = nil;
+ self.wrappedContext = nil;
+ [super dealloc];
+}
+
+- (id)valueForKey:(NSString *)key
+{
+ // Fetch the value that we may format
+ id value = [wrappedContext valueForKey:key];
+
+ // We format only numbers
+ if (![value isKindOfClass:[NSNumber class]]) {
+ return value;
+ }
+
+ // Let's not mess with booleans, which control mustache section logic.
+ if ((void *)value == kCFBooleanFalse || (void *)value == kCFBooleanTrue) {
+ return value;
+ }
+
+ // Let's format our number
+ return [numberFormatter stringFromNumber:(NSNumber *)value];
+}
+
+@end
+
View
48 GRMustache1-ios.xcodeproj/project.pbxproj
@@ -150,6 +150,12 @@
56D585781447F7E300DA3F67 /* GRMustacheContextStrategy.m in Sources */ = {isa = PBXBuildFile; fileRef = 563BF72F14407B0D005A4F58 /* GRMustacheContextStrategy.m */; };
56D585791447F7F400DA3F67 /* GRMustacheNSUndefinedKeyExceptionGuard.m in Sources */ = {isa = PBXBuildFile; fileRef = 563BF71D14404493005A4F58 /* GRMustacheNSUndefinedKeyExceptionGuard.m */; };
56D5857A1447F7F800DA3F67 /* GRMustacheProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 563BF71F14404493005A4F58 /* GRMustacheProperty.m */; };
+ 56D8810D14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 56D8810B14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 56D8810E14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 56D8810B14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 56D8810F14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 56D8810C14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m */; };
+ 56D8811014CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 56D8810C14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m */; };
+ 56D8811614CE91BD00AB3CE4 /* GRMustacheHelperTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 56D8811314CE91BD00AB3CE4 /* GRMustacheHelperTest.m */; };
+ 56D8811714CE91BD00AB3CE4 /* GRMustacheNumberFormatterHelperTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 56D8811514CE91BD00AB3CE4 /* GRMustacheNumberFormatterHelperTest.m */; };
56FDC0A6144AD9B400022D47 /* GRMustacheContextStrategyTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 56FDC0A5144AD9B400022D47 /* GRMustacheContextStrategyTest.m */; };
/* End PBXBuildFile section */
@@ -279,6 +285,12 @@
5672485913E0310D0043E2FA /* Model.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Model.xcdatamodel; sourceTree = "<group>"; };
5672485E13E0311D0043E2FA /* syntax_error.conf */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = syntax_error.conf; sourceTree = "<group>"; };
569C86751424A6CE00D6BC77 /* GRBoolean_private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRBoolean_private.h; sourceTree = "<group>"; };
+ 56D8810B14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheNumberFormatterHelper.h; sourceTree = "<group>"; };
+ 56D8810C14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheNumberFormatterHelper.m; sourceTree = "<group>"; };
+ 56D8811214CE91BD00AB3CE4 /* GRMustacheHelperTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheHelperTest.h; sourceTree = "<group>"; };
+ 56D8811314CE91BD00AB3CE4 /* GRMustacheHelperTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheHelperTest.m; sourceTree = "<group>"; };
+ 56D8811414CE91BD00AB3CE4 /* GRMustacheNumberFormatterHelperTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheNumberFormatterHelperTest.h; sourceTree = "<group>"; };
+ 56D8811514CE91BD00AB3CE4 /* GRMustacheNumberFormatterHelperTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheNumberFormatterHelperTest.m; sourceTree = "<group>"; };
56FDC0A4144AD9B400022D47 /* GRMustacheContextStrategyTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheContextStrategyTest.h; sourceTree = "<group>"; };
56FDC0A5144AD9B400022D47 /* GRMustacheContextStrategyTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheContextStrategyTest.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -372,18 +384,10 @@
isa = PBXGroup;
children = (
565F694B139FF062004D1894 /* Classes */,
- 565F6922139FF016004D1894 /* Supporting Files */,
);
path = GRMustache1;
sourceTree = "<group>";
};
- 565F6922139FF016004D1894 /* Supporting Files */ = {
- isa = PBXGroup;
- children = (
- );
- name = "Supporting Files";
- sourceTree = "<group>";
- };
565F6932139FF017004D1894 /* GRMustache1Tests */ = {
isa = PBXGroup;
children = (
@@ -408,6 +412,7 @@
56E29A5B13BC3B65006336DB /* Loading */,
56E29A5913BC3B50006336DB /* Parsing */,
56E29A5A13BC3B5A006336DB /* Rendering */,
+ 56D8810A14CE916600AB3CE4 /* Utils */,
565F694C139FF062004D1894 /* GRBoolean.h */,
569C86751424A6CE00D6BC77 /* GRBoolean_private.h */,
565F694D139FF062004D1894 /* GRBoolean.m */,
@@ -442,6 +447,7 @@
565F6B56139FF13B004D1894 /* v1.4 */,
565F6B5B139FF13B004D1894 /* v1.5 */,
5672485313E0310D0043E2FA /* v1.7 */,
+ 56D8811114CE91BD00AB3CE4 /* v1.9 */,
);
path = Tests;
sourceTree = SOURCE_ROOT;
@@ -555,6 +561,26 @@
path = v1.7;
sourceTree = "<group>";
};
+ 56D8810A14CE916600AB3CE4 /* Utils */ = {
+ isa = PBXGroup;
+ children = (
+ 56D8810B14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h */,
+ 56D8810C14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m */,
+ );
+ name = Utils;
+ sourceTree = "<group>";
+ };
+ 56D8811114CE91BD00AB3CE4 /* v1.9 */ = {
+ isa = PBXGroup;
+ children = (
+ 56D8811214CE91BD00AB3CE4 /* GRMustacheHelperTest.h */,
+ 56D8811314CE91BD00AB3CE4 /* GRMustacheHelperTest.m */,
+ 56D8811414CE91BD00AB3CE4 /* GRMustacheNumberFormatterHelperTest.h */,
+ 56D8811514CE91BD00AB3CE4 /* GRMustacheNumberFormatterHelperTest.m */,
+ );
+ path = v1.9;
+ sourceTree = "<group>";
+ };
56E29A5713BC3B45006336DB /* Abstract Tree */ = {
isa = PBXGroup;
children = (
@@ -639,6 +665,7 @@
565F6997139FF062004D1894 /* GRMustacheTemplateLoader_protected.h in Headers */,
565F69A2139FF062004D1894 /* GRMustacheVersion.h in Headers */,
563BF73514407C06005A4F58 /* GRMustacheContextStrategy_private.h in Headers */,
+ 56D8810D14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h in Headers */,
565F697C139FF062004D1894 /* GRMustache_private.h in Headers */,
565F697E139FF062004D1894 /* GRMustacheBundleTemplateLoader_private.h in Headers */,
565F6983139FF062004D1894 /* GRMustacheContext_private.h in Headers */,
@@ -676,6 +703,7 @@
566529AF13A12E9600B98B87 /* GRMustacheTemplateLoader.h in Headers */,
566529B013A12E9600B98B87 /* GRMustacheTemplateLoader_protected.h in Headers */,
566529B113A12E9600B98B87 /* GRMustacheVersion.h in Headers */,
+ 56D8810E14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.h in Headers */,
563BF73414407BED005A4F58 /* JRSwizzle.h in Headers */,
563BF73114407BAF005A4F58 /* GRMustacheContextStrategy_private.h in Headers */,
563BF73214407BAF005A4F58 /* GRMustacheNSUndefinedKeyExceptionGuard_private.h in Headers */,
@@ -848,6 +876,7 @@
563BF72114404493005A4F58 /* GRMustacheNSUndefinedKeyExceptionGuard.m in Sources */,
563BF72314404493005A4F58 /* GRMustacheProperty.m in Sources */,
56D585781447F7E300DA3F67 /* GRMustacheContextStrategy.m in Sources */,
+ 56D8810F14CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -878,6 +907,8 @@
5672485C13E0310D0043E2FA /* Model.xcdatamodeld in Sources */,
563BF72D14407AC2005A4F58 /* GRMustachePrivateAPITest.m in Sources */,
56FDC0A6144AD9B400022D47 /* GRMustacheContextStrategyTest.m in Sources */,
+ 56D8811614CE91BD00AB3CE4 /* GRMustacheHelperTest.m in Sources */,
+ 56D8811714CE91BD00AB3CE4 /* GRMustacheNumberFormatterHelperTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -904,6 +935,7 @@
56D585791447F7F400DA3F67 /* GRMustacheNSUndefinedKeyExceptionGuard.m in Sources */,
56D5857A1447F7F800DA3F67 /* GRMustacheProperty.m in Sources */,
560F68E11447FF7E002C50A4 /* JRSwizzle.m in Sources */,
+ 56D8811014CE918600AB3CE4 /* GRMustacheNumberFormatterHelper.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
22 GRMustache1-macosx.xcodeproj/project.pbxproj
@@ -114,6 +114,9 @@
5672484F13E02F430043E2FA /* Model.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 5672484C13E02F430043E2FA /* Model.xcdatamodeld */; };
569C86701424A14F00D6BC77 /* GRBoolean_private.h in Headers */ = {isa = PBXBuildFile; fileRef = 569C866F1424A14E00D6BC77 /* GRBoolean_private.h */; };
56D880F114CDE9DF00AB3CE4 /* GRMustacheHelperTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 56D880F014CDE9DF00AB3CE4 /* GRMustacheHelperTest.m */; };
+ 56D8810114CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = 56D880FF14CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ 56D8810214CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 56D8810014CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.m */; };
+ 56D8810514CE87E000AB3CE4 /* GRMustacheNumberFormatterHelperTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 56D8810414CE87E000AB3CE4 /* GRMustacheNumberFormatterHelperTest.m */; };
56F5A821143EC5CB00478651 /* comments.yml in Resources */ = {isa = PBXBuildFile; fileRef = 56F5A813143EC5CB00478651 /* comments.yml */; };
56F5A823143EC5CB00478651 /* delimiters.yml in Resources */ = {isa = PBXBuildFile; fileRef = 56F5A815143EC5CB00478651 /* delimiters.yml */; };
56F5A825143EC5CB00478651 /* interpolation.yml in Resources */ = {isa = PBXBuildFile; fileRef = 56F5A817143EC5CB00478651 /* interpolation.yml */; };
@@ -277,6 +280,10 @@
569C866F1424A14E00D6BC77 /* GRBoolean_private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRBoolean_private.h; sourceTree = "<group>"; };
56D880EF14CDE9DF00AB3CE4 /* GRMustacheHelperTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheHelperTest.h; sourceTree = "<group>"; };
56D880F014CDE9DF00AB3CE4 /* GRMustacheHelperTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheHelperTest.m; sourceTree = "<group>"; };
+ 56D880FF14CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheNumberFormatterHelper.h; sourceTree = "<group>"; };
+ 56D8810014CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheNumberFormatterHelper.m; sourceTree = "<group>"; };
+ 56D8810314CE87E000AB3CE4 /* GRMustacheNumberFormatterHelperTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheNumberFormatterHelperTest.h; sourceTree = "<group>"; };
+ 56D8810414CE87E000AB3CE4 /* GRMustacheNumberFormatterHelperTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheNumberFormatterHelperTest.m; sourceTree = "<group>"; };
56F5A813143EC5CB00478651 /* comments.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = comments.yml; sourceTree = "<group>"; };
56F5A815143EC5CB00478651 /* delimiters.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = delimiters.yml; sourceTree = "<group>"; };
56F5A817143EC5CB00478651 /* interpolation.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = interpolation.yml; sourceTree = "<group>"; };
@@ -427,6 +434,7 @@
56E29A6413BC3C62006336DB /* Loading */,
56E29A6513BC3C6C006336DB /* Parsing */,
56E29A6613BC3C76006336DB /* Rendering */,
+ 56D880FE14CE85BE00AB3CE4 /* Utils */,
565F694C139FF062004D1894 /* GRBoolean.h */,
569C866F1424A14E00D6BC77 /* GRBoolean_private.h */,
565F694D139FF062004D1894 /* GRBoolean.m */,
@@ -593,10 +601,21 @@
children = (
56D880EF14CDE9DF00AB3CE4 /* GRMustacheHelperTest.h */,
56D880F014CDE9DF00AB3CE4 /* GRMustacheHelperTest.m */,
+ 56D8810314CE87E000AB3CE4 /* GRMustacheNumberFormatterHelperTest.h */,
+ 56D8810414CE87E000AB3CE4 /* GRMustacheNumberFormatterHelperTest.m */,
);
path = v1.9;
sourceTree = "<group>";
};
+ 56D880FE14CE85BE00AB3CE4 /* Utils */ = {
+ isa = PBXGroup;
+ children = (
+ 56D880FF14CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.h */,
+ 56D8810014CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.m */,
+ );
+ name = Utils;
+ sourceTree = "<group>";
+ };
56E29A6213BC3C59006336DB /* Abstract Tree */ = {
isa = PBXGroup;
children = (
@@ -695,6 +714,7 @@
565F6997139FF062004D1894 /* GRMustacheTemplateLoader_protected.h in Headers */,
565F69A2139FF062004D1894 /* GRMustacheVersion.h in Headers */,
565F697C139FF062004D1894 /* GRMustache_private.h in Headers */,
+ 56D8810114CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.h in Headers */,
565F697E139FF062004D1894 /* GRMustacheBundleTemplateLoader_private.h in Headers */,
565F6983139FF062004D1894 /* GRMustacheContext_private.h in Headers */,
565F6985139FF062004D1894 /* GRMustacheDirectoryTemplateLoader_private.h in Headers */,
@@ -858,6 +878,7 @@
563BF6CB14403D06005A4F58 /* GRMustacheProperty.m in Sources */,
563BF6CF14403FE1005A4F58 /* GRMustacheNSUndefinedKeyExceptionGuard.m in Sources */,
563BF72714404BF1005A4F58 /* GRMustacheContextStrategy.m in Sources */,
+ 56D8810214CE85E600AB3CE4 /* GRMustacheNumberFormatterHelper.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -898,6 +919,7 @@
563BF74D1440A381005A4F58 /* GRMustacheDefaultTemplateOptionsTest.m in Sources */,
56FDC09F144AD5D200022D47 /* GRMustacheContextStrategyTest.m in Sources */,
56D880F114CDE9DF00AB3CE4 /* GRMustacheHelperTest.m in Sources */,
+ 56D8810514CE87E000AB3CE4 /* GRMustacheNumberFormatterHelperTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
12 RELEASE_NOTES.md
@@ -3,14 +3,16 @@ GRMustache Release Notes
## v1.9
-**Better lambda encapsulation with classes conforming to the GRMustacheHelper protocol.**
+- **Better lambda encapsulation with classes conforming to the GRMustacheHelper protocol.**
+- **Format all numbers in a section with GRMustacheNumberFormatterHelper**
New protocol:
- @protocol GRMustacheHelper<NSObject>
- @required
- - (NSString *)renderSection:(GRMustacheSection *)section withContext:(id)context
- @end
+- `GRMustacheHelper`
+
+New class:
+
+- `GRMustacheNumberFormatterHelper`
## v1.8.6
View
4 Tests/v1.9/GRMustacheHelperTest.h
@@ -20,8 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-#define GRMUSTACHE_VERSION_MIN_REQUIRED GRMUSTACHE_VERSION_1_8
-#define GRMUSTACHE_VERSION_MAX_REQUIRED GRMUSTACHE_VERSION_1_8
+#define GRMUSTACHE_VERSION_MIN_REQUIRED GRMUSTACHE_VERSION_1_9
+#define GRMUSTACHE_VERSION_MAX_REQUIRED GRMUSTACHE_VERSION_1_9
#import "GRMustachePublicAPITest.h"
@interface GRMustacheHelperTest : GRMustachePublicAPITest
View
2  Tests/v1.9/GRMustacheHelperTest.m
@@ -34,7 +34,7 @@ - (NSString *)renderSection:(GRMustacheSection *)section withContext:(id)context
@implementation GRMustacheHelperTest
-- (void)testDefaultTemplateOptions
+- (void)testRenderSectionWithContextMethodPerformsRendering
{
NSString *templateString = @"{{#foobar}}---{{/foobar}}";
NSDictionary *context = [NSDictionary dictionaryWithObject:[[[GRMustacheFoobarHelper alloc] init] autorelease] forKey:@"foobar"];
View
29 Tests/v1.9/GRMustacheNumberFormatterHelperTest.h
@@ -0,0 +1,29 @@
+// The MIT License
+//
+// Copyright (c) 2010 Gwendal Roué
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#define GRMUSTACHE_VERSION_MIN_REQUIRED GRMUSTACHE_VERSION_1_9
+#define GRMUSTACHE_VERSION_MAX_REQUIRED GRMUSTACHE_VERSION_1_9
+#import "GRMustachePublicAPITest.h"
+
+@interface GRMustacheNumberFormatterHelperTest : GRMustachePublicAPITest
+
+@end
View
59 Tests/v1.9/GRMustacheNumberFormatterHelperTest.m
@@ -0,0 +1,59 @@
+// The MIT License
+//
+// Copyright (c) 2010 Gwendal Roué
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import "GRMustacheNumberFormatterHelperTest.h"
+
+@implementation GRMustacheNumberFormatterHelperTest
+
+- (void)testNumberFormatting
+{
+ NSString *templateString = @"{{#format}}{{value}}{{/format}}";
+ GRMustacheTemplate *template = [GRMustacheTemplate parseString:templateString error:NULL];
+
+ NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
+ numberFormatter.numberStyle = kCFNumberFormatterScientificStyle;
+ NSDictionary *helper = [NSDictionary dictionaryWithObject:[GRMustacheNumberFormatterHelper helperWithNumberFormatter:numberFormatter] forKey:@"format"];
+
+ NSDictionary *data = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.5] forKey:@"value"];
+ NSString *result = [template renderObjects:helper, data, nil];
+ STAssertEqualObjects(result, @"5E-1", nil);
+}
+
+- (void)testBooleansAreNotAffected
+{
+ NSString *templateString = @"{{#format}}{{value}}{{/format}}";
+ GRMustacheTemplate *template = [GRMustacheTemplate parseString:templateString error:NULL];
+
+ NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
+ numberFormatter.numberStyle = kCFNumberFormatterScientificStyle;
+ NSDictionary *helper = [NSDictionary dictionaryWithObject:[GRMustacheNumberFormatterHelper helperWithNumberFormatter:numberFormatter] forKey:@"format"];
+
+ NSDictionary *data = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"value"];
+ NSString *result = [template renderObjects:helper, data, nil];
+ STAssertEqualObjects(result, @"1", nil);
+
+ data = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"value"];
+ result = [template renderObjects:helper, data, nil];
+ STAssertEqualObjects(result, @"", nil);
+}
+
+@end
View
1  include/GRMustache.h
@@ -165,3 +165,4 @@ typedef struct {
#import "GRMustacheEnvironment.h"
#import "GRMustacheError.h"
#import "GRMustacheVersion.h"
+#import "GRMustacheNumberFormatterHelper.h"
View
33 include/GRMustacheNumberFormatterHelper.h
@@ -0,0 +1,33 @@
+// The MIT License
+//
+// Copyright (c) 2010 Gwendal Roué
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#import <Foundation/Foundation.h>
+#import "GRMustacheAvailabilityMacros.h"
+#import "GRMustacheLambda.h"
+
+@interface GRMustacheNumberFormatterHelper : NSObject<GRMustacheHelper> {
+@private
+ NSNumberFormatter *numberFormatter;
+}
+@property (nonatomic, readonly, retain) NSNumberFormatter *numberFormatter AVAILABLE_GRMUSTACHE_VERSION_1_9_AND_LATER;
++ (id)helperWithNumberFormatter:(NSNumberFormatter *)numberFormatter AVAILABLE_GRMUSTACHE_VERSION_1_9_AND_LATER;
+@end
View
BIN  lib/libGRMustache1-ios3.a
Binary file not shown
View
BIN  lib/libGRMustache1-ios4.a
Binary file not shown
View
BIN  lib/libGRMustache1-macosx10.6.a
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.