Skip to content

Commit ea1b7c9

Browse files
committed
[Attribute] Restore IN and BETWEEN
1 parent 63cdab8 commit ea1b7c9

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

QueryKit/Attribute.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,17 @@ public func ~= <T>(left: Attribute<T>, right: T) -> NSPredicate {
8888
return left.expression ~= left.expressionForValue(right)
8989
}
9090

91-
//public func << <T>(left: Attribute<T>, right: [T]) -> NSPredicate {
92-
// return left.expression << NSExpression(forConstantValue: right._asCocoaArray())
93-
//}
94-
//
95-
//public func << <T>(left: Attribute<T>, right: Range<T>) -> NSPredicate {
96-
// let rightExpression = NSExpression(forConstantValue: [right.startIndex, right.endIndex]._asCocoaArray())
97-
//
98-
// return NSComparisonPredicate(leftExpression: left.expression, rightExpression: rightExpression, modifier: NSComparisonPredicateModifier.DirectPredicateModifier, type: NSPredicateOperatorType.BetweenPredicateOperatorType, options: NSComparisonPredicateOptions(0))
99-
//}
91+
public func << <T>(left: Attribute<T>, right: [T]) -> NSPredicate {
92+
let value = map(right) { value in return value as! NSObject }
93+
return left.expression << NSExpression(forConstantValue: value)
94+
}
95+
96+
public func << <T>(left: Attribute<T>, right: Range<T>) -> NSPredicate {
97+
let value = [right.startIndex as! NSObject, right.endIndex as! NSObject] as NSArray
98+
let rightExpression = NSExpression(forConstantValue: value)
99+
100+
return NSComparisonPredicate(leftExpression: left.expression, rightExpression: rightExpression, modifier: NSComparisonPredicateModifier.DirectPredicateModifier, type: NSPredicateOperatorType.BetweenPredicateOperatorType, options: NSComparisonPredicateOptions(0))
101+
}
100102

101103
/// MARK: Bool Attributes
102104

QueryKitTests/AttributeTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ class AttributeTests: XCTestCase {
8484
XCTAssertEqual(predicate, NSPredicate(format: "age LIKE 10"))
8585
}
8686

87-
// func testInOperator() {
88-
// var predicate:NSPredicate = (attribute << [5, 10])
89-
// XCTAssertEqual(predicate, NSPredicate(format:"age IN %@", [5, 10]))
90-
// }
91-
92-
// func testBetweenRangeOperator() {
93-
// var predicate:NSPredicate = attribute << (5..<10)
94-
// XCTAssertEqual(predicate, NSPredicate(format:"age BETWEEN %@", [5, 10])!)
95-
// }
87+
func testInOperator() {
88+
var predicate:NSPredicate = (attribute << [5, 10])
89+
XCTAssertEqual(predicate, NSPredicate(format: "age IN %@", [5, 10]))
90+
}
91+
92+
func testBetweenRangeOperator() {
93+
var predicate:NSPredicate = attribute << (5..<10)
94+
XCTAssertEqual(predicate, NSPredicate(format: "age BETWEEN %@", [5, 10]))
95+
}
9696

9797
func testOptionalEqualityOperator() {
9898
let attribute = Attribute<String?>("name")

0 commit comments

Comments
 (0)