diff --git a/Sources/Where.swift b/Sources/Where.swift index 5cd0294..e1ed743 100644 --- a/Sources/Where.swift +++ b/Sources/Where.swift @@ -332,7 +332,7 @@ public extension Where where D: NSManagedObject { */ public init(_ keyPath: KeyPath, isEqualTo value: Void?) { - self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == nil")) + self.init(keyPath._kvcKeyPathString!, isEqualTo: value) } /** @@ -343,7 +343,7 @@ public extension Where where D: NSManagedObject { */ public init(_ keyPath: KeyPath, isEqualTo value: Void?) { - self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == nil")) + self.init(keyPath._kvcKeyPathString!, isEqualTo: value) } /** @@ -354,15 +354,7 @@ public extension Where where D: NSManagedObject { */ 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()])) - } + self.init(keyPath._kvcKeyPathString!, isEqualTo: value) } /** @@ -373,15 +365,7 @@ public extension Where where D: NSManagedObject { */ 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()])) - } + self.init(keyPath._kvcKeyPathString!, isEqualTo: value) } /** @@ -392,7 +376,7 @@ public extension Where where D: NSManagedObject { */ public init(_ keyPath: KeyPath, isEqualTo objectID: NSManagedObjectID) { - self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) == %@", argumentArray: [objectID])) + self.init(keyPath._kvcKeyPathString!, isEqualTo: objectID) } /** @@ -403,7 +387,7 @@ public extension Where where D: NSManagedObject { */ 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)) + self.init(keyPath._kvcKeyPathString!, isMemberOf: list) } /** @@ -414,7 +398,7 @@ public extension Where where D: NSManagedObject { */ 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)) + self.init(keyPath._kvcKeyPathString!, isMemberOf: list) } /** @@ -425,7 +409,123 @@ public extension Where where D: NSManagedObject { */ public init(_ keyPath: KeyPath, isMemberOf list: S) where S.Iterator.Element: NSManagedObjectID { - self.init(NSPredicate(format: "\(keyPath._kvcKeyPathString!) IN %@", list.map({ $0 }) as NSArray)) + self.init(keyPath._kvcKeyPathString!, isMemberOf: list) + } +} + + +// MARK: - Where where D: CoreStoreObject + +public extension Where where D: CoreStoreObject { + + /** + 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.Optional>, isEqualTo value: Void?) { + + self.init(D.meta[keyPath: keyPath].keyPath, isEqualTo: value) + } + + /** + 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.ToOne>, isEqualTo value: Void?) { + + self.init(D.meta[keyPath: keyPath].keyPath, isEqualTo: value) + } + + /** + 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.Required>, isEqualTo value: V?) { + + self.init(D.meta[keyPath: keyPath].keyPath, isEqualTo: value) + } + + /** + 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.Optional>, isEqualTo value: V?) { + + self.init(D.meta[keyPath: keyPath].keyPath, isEqualTo: value) + } + + /** + 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.ToOne>, isEqualTo value: O?) { + + self.init(D.meta[keyPath: keyPath].keyPath, isEqualTo: value) + } + + /** + 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.ToOne>, isEqualTo objectID: NSManagedObjectID) { + + self.init(D.meta[keyPath: keyPath].keyPath, isEqualTo: 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.Required>, isMemberOf list: S) where S.Iterator.Element == V { + + self.init(D.meta[keyPath: keyPath].keyPath, isMemberOf: list) + } + + /** + 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.Optional>, isMemberOf list: S) where S.Iterator.Element == V { + + self.init(D.meta[keyPath: keyPath].keyPath, isMemberOf: list) + } + + /** + 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.ToOne>, isMemberOf list: S) where S.Iterator.Element == O { + + self.init(D.meta[keyPath: keyPath].keyPath, isMemberOf: list) + } + + /** + 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.ToOne>, isMemberOf list: S) where S.Iterator.Element: NSManagedObjectID { + + self.init(D.meta[keyPath: keyPath].keyPath, isMemberOf: list) } }