-
Notifications
You must be signed in to change notification settings - Fork 9
/
ActiveSupport.m
119 lines (94 loc) · 4.01 KB
/
ActiveSupport.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// ActiveSupport.m
// Shopify_Mobile
//
// Created by Matthew Newberry on 8/2/10.
// Copyright 2010 Shopify. All rights reserved.
//
#import "ActiveSupport.h"
@implementation ActiveSupport
+ (NSArray*) sortDescriptorsFromString:(NSString*)string {
NSMutableArray* sortDescriptors = nil;
NSArray* sortChunks = [string componentsSeparatedByString:@" "];
if ([sortChunks count] % 2 == 0) {
sortDescriptors = [NSMutableArray arrayWithCapacity:[sortChunks count] / 2];
for (int chunkIdx = 0; chunkIdx < [sortChunks count]; chunkIdx += 2) {
[sortDescriptors addObject:
[[[NSSortDescriptor alloc] initWithKey:[sortChunks objectAtIndex:chunkIdx] ascending:
[[sortChunks objectAtIndex:chunkIdx + 1] caseInsensitiveCompare:@"asc"] == NSOrderedSame] autorelease]];
}
}
return sortDescriptors;
}
+ (NSArray*) sortDescriptorsFromParameters:(id)parameters {
if ([parameters isKindOfClass:[NSString class]])
return [self sortDescriptorsFromString:parameters];
if ([parameters isKindOfClass:[NSDictionary class]])
return [self sortDescriptorsFromParameters:[parameters objectForKey:@"$sort"]];
else if ([parameters isKindOfClass:[NSArray class]])
return parameters;
return nil;
}
+ (NSURL*) URLWithSite:(NSString*)site andFormat:(NSString*)format andParameters:(id)parameters {
// Build query parameter string from supplied parameters
NSMutableString *str = [NSMutableString stringWithString:site];
// Add in format if extant
if (format != nil) {
[str appendString:@"."];
[str appendString:format];
}
if (parameters != nil) {
[str appendString:@"?"];
// If parameters are just a string, add in directly
if ([parameters isKindOfClass:[NSString class]])
[str appendString:parameters];
// If parameters are a dictionary, iterate and add each pair
else if ([parameters isKindOfClass:[NSDictionary class]]) {
BOOL first = YES;
for (NSString *key in [(NSDictionary*)parameters allKeys]) {
if (first) first = NO;
else [str appendString:@"&"];
[str appendString:[NSString stringWithFormat:@"%@=%@", key, [(NSDictionary*)parameters objectForKey:key]]];
}
}
}
return [NSURL URLWithString:str];
}
#pragma mark -
#pragma mark Predicates
/**
Generates a predicate from the following kinds of objects:
Predicate - returns untouched
Dictionary - keys equalling values
String - straight transformation using predicate formatting
*/
+ (NSPredicate*) predicateFromObject:(id)object {
return object != nil ? [[self variablePredicateFromObject:object] predicateWithSubstitutionVariables:object] : nil;
}
+ (NSPredicate*) variablePredicateFromObject:(id)object {
if (object != nil) {
if ([object isKindOfClass:[NSPredicate class]])
return object;
if ([object isKindOfClass:[NSString class]])
return [NSPredicate predicateWithFormat:(NSString*)object];
if ([object isKindOfClass:[NSDictionary class]]) {
NSMutableArray *predicates = [NSMutableArray arrayWithCapacity:[object count]];
for (NSString *key in object) {
if (![key hasPrefix:@"$"])
[predicates addObject:[self equivalencyPredicateForKey:key]];
}
return [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
}
if([object isKindOfClass:[NSNumber class]])
return [NSPredicate predicateWithFormat:@"id = %i", object];
}
return [NSPredicate predicateWithValue:YES];
}
+ (NSPredicate*) equivalencyPredicateForKey:(NSString*)key {
return [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:key]
rightExpression:[NSExpression expressionForVariable:key]
modifier:NSDirectPredicateModifier
type:NSEqualToPredicateOperatorType
options:0];
}
@end