Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 4 commits
  • 5 files changed
  • 0 comments
  • 1 contributor
20 Merlin.xcodeproj/project.pbxproj
@@ -356,6 +356,10 @@
356 356 isa = XCBuildConfiguration;
357 357 buildSettings = {
358 358 ALWAYS_SEARCH_USER_PATHS = NO;
  359 + "ARCHS[sdk=iphoneos*]" = (
  360 + armv6,
  361 + armv7,
  362 + );
359 363 DSTROOT = /tmp/Merlin.dst;
360 364 GCC_PRECOMPILE_PREFIX_HEADER = YES;
361 365 GCC_PREFIX_HEADER = "Merlin/Merlin-Prefix.pch";
@@ -373,6 +377,10 @@
373 377 isa = XCBuildConfiguration;
374 378 buildSettings = {
375 379 ALWAYS_SEARCH_USER_PATHS = NO;
  380 + "ARCHS[sdk=iphoneos*]" = (
  381 + armv6,
  382 + armv7,
  383 + );
376 384 DSTROOT = /tmp/Merlin.dst;
377 385 GCC_PRECOMPILE_PREFIX_HEADER = YES;
378 386 GCC_PREFIX_HEADER = "Merlin/Merlin-Prefix.pch";
@@ -450,6 +458,10 @@
450 458 isa = XCBuildConfiguration;
451 459 buildSettings = {
452 460 ALWAYS_SEARCH_USER_PATHS = NO;
  461 + "ARCHS[sdk=iphoneos*]" = (
  462 + armv6,
  463 + armv7,
  464 + );
453 465 DSTROOT = /tmp/Merlin.dst;
454 466 GCC_PRECOMPILE_PREFIX_HEADER = YES;
455 467 GCC_PREFIX_HEADER = "Merlin/Merlin-Prefix.pch";
@@ -509,6 +521,10 @@
509 521 armv6,
510 522 armv7,
511 523 );
  524 + "ARCHS[sdk=iphoneos*]" = (
  525 + armv6,
  526 + armv7,
  527 + );
512 528 DSTROOT = /tmp/Merlin.dst;
513 529 GCC_PRECOMPILE_PREFIX_HEADER = YES;
514 530 GCC_PREFIX_HEADER = "Merlin/Merlin-Prefix.pch";
@@ -564,6 +580,10 @@
564 580 isa = XCBuildConfiguration;
565 581 buildSettings = {
566 582 ALWAYS_SEARCH_USER_PATHS = NO;
  583 + "ARCHS[sdk=iphoneos*]" = (
  584 + armv6,
  585 + armv7,
  586 + );
567 587 DSTROOT = /tmp/Merlin.dst;
568 588 GCC_PRECOMPILE_PREFIX_HEADER = YES;
569 589 GCC_PREFIX_HEADER = "Merlin/Merlin-Prefix.pch";
10 Merlin/MLBase.h
@@ -5,6 +5,11 @@
5 5
6 6 #import <Foundation/Foundation.h>
7 7
  8 +typedef enum {
  9 + MLNamingStyleCamelCase = 0,
  10 + MLNamingStyleSnakeCase
  11 +} MLNamingStyle;
  12 +
8 13 @class MLDatabase;
9 14
10 15 /*!
@@ -23,6 +28,9 @@
23 28 */
24 29 + (void)setReturnsNilForNull:(BOOL)returnNil;
25 30
  31 ++ (MLNamingStyle)namingStyle;
  32 ++ (void)setNamingStyle:(MLNamingStyle)newStyle;
  33 +
26 34 /*!
27 35 @methodgroup Configuration and setup
28 36 */
@@ -49,6 +57,8 @@
49 57 @methodgroup Finders
50 58 */
51 59
  60 ++ (void)fetchObjectsWithQuery:(NSString *)queryString withBlock:(void (^)(MLBase *obj))block;
  61 +
52 62 /*!
53 63 @method first
54 64 @abstract Finds the first record in the database, sorted by id.
67 Merlin/MLBase.m
@@ -28,7 +28,6 @@ @interface MLBase()
28 28
29 29 + (MLDatabase *)database;
30 30 + (void)evaluateQuery:(NSString *)queryString withBlock:(void (^)(NSDictionary *attributes))block;
31   -+ (void)fetchObjectsWithQuery:(NSString *)queryString withBlock:(void (^)(MLBase *obj))block;
32 31 + (void)injectColumnProperties:(NSArray *)columns;
33 32
34 33 - (BOOL)createOrUpdate;
@@ -50,6 +49,7 @@ @implementation MLBase
50 49 // Hash to map class names to their respective databases
51 50 static NSMutableDictionary *databaseMapping = nil;
52 51 static BOOL returnNilForNull = NO;
  52 +static MLNamingStyle namingStyle = MLNamingStyleCamelCase;
53 53
54 54 + (void)initialize
55 55 {
@@ -64,6 +64,16 @@ + (void)setReturnsNilForNull:(BOOL)returnNil
64 64 returnNilForNull = returnNil;
65 65 }
66 66
  67 ++ (MLNamingStyle)namingStyle
  68 +{
  69 + return namingStyle;
  70 +}
  71 +
  72 ++ (void)setNamingStyle:(MLNamingStyle)newStyle
  73 +{
  74 + namingStyle = newStyle;
  75 +}
  76 +
67 77 + (void)setDatabase:(MLDatabase *)aDatabase
68 78 {
69 79 NSString *className = NSStringFromClass(self);
@@ -87,8 +97,17 @@ + (MLDatabase *)database
87 97
88 98 + (NSString *)tableName
89 99 {
90   - NSString *className = [NSStringFromClass([self class]) lowerCamelCaseString];
91   - return [className stringByAppendingString:@"s"];
  100 + // TODO: Support proper inflection
  101 + NSString *tableName = [[NSStringFromClass([self class]) lowerCamelCaseString] stringByAppendingString:@"s"];
  102 +
  103 + if ([self namingStyle] == MLNamingStyleSnakeCase)
  104 + {
  105 + return [tableName underscoredString];
  106 + }
  107 + else
  108 + {
  109 + return tableName;
  110 + }
92 111 }
93 112
94 113 // TODO: Better way to keep track of columns per class?
@@ -172,6 +191,12 @@ + (NSArray *)columns
172 191 id getSQLiteAttributeIMP(MLBase *self, SEL _cmd)
173 192 {
174 193 NSString *getterName = NSStringFromSelector(_cmd);
  194 +
  195 + if ([[self class] namingStyle] == MLNamingStyleSnakeCase)
  196 + {
  197 + getterName = [getterName underscoredString];
  198 + }
  199 +
175 200 id value = [self->_attributes valueForKey:getterName];
176 201
177 202 return (returnNilForNull && value == [NSNull null]) ? nil : value;
@@ -202,6 +227,11 @@ void setSQLiteAttributeIMP(MLBase *self, SEL _cmd, id newValue)
202 227 keyName = [keyName stringByReplacingOccurrencesOfString:@":" withString:@""];
203 228 }
204 229
  230 + if ([[self class] namingStyle] == MLNamingStyleSnakeCase)
  231 + {
  232 + keyName = [keyName underscoredString];
  233 + }
  234 +
205 235 // We can't put nil into a dictionary
206 236 if (newValue == nil)
207 237 {
@@ -217,10 +247,10 @@ + (void)injectColumnProperties:(NSArray *)columns
217 247 {
218 248 for (MLColumn *column in columns)
219 249 {
220   - NSString *getterName = column.name;
  250 + NSString *getterName = [column.name lowerCamelCaseString];
221 251 class_addMethod(self, NSSelectorFromString(getterName), (IMP)&getSQLiteAttributeIMP, "@@:");
222 252
223   - NSString *capitalizedColumnName = [[[column.name substringToIndex:1] uppercaseString] stringByAppendingString:[column.name substringFromIndex:1]];
  253 + NSString *capitalizedColumnName = [[[getterName substringToIndex:1] uppercaseString] stringByAppendingString:[getterName substringFromIndex:1]];
224 254 NSString *setterName = [NSString stringWithFormat:@"set%@:", capitalizedColumnName];
225 255 class_addMethod(self, NSSelectorFromString(setterName), (IMP)&setSQLiteAttributeIMP, "v@:@");
226 256 }
@@ -449,6 +479,12 @@ - (BOOL)update
449 479 for (int i = 0; i < changedColumns.count; ++i)
450 480 {
451 481 NSString *changedColumnName = [changedColumns objectAtIndex:i];
  482 +
  483 + if ([[self class] namingStyle] == MLNamingStyleSnakeCase)
  484 + {
  485 + changedColumnName = [changedColumnName underscoredString];
  486 + }
  487 +
452 488 id value = [_changedAttributes valueForKey:changedColumnName];
453 489
454 490 if (![value isKindOfClass:[NSString class]])
@@ -483,8 +519,25 @@ - (BOOL)create
483 519 [[self class] tableName]];
484 520
485 521 NSArray *columns = [[self class] columns];
486   - NSString *columnNames = [[columns valueForKey:@"name"] componentsJoinedByString:@","];
487   - [insertQueryString appendFormat:@"%@) VALUES(", columnNames];
  522 + NSArray *columnNames = [columns valueForKey:@"name"];
  523 + for (NSInteger i = 0; i < columnNames.count; i++)
  524 + {
  525 + NSString *columnName = [columnNames objectAtIndex:i];
  526 +
  527 + if ([[self class] namingStyle] == MLNamingStyleSnakeCase)
  528 + {
  529 + columnName = [columnName underscoredString];
  530 + }
  531 +
  532 + [insertQueryString appendString:columnName];
  533 +
  534 + if (i < columnNames.count - 1)
  535 + {
  536 + [insertQueryString appendString:@","];
  537 + }
  538 + }
  539 +
  540 + [insertQueryString appendString:@") VALUES("];
488 541
489 542 for (int i = 0; i < columns.count; ++i)
490 543 {
2  Merlin/NSString+MerlinAdditions.h
@@ -17,4 +17,6 @@
17 17 */
18 18 - (NSString *)lowerCamelCaseString;
19 19
  20 +- (NSString *)underscoredString;
  21 +
20 22 @end
44 Merlin/NSString+MerlinAdditions.m
@@ -19,7 +19,49 @@ - (NSString *)lowerCamelCaseString
19 19
20 20 NSString *firstLetter = [[self substringToIndex:1] lowercaseString];
21 21 NSString *remainder = [self substringFromIndex:1];
22   - return [firstLetter stringByAppendingString:remainder];
  22 +
  23 + NSMutableString *resultStr = [NSMutableString stringWithFormat:@"%@%@", firstLetter, remainder];
  24 +
  25 + NSRegularExpression *charactersToConvertRegEx = [NSRegularExpression regularExpressionWithPattern:@"([_-])([a-zA-Z\\d])"
  26 + options:0
  27 + error:NULL];
  28 +
  29 + __block NSInteger rangeOffset = 0;
  30 + [charactersToConvertRegEx enumerateMatchesInString:resultStr
  31 + options:0
  32 + range:NSMakeRange(0, resultStr.length)
  33 + usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
  34 + {
  35 + NSRange overallRange = [result rangeAtIndex:0];
  36 + overallRange.location -= rangeOffset;
  37 +
  38 + NSRange characterToCapitalizeRange = [result rangeAtIndex:2];
  39 + characterToCapitalizeRange.location -= rangeOffset;
  40 +
  41 + NSString *characterToCapitalize = [[resultStr substringWithRange:characterToCapitalizeRange] uppercaseString];
  42 + [resultStr replaceCharactersInRange:overallRange withString:characterToCapitalize];
  43 +
  44 + rangeOffset += characterToCapitalizeRange.length;
  45 + }];
  46 +
  47 + return resultStr;
  48 +}
  49 +
  50 +- (NSString *)underscoredString
  51 +{
  52 + NSRegularExpression *doubleUppercaseRegEx = [NSRegularExpression regularExpressionWithPattern:@"([A-Z]+)([A-Z][a-z])"
  53 + options:0
  54 + error:NULL];
  55 + NSRegularExpression *uppercaseRegEx = [NSRegularExpression regularExpressionWithPattern:@"([a-z\\d])([A-Z])"
  56 + options:0
  57 + error:NULL];
  58 +
  59 + NSString *result = nil;
  60 +
  61 + result = [doubleUppercaseRegEx stringByReplacingMatchesInString:self options:0 range:NSMakeRange(0, self.length) withTemplate:@"$1_$2"];
  62 + result = [uppercaseRegEx stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, result.length) withTemplate:@"$1_$2"];
  63 +
  64 + return [result lowercaseString];
23 65 }
24 66
25 67 @end

No commit comments for this range

Something went wrong with that request. Please try again.