Skip to content

Commit afa023a

Browse files
committed
[Attribute] Add an IN operator
1 parent 745a015 commit afa023a

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

QueryKit/Attribute.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public func ~= <T>(left: Attribute<T>, right: T) -> NSPredicate {
6767
return left.expression ~= NSExpression(forConstantValue: right as NSObject)
6868
}
6969

70+
public func << <T>(left: Attribute<T>, right: [T]) -> NSPredicate {
71+
return left.expression << NSExpression(forConstantValue: right._asCocoaArray())
72+
}
73+
74+
public func << <T>(left: Attribute<T>, right: Range<T>) -> NSPredicate {
75+
return left << Array<T>(right)
76+
}
77+
7078
/// MARK: Bool Attributes
7179

7280
prefix public func ! (left: Attribute<Bool>) -> NSPredicate {

QueryKit/Expression.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ public func <= (left: NSExpression, right: NSExpression) -> NSPredicate {
3535
public func ~= (left: NSExpression, right: NSExpression) -> NSPredicate {
3636
return NSComparisonPredicate(leftExpression: left, rightExpression: right, modifier: NSComparisonPredicateModifier.DirectPredicateModifier, type: NSPredicateOperatorType.LikePredicateOperatorType, options: NSComparisonPredicateOptions(0))
3737
}
38+
39+
public func << (left: NSExpression, right: NSExpression) -> NSPredicate {
40+
return NSComparisonPredicate(leftExpression: left, rightExpression: right, modifier: NSComparisonPredicateModifier.DirectPredicateModifier, type: NSPredicateOperatorType.InPredicateOperatorType, options: NSComparisonPredicateOptions(0))
41+
}

QueryKitTests/AttributeTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class AttributeTests: XCTestCase {
8383
var predicate:NSPredicate = (attribute ~= 10)
8484
XCTAssertEqual(predicate, NSPredicate(format:"age LIKE 10")!)
8585
}
86+
87+
func testInOperator() {
88+
var predicate:NSPredicate = (attribute << [5, 10])
89+
XCTAssertEqual(predicate, NSPredicate(format:"age IN %@", [5, 10])!)
90+
}
91+
92+
func testInRangeOperator() {
93+
var predicate:NSPredicate = attribute << (5..<10)
94+
XCTAssertEqual(predicate, NSPredicate(format:"age IN %@", [5, 6, 7, 8, 9])!)
95+
}
8696
}
8797

8898
class CollectionAttributeTests: XCTestCase {

QueryKitTests/ExpressionTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ class ExpressionTests: XCTestCase {
4848
func testLikeOperator() {
4949
XCTAssertEqual(leftHandSide ~= rightHandSide, NSPredicate(format:"age LIKE 10")!)
5050
}
51+
52+
func testInOperator() {
53+
XCTAssertEqual(leftHandSide << rightHandSide, NSPredicate(format:"age IN 10")!)
54+
}
5155
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ let name = Attribute<String>("name")
108108
let age = Attribute<Int>("age")
109109

110110
name == "Kyle"
111+
name << ["Kyle", "Katie"]
111112

112113
age == 27
113114
age >= 25
115+
age << (22...30)
114116
```
115117

116118
#### Operators
@@ -124,6 +126,8 @@ age >= 25
124126
| > | x is more than y |
125127
| >= | x is more than or equal to y |
126128
| ~= | x is like y |
129+
| ~= | x is like y |
130+
| << | x IN y, where y is an array or set |
127131

128132
## Predicate extensions
129133

0 commit comments

Comments
 (0)