diff --git a/CoreStoreTests/DynamicModelTests.swift b/CoreStoreTests/DynamicModelTests.swift index 47d3c4c..d9692e1 100644 --- a/CoreStoreTests/DynamicModelTests.swift +++ b/CoreStoreTests/DynamicModelTests.swift @@ -72,10 +72,13 @@ class DynamicModelTests: XCTestCase { XCTAssertEqual(mascot.nickname*, "Riko") - let p1 = (Bird.meta.species == "Swift") - XCTAssertEqual(p1, Where("%K == %@", "species", "Swift").predicate) + let p1 = Bird.where({ $0.species == "Swift" }) + XCTAssertEqual(p1.predicate, Where("%K == %@", "species", "Swift").predicate) - let p2 = (Mascot.meta.nickname == "Riko") - XCTAssertEqual(p2, Where("%K == %@", "nickname", "Riko").predicate) + let p2 = Mascot.where({ $0.nickname == "Riko" }) + XCTAssertEqual(p2.predicate, Where("%K == %@", "nickname", "Riko").predicate) + + let p3 = Mascot.where({ $0.year == 2016 }) + XCTAssertEqual(p3.predicate, Where("%K == %@", "year", 2016).predicate) } } diff --git a/Sources/Setup/Dynamic Models/Prototype.swift b/Sources/Setup/Dynamic Models/Prototype.swift index fc8b7de..67a9ee6 100644 --- a/Sources/Setup/Dynamic Models/Prototype.swift +++ b/Sources/Setup/Dynamic Models/Prototype.swift @@ -168,7 +168,7 @@ public extension ManagedObjectProtocol where Self: CoreStoreManagedObject { public typealias Attribute = AttributeContainer - public static var meta: Self { + static var meta: Self { return self.init(nil) } @@ -184,6 +184,12 @@ public extension ManagedObjectProtocol where Self: CoreStoreManagedObject { return attribute(self.meta).keyPath } + + @inline(__always) + public static func `where`(_ condition: (Self) -> Where) -> Where { + + return condition(self.meta) + } } @@ -211,43 +217,36 @@ public postfix func * (_ a public extension AttributeContainer.Required where V: CVarArg { - public static func == (_ attribute: AttributeContainer.Required, _ value: V) -> NSPredicate { + public static func == (_ attribute: AttributeContainer.Required, _ value: V) -> Where { - return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value]) + return Where(attribute.keyPath, isEqualTo: value) } - public static func < (_ attribute: AttributeContainer.Required, _ value: V) -> NSPredicate { + public static func < (_ attribute: AttributeContainer.Required, _ value: V) -> Where { - return NSPredicate(format: "%K < %@", argumentArray: [attribute.keyPath, value]) + return Where("%K < %@", attribute.keyPath, value) } - public static func > (_ attribute: AttributeContainer.Required, _ value: V) -> NSPredicate { + public static func > (_ attribute: AttributeContainer.Required, _ value: V) -> Where { - return NSPredicate(format: "%K > %@", argumentArray: [attribute.keyPath, value]) + return Where("%K > %@", attribute.keyPath, value) } - public static func <= (_ attribute: AttributeContainer.Required, _ value: V) -> NSPredicate { + public static func <= (_ attribute: AttributeContainer.Required, _ value: V) -> Where { - return NSPredicate(format: "%K <= %@", argumentArray: [attribute.keyPath, value]) + return Where("%K <= %@", attribute.keyPath, value) } - public static func >= (_ attribute: AttributeContainer.Required, _ value: V) -> NSPredicate { + public static func >= (_ attribute: AttributeContainer.Required, _ value: V) -> Where { - return NSPredicate(format: "%K >= %@", argumentArray: [attribute.keyPath, value]) + return Where("%K >= %@", attribute.keyPath, value) } - public static func != (_ attribute: AttributeContainer.Required, _ value: V) -> NSPredicate { + public static func != (_ attribute: AttributeContainer.Required, _ value: V) -> Where { - return NSPredicate(format: "%K != %@", argumentArray: [attribute.keyPath, value]) + return Where("%K != %@", attribute.keyPath, value) } } public extension AttributeContainer.Optional where V: CVarArg { - public static func == (_ attribute: AttributeContainer.Optional, _ value: V?) -> NSPredicate { + public static func == (_ attribute: AttributeContainer.Optional, _ value: V?) -> Where { - if let value = value { - - return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value]) - } - else { - - return NSPredicate(format: "%K == nil", attribute.keyPath) - } + return Where(attribute.keyPath, isEqualTo: value) } }