From 37fbedd79967efff20edd7e19a847ead2e8397bd Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Wed, 9 Aug 2017 08:11:41 +0900 Subject: [PATCH] WIP: keypath utilities for all raw clauses --- Sources/Where.swift | 110 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/Sources/Where.swift b/Sources/Where.swift index b8b6fdd..1daad7e 100644 --- a/Sources/Where.swift +++ b/Sources/Where.swift @@ -328,6 +328,116 @@ public protocol WhereClause { } +// MARK: - Where where D: NSManagedObject + +public extension Where where D: NSManagedObject { + + /** + Initializes a `Where` clause that compares equality to `nil` + + - parameter keyPath: the keyPath to compare with + - parameter value: the arguments for the `==` operator + */ + public init(_ keyPath: KeyPath, isEqualTo value: Void?) { + + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == nil")) + } + + /** + Initializes a `Where` clause that compares equality to `nil` + + - parameter keyPath: the keyPath to compare with + - parameter value: the arguments for the `==` operator + */ + public init(_ keyPath: KeyPath, isEqualTo value: Void?) { + + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == nil")) + } + + /** + Initializes a `Where` clause that compares equality + + - parameter keyPath: the keyPath to compare with + - parameter value: the arguments for the `==` operator + */ + public init(_ keyPath: KeyPath, isEqualTo value: V?) { + + switch value { + + case nil, + is NSNull: + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == nil")) + + case let value?: + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == %@", argumentArray: [value.cs_toQueryableNativeType()])) + } + } + + /** + Initializes a `Where` clause that compares equality + + - parameter keyPath: the keyPath to compare with + - parameter value: the arguments for the `==` operator + */ + public init(_ keyPath: KeyPath, isEqualTo value: O?) { + + switch value { + + case nil, + is NSNull: + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == nil")) + + case let value?: + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == %@", argumentArray: [value.cs_id()])) + } + } + + /** + Initializes a `Where` clause that compares equality + + - parameter keyPath: the keyPath to compare with + - parameter objectID: the arguments for the `==` operator + */ + public init(_ keyPath: KeyPath, isEqualTo objectID: NSManagedObjectID) { + + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == %@", argumentArray: [objectID])) + } + + /** + Initializes a `Where` clause that compares membership + + - parameter keyPath: the keyPath to compare with + - parameter list: the sequence to check membership of + */ + public init(_ keyPath: KeyPath, isMemberOf list: S) where S.Iterator.Element == V { + + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) IN %@", list.map({ $0.cs_toQueryableNativeType() }) as NSArray)) + } + + /** + Initializes a `Where` clause that compares membership + + - parameter keyPath: the keyPath to compare with + - parameter list: the sequence to check membership of + */ + public init(_ keyPath: KeyPath, isMemberOf list: S) where S.Iterator.Element == O { + + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) IN %@", list.map({ $0.cs_id() }) as NSArray)) + } + + /** + Initializes a `Where` clause that compares membership + + - parameter keyPath: the keyPath to compare with + - parameter list: the sequence to check membership of + */ + public init(_ keyPath: KeyPath, isMemberOf list: S) where S.Iterator.Element: NSManagedObjectID { + + self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) IN %@", list.map({ $0 }) as NSArray)) + } +} + + // MARK: - Sequence where Iterator.Element: WhereClause public extension Sequence where Iterator.Element: WhereClause {