Permalink
Browse files

Added Properties class to manage the JSON manifest.

At the moment, the class contains defaults but does not load an external manifest file yet.
  • Loading branch information...
1 parent 69defcf commit d9ad5e28deb385a88e088b7782164e281a91a928 @Simbul Simbul committed Jul 11, 2011
Showing with 333 additions and 0 deletions.
  1. +14 −0 Baker.xcodeproj/project.pbxproj
  2. +45 −0 Classes/Properties.h
  3. +121 −0 Classes/Properties.m
  4. +43 −0 PropertiesTest.h
  5. +110 −0 PropertiesTest.m
View
14 Baker.xcodeproj/project.pbxproj
@@ -45,6 +45,9 @@
BFF823C9138055F600562F57 /* CJSONScanner.m in Sources */ = {isa = PBXBuildFile; fileRef = BFF823B4138055F600562F57 /* CJSONScanner.m */; };
BFF823CA138055F600562F57 /* CJSONSerializer.m in Sources */ = {isa = PBXBuildFile; fileRef = BFF823B6138055F600562F57 /* CJSONSerializer.m */; };
BFF823CB138055F600562F57 /* CJSONSerializer.m in Sources */ = {isa = PBXBuildFile; fileRef = BFF823B6138055F600562F57 /* CJSONSerializer.m */; };
+ D20915C013CB9C7800D3B55B /* Properties.m in Sources */ = {isa = PBXBuildFile; fileRef = D20915BF13CB9C7800D3B55B /* Properties.m */; };
+ D20915C413CB9FDC00D3B55B /* PropertiesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D20915C313CB9FDC00D3B55B /* PropertiesTest.m */; };
+ D20915C513CBA1C600D3B55B /* Properties.m in Sources */ = {isa = PBXBuildFile; fileRef = D20915BF13CB9C7800D3B55B /* Properties.m */; };
D21A0B86135CF1F2005D6E8A /* libOCMock.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D21A0B85135CF1F2005D6E8A /* libOCMock.a */; };
D2700BD81355BC3100889DD8 /* OCMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D2700BD71355BC3100889DD8 /* OCMock.framework */; };
D2700BDB1356048800889DD8 /* OCMock.framework in Copy OCMock */ = {isa = PBXBuildFile; fileRef = D2700BD71355BC3100889DD8 /* OCMock.framework */; };
@@ -124,6 +127,10 @@
BFF823B5138055F600562F57 /* CJSONSerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CJSONSerializer.h; sourceTree = "<group>"; };
BFF823B6138055F600562F57 /* CJSONSerializer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CJSONSerializer.m; sourceTree = "<group>"; };
BFF823B7138055F600562F57 /* JSONRepresentation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONRepresentation.h; sourceTree = "<group>"; };
+ D20915BE13CB9C7800D3B55B /* Properties.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Properties.h; sourceTree = "<group>"; };
+ D20915BF13CB9C7800D3B55B /* Properties.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Properties.m; sourceTree = "<group>"; };
+ D20915C213CB9FDC00D3B55B /* PropertiesTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PropertiesTest.h; sourceTree = "<group>"; };
+ D20915C313CB9FDC00D3B55B /* PropertiesTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PropertiesTest.m; sourceTree = "<group>"; };
D21A0B85135CF1F2005D6E8A /* libOCMock.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libOCMock.a; sourceTree = "<group>"; };
D2700BD71355BC3100889DD8 /* OCMock.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = OCMock.framework; sourceTree = "<group>"; };
D281D02D13546BB1009F1B11 /* InterceptorWindowTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InterceptorWindowTests.h; sourceTree = "<group>"; };
@@ -176,6 +183,8 @@
D2A198A91375972600C6BE2F /* IndexViewController.m */,
9F4F07F111F8DE8200375A8C /* RootViewController.h */,
9F4F07F211F8DE8200375A8C /* RootViewController.m */,
+ D20915BE13CB9C7800D3B55B /* Properties.h */,
+ D20915BF13CB9C7800D3B55B /* Properties.m */,
1D3623240D0F684500981E51 /* BakerAppDelegate.h */,
1D3623250D0F684500981E51 /* BakerAppDelegate.m */,
F4EC203612862D4D008E94D1 /* InterceptorWindow.h */,
@@ -325,6 +334,8 @@
D2700BD61355BC0800889DD8 /* Frameworks */,
D281D02D13546BB1009F1B11 /* InterceptorWindowTests.h */,
D281D02E13546BB1009F1B11 /* InterceptorWindowTests.m */,
+ D20915C213CB9FDC00D3B55B /* PropertiesTest.h */,
+ D20915C313CB9FDC00D3B55B /* PropertiesTest.m */,
);
name = Tests;
sourceTree = "<group>";
@@ -457,6 +468,7 @@
BFF823C6138055F600562F57 /* CJSONDeserializer.m in Sources */,
BFF823C8138055F600562F57 /* CJSONScanner.m in Sources */,
BFF823CA138055F600562F57 /* CJSONSerializer.m in Sources */,
+ D20915C013CB9C7800D3B55B /* Properties.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -476,6 +488,8 @@
BFF823C7138055F600562F57 /* CJSONDeserializer.m in Sources */,
BFF823C9138055F600562F57 /* CJSONScanner.m in Sources */,
BFF823CB138055F600562F57 /* CJSONSerializer.m in Sources */,
+ D20915C413CB9FDC00D3B55B /* PropertiesTest.m in Sources */,
+ D20915C513CBA1C600D3B55B /* Properties.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
45 Classes/Properties.h
@@ -0,0 +1,45 @@
+//
+// Properties.h
+// Baker
+//
+// ==========================================================================================
+//
+// Copyright (c) 2011, Davide Casali, Marco Colombo, Alessandro Morandi
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+// Redistributions in binary form must reproduce the above copyright notice, this list of
+// conditions and the following disclaimer in the documentation and/or other materials
+// provided with the distribution.
+// Neither the name of the Baker Framework nor the names of its contributors may be used to
+// endorse or promote products derived from this software without specific prior written
+// permission.
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface Properties : NSObject {
+ NSDictionary *manifest;
+ NSDictionary *defaults;
+}
+
+- (id)get:(NSString *)rootName, ...;
+- (id)getFrom:(NSDictionary *)dictionary withKeys:(NSArray *)keys;
+- (id)getFrom:(NSDictionary *)dictionary withFallback:(NSDictionary *)fallbackDictionary withKeys:(NSArray *)keys;
+- (NSDictionary *)initDefaults;
+
+@end
View
121 Classes/Properties.m
@@ -0,0 +1,121 @@
+//
+// Properties.m
+// Baker
+//
+// ==========================================================================================
+//
+// Copyright (c) 2011, Davide Casali, Marco Colombo, Alessandro Morandi
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+// Redistributions in binary form must reproduce the above copyright notice, this list of
+// conditions and the following disclaimer in the documentation and/or other materials
+// provided with the distribution.
+// Neither the name of the Baker Framework nor the names of its contributors may be used to
+// endorse or promote products derived from this software without specific prior written
+// permission.
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import "Properties.h"
+#import "NSDictionary_JSONExtensions.h"
+
+
+@implementation Properties
+
+- (id)init {
+ self = [super init];
+ if (self) {
+ manifest = [[NSDictionary alloc] init];
+ defaults = [self initDefaults];
+ }
+ return self;
+}
+
+/**
+ Get the value for the specified property.
+ E.g. with a JSON like this:
+ {
+ "prop1": "value1",
+ "nest":{
+ "prop2": "value2"
+ }
+ }
+ Some calls could be:
+ [get @"prop1", nil] // returns "value1"
+ [get @"nest", "prop2", nil] // returns "value2"
+
+ Remember to end the list of parameters with nil.
+ */
+- (id)get:(NSString *)rootName, ... {
+ NSMutableArray *keys = [[NSMutableArray alloc] init];
+ va_list args;
+ va_start(args, rootName);
+ for (NSString *arg = rootName; arg != nil; arg = va_arg(args, NSString*)) {
+ if ([arg isKindOfClass:[NSString class]]) {
+ [keys addObject:arg];
+ }
+ }
+ va_end(args);
+
+
+ return [self getFrom:manifest withFallback:defaults withKeys:keys];
+}
+
+- (id)getFrom:(NSDictionary *)dictionary withFallback:(NSDictionary *)fallbackDictionary withKeys:(NSArray *)keys {
+ id rootObj = [dictionary objectForKey:[keys objectAtIndex:0]];
+ if (rootObj == nil) {
+ return [self getFrom:fallbackDictionary withKeys:keys];
+ } else {
+ if ([rootObj isKindOfClass:[NSDictionary class]] ) {
+ NSRange range = NSMakeRange(1, [keys count] - 1);
+ return [self getFrom:rootObj withFallback:defaults withKeys:[keys subarrayWithRange:range]];
+ } else {
+ return rootObj;
+ }
+ }
+}
+
+- (id)getFrom:(NSDictionary *)dictionary withKeys:(NSArray *)keys {
+ id rootObj = [dictionary objectForKey:[keys objectAtIndex:0]];
+ if ([rootObj isKindOfClass:[NSDictionary class]] ) {
+ NSRange range = NSMakeRange(1, [keys count] - 1);
+ return [self getFrom:rootObj withKeys:[keys subarrayWithRange:range]];
+ } else {
+ return rootObj;
+ }
+}
+
+- (NSDictionary *)initDefaults {
+ NSString *json = @"{"
+ "\"properties\": {"
+ "\"orientation\": \"both\","
+ "\"pinchToZoom\": false"
+ "},"
+ "\"x-baker\": {"
+ "\"background\": \"#000000\","
+ "\"verticalBounce\": true,"
+ "\"indexHeight\": 150,"
+ "\"mediaAutoPlay\": true"
+ "}"
+ "}";
+ NSError *e = NULL;
+ NSDictionary *ret;
+
+ ret = [NSDictionary dictionaryWithJSONString:json error:&e];
+ return ret;
+}
+
+@end
View
43 PropertiesTest.h
@@ -0,0 +1,43 @@
+//
+// PropertiesTest.h
+// Baker
+//
+// ==========================================================================================
+//
+// Copyright (c) 2011, Davide Casali, Marco Colombo, Alessandro Morandi
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+// Redistributions in binary form must reproduce the above copyright notice, this list of
+// conditions and the following disclaimer in the documentation and/or other materials
+// provided with the distribution.
+// Neither the name of the Baker Framework nor the names of its contributors may be used to
+// endorse or promote products derived from this software without specific prior written
+// permission.
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+// Application unit tests contain unit test code that must be injected into an application to run correctly.
+// Define USE_APPLICATION_UNIT_TEST to 0 if the unit test code is designed to be linked into an independent test executable.
+
+#import <SenTestingKit/SenTestingKit.h>
+#import <UIKit/UIKit.h>
+
+
+@interface PropertiesTest : SenTestCase {
+
+}
+
+@end
View
110 PropertiesTest.m
@@ -0,0 +1,110 @@
+//
+// PropertiesTest.m
+// Baker
+//
+// ==========================================================================================
+//
+// Copyright (c) 2011, Davide Casali, Marco Colombo, Alessandro Morandi
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+// Redistributions in binary form must reproduce the above copyright notice, this list of
+// conditions and the following disclaimer in the documentation and/or other materials
+// provided with the distribution.
+// Neither the name of the Baker Framework nor the names of its contributors may be used to
+// endorse or promote products derived from this software without specific prior written
+// permission.
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import "PropertiesTest.h"
+#import "Properties.h"
+
+
+@implementation PropertiesTest
+
+- (void)testGetMultiArgs {
+
+ Properties *properties = [[Properties alloc]init];
+ id property = [properties get:@"one", @"two", @"three", nil];
+ STAssertNil(property, @"Should not find non existing property");
+
+}
+
+- (void)testFallbackFrom {
+ NSArray *objects = [NSArray arrayWithObjects:@"blah", nil];
+ NSArray *keys = [NSArray arrayWithObjects:@"string", nil];
+ NSDictionary *fbDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
+ NSDictionary *dict = [[NSDictionary alloc] init];
+
+ Properties *properties = [[Properties alloc]init];
+ id property = [properties getFrom:dict withFallback:fbDict withKeys:keys];
+ STAssertEquals(property, @"blah", @"Should return the fallback item");
+}
+
+- (void)testGetSimpleTypesFrom {
+ NSArray *objects = [NSArray arrayWithObjects:
+ @"blah",
+ [[NSNumber alloc] initWithInteger:1],
+ [NSNumber numberWithBool:YES],
+ nil];
+ NSArray *keys = [NSArray arrayWithObjects:
+ @"string",
+ @"number",
+ @"boolean",
+ nil];
+ NSDictionary *dict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
+
+ Properties *properties = [[Properties alloc]init];
+ id property;
+ NSArray *queryKeys;
+
+ queryKeys = [NSArray arrayWithObjects:@"string", nil];
+ property = [properties getFrom:dict withKeys:queryKeys];
+ STAssertEqualObjects(property, @"blah", @"Should return a string");
+
+ queryKeys = [NSArray arrayWithObjects:@"number", nil];
+ property = [properties getFrom:dict withKeys:queryKeys];
+ STAssertEqualObjects(property, [[NSNumber alloc] initWithInteger:1], @"Should return a number");
+
+ queryKeys = [NSArray arrayWithObjects:@"boolean", nil];
+ property = [properties getFrom:dict withKeys:queryKeys];
+ STAssertEquals([property boolValue], YES, @"Should return an int corresponding to a boolean");
+}
+
+- (void)testDefaults {
+ Properties *properties = [[Properties alloc]init];
+ id property;
+
+ property = [properties get:@"properties", @"orientation", nil];
+ STAssertEqualObjects(property, @"both", @"Should return expected default value for orientation");
+
+ property = [properties get:@"properties", @"pinchToZoom", nil];
+ STAssertEquals([property boolValue], NO, @"Should return expected default value for pinchToZoom");
+
+ property = [properties get:@"x-baker", @"background", nil];
+ STAssertEqualObjects(property, @"#000000", @"Should return expected default value for background");
+
+ property = [properties get:@"x-baker", @"verticalBounce", nil];
+ STAssertEquals([property boolValue], YES, @"Should return expected default value for verticalBounce");
+
+ property = [properties get:@"x-baker", @"indexHeight", nil];
+ STAssertEqualObjects(property, [[NSNumber alloc] initWithInteger:150], @"Should return expected default value for indexHeight");
+
+ property = [properties get:@"x-baker", @"mediaAutoPlay", nil];
+ STAssertEquals([property boolValue], YES, @"Should return expected default value for mediaAutoPlay");
+}
+
+@end

0 comments on commit d9ad5e2

Please sign in to comment.