diff --git a/CoreStoreTests/DynamicModelTests.swift b/CoreStoreTests/DynamicModelTests.swift index 4d68ddb..197cd7c 100644 --- a/CoreStoreTests/DynamicModelTests.swift +++ b/CoreStoreTests/DynamicModelTests.swift @@ -208,14 +208,14 @@ class DynamicModelTests: BaseTestDataTestCase { stack.perform( asynchronous: { (transaction) in - let p1 = Animal.where({ $0.species == "Sparrow" }) + let p1 = Where({ $0.species == "Sparrow" }) XCTAssertEqual(p1.predicate, NSPredicate(format: "%K == %@", "species", "Sparrow")) let bird = transaction.fetchOne(From(), p1) XCTAssertNotNil(bird) XCTAssertEqual(bird!.species.value, "Sparrow") - let p2 = Dog.where({ $0.nickname == "Spot" }) + let p2 = Where({ $0.nickname == "Spot" }) XCTAssertEqual(p2.predicate, NSPredicate(format: "%K == %@", "nickname", "Spot")) let dog = transaction.fetchOne(From().where(\.nickname == "Spot")) @@ -227,7 +227,7 @@ class DynamicModelTests: BaseTestDataTestCase { XCTAssertNotNil(person) XCTAssertEqual(person!.pets.value.first, dog) - let p3 = Dog.where({ $0.age == 10 }) + let p3 = Where({ $0.age == 10 }) XCTAssertEqual(p3.predicate, NSPredicate(format: "%K == %d", "age", 10)) _ = transaction.fetchAll( @@ -241,19 +241,19 @@ class DynamicModelTests: BaseTestDataTestCase { ) _ = transaction.fetchAll( From(), - Dog.where({ $0.age > 10 && $0.age <= 15 }) + Where({ $0.age > 10 && $0.age <= 15 }) ) _ = transaction.fetchAll( From(), - Dog.where({ $0.species == "Dog" && $0.age == 10 }) + Where({ $0.species == "Dog" && $0.age == 10 }) ) _ = transaction.fetchAll( From(), - Dog.where({ $0.age == 10 && $0.species == "Dog" }) + Where({ $0.age == 10 && $0.species == "Dog" }) ) _ = transaction.fetchAll( From(), - Dog.where({ $0.age > 10 && $0.age <= 15 }) + Where({ $0.age > 10 && $0.age <= 15 }) ) _ = transaction.fetchAll( From(), diff --git a/Sources/CoreStoreObject+Querying.swift b/Sources/CoreStoreObject+Querying.swift index 1d5800f..1f28aee 100644 --- a/Sources/CoreStoreObject+Querying.swift +++ b/Sources/CoreStoreObject+Querying.swift @@ -85,61 +85,6 @@ public extension DynamicObject where Self: CoreStoreObject { return relationship(self.meta).keyPath } - - /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. - ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.nickname == "John" }) - ``` - */ - public static func `where`(_ condition: (Self) -> Where) -> Where { - - return condition(self.meta) - } - - /** - Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. - ``` - let person = CoreStore.fetchAll(From(), Person.orderBy(ascending: { $0.age })) - ``` - */ - public static func orderBy(ascending attribute: (Self) -> ValueContainer.Required) -> OrderBy { - - return OrderBy(.ascending(attribute(self.meta).keyPath)) - } - - /** - Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. - ``` - let person = CoreStore.fetchAll(From(), Person.orderBy(ascending: { $0.age })) - ``` - */ - public static func orderBy(ascending attribute: (Self) -> ValueContainer.Optional) -> OrderBy { - - return OrderBy(.ascending(attribute(self.meta).keyPath)) - } - - /** - Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. - ``` - let person = CoreStore.fetchAll(From(), Person.orderBy(descending: { $0.age })) - ``` - */ - public static func orderBy(descending attribute: (Self) -> ValueContainer.Required) -> OrderBy { - - return OrderBy(.descending(attribute(self.meta).keyPath)) - } - - /** - Creates an `OrderBy` clause from a `CoreStoreObject.Value` property. - ``` - let person = CoreStore.fetchAll(From(), Person.orderBy(descending: { $0.age })) - ``` - */ - public static func orderBy(descending attribute: (Self) -> ValueContainer.Optional) -> OrderBy { - - return OrderBy(.descending(attribute(self.meta).keyPath)) - } } @@ -148,9 +93,9 @@ public extension DynamicObject where Self: CoreStoreObject { public extension ValueContainer.Required { /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.nickname == "John" }) + let person = CoreStore.fetchOne(From().where({ $0.nickname == "John" })) ``` */ @inline(__always) @@ -160,9 +105,9 @@ public extension ValueContainer.Required { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is not equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.nickname != "John" }) + let person = CoreStore.fetchOne(From().where({ $0.nickname != "John" })) ``` */ @inline(__always) @@ -172,9 +117,9 @@ public extension ValueContainer.Required { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is less than a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age < 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age < 20 })) ``` */ @inline(__always) @@ -184,9 +129,9 @@ public extension ValueContainer.Required { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is greater than a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age > 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age > 20 })) ``` */ @inline(__always) @@ -196,9 +141,9 @@ public extension ValueContainer.Required { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is less than or equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age <= 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age <= 20 })) ``` */ @inline(__always) @@ -208,9 +153,9 @@ public extension ValueContainer.Required { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is greater than or equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age >= 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age >= 20 })) ``` */ @inline(__always) @@ -220,9 +165,9 @@ public extension ValueContainer.Required { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by checking if a sequence contains the value of a property ``` - let dog = CoreStore.fetchOne(From(), Dog.where { ["Pluto", "Snoopy", "Scooby"] ~= $0.nickname }) + let dog = CoreStore.fetchOne(From().where({ ["Pluto", "Snoopy", "Scooby"] ~= $0.nickname })) ``` */ @inline(__always) @@ -238,9 +183,9 @@ public extension ValueContainer.Required { public extension ValueContainer.Optional { /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.nickname == "John" }) + let person = CoreStore.fetchOne(From().where({ $0.nickname == "John" })) ``` */ @inline(__always) @@ -250,9 +195,9 @@ public extension ValueContainer.Optional { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is not equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.nickname != "John" }) + let person = CoreStore.fetchOne(From().where({ $0.nickname != "John" })) ``` */ @inline(__always) @@ -262,9 +207,9 @@ public extension ValueContainer.Optional { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is less than a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age < 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age < 20 })) ``` */ @inline(__always) @@ -281,9 +226,9 @@ public extension ValueContainer.Optional { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is greater than a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age > 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age > 20 })) ``` */ @inline(__always) @@ -300,9 +245,9 @@ public extension ValueContainer.Optional { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is less than or equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age <= 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age <= 20 })) ``` */ @inline(__always) @@ -319,9 +264,9 @@ public extension ValueContainer.Optional { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by comparing if a property is greater than or equal to a value ``` - let person = CoreStore.fetchOne(From(), Person.where { $0.age >= 20 }) + let person = CoreStore.fetchOne(From().where({ $0.age >= 20 })) ``` */ @inline(__always) @@ -338,9 +283,9 @@ public extension ValueContainer.Optional { } /** - Creates a `Where` clause from a `CoreStoreObject.Value` property. + Creates a `Where` clause by checking if a sequence contains the value of a property ``` - let dog = CoreStore.fetchOne(From(), Dog.where { ["Pluto", "Snoopy", "Scooby"] ~= $0.nickname }) + let dog = CoreStore.fetchOne(From().where({ ["Pluto", "Snoopy", "Scooby"] ~= $0.nickname })) ``` */ @inline(__always) @@ -356,9 +301,9 @@ public extension ValueContainer.Optional { public extension RelationshipContainer.ToOne { /** - Creates a `Where` clause from a `CoreStoreObject.Relationship` property. + Creates a `Where` clause by comparing if a property is equal to a value ``` - let dog = CoreStore.fetchOne(From(), Dog.where { $0.master == me }) + let dog = CoreStore.fetchOne(From().where({ $0.master == me })) ``` */ @inline(__always) @@ -368,9 +313,9 @@ public extension RelationshipContainer.ToOne { } /** - Creates a `Where` clause from a `CoreStoreObject.Relationship` property. + Creates a `Where` clause by comparing if a property is not equal to a value ``` - let dog = CoreStore.fetchOne(From(), Dog.where { $0.master != me }) + let dog = CoreStore.fetchOne(From().where({ $0.master != me })) ``` */ @inline(__always) @@ -380,9 +325,9 @@ public extension RelationshipContainer.ToOne { } /** - Creates a `Where` clause from a `CoreStoreObject.Relationship` property. + Creates a `Where` clause by checking if a sequence contains the value of a property ``` - let dog = CoreStore.fetchOne(From(), Dog.where { [john, joe, bob] ~= $0.master }) + let dog = CoreStore.fetchOne(From().where({ [john, joe, bob] ~= $0.master })) ``` */ @inline(__always) @@ -391,3 +336,39 @@ public extension RelationshipContainer.ToOne { return Where(relationship.keyPath, isMemberOf: sequence) } } + + +// MARK: Deprecated + +extension DynamicObject where Self: CoreStoreObject { + + @available(*, deprecated, message: "Use the Where(_:) initializer that accepts the same closure argument") + public static func `where`(_ condition: (Self) -> Where) -> Where { + + return condition(self.meta) + } + + @available(*, deprecated, message: "Use the new OrderBy(ascending:) overload that accepts the same closure argument") + public static func orderBy(ascending attribute: (Self) -> ValueContainer.Required) -> OrderBy { + + return OrderBy(.ascending(attribute(self.meta).keyPath)) + } + + @available(*, deprecated, message: "Use the new OrderBy(ascending:) overload that accepts the same closure argument") + public static func orderBy(ascending attribute: (Self) -> ValueContainer.Optional) -> OrderBy { + + return OrderBy(.ascending(attribute(self.meta).keyPath)) + } + + @available(*, deprecated, message: "Use the new OrderBy(descending:) overload that accepts the same closure argument") + public static func orderBy(descending attribute: (Self) -> ValueContainer.Required) -> OrderBy { + + return OrderBy(.descending(attribute(self.meta).keyPath)) + } + + @available(*, deprecated, message: "Use the new OrderBy(descending:) overload that accepts the same closure argument") + public static func orderBy(descending attribute: (Self) -> ValueContainer.Optional) -> OrderBy { + + return OrderBy(.descending(attribute(self.meta).keyPath)) + } +} diff --git a/Sources/From+Querying.swift b/Sources/From+Querying.swift index 277f97b..91648bb 100644 --- a/Sources/From+Querying.swift +++ b/Sources/From+Querying.swift @@ -217,17 +217,37 @@ public extension From { public extension From where D: NSManagedObject { + /** + Creates a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + + - parameter keyPath: the keyPath to query the value for + - returns: a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + */ public func select(_ keyPath: KeyPath) -> QueryChainBuilder { return self.select(R.self, [SelectTerm.attribute(keyPath)]) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections + + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath) -> SectionMonitorChainBuilder { return self.sectionBy(sectionKeyPath._kvcKeyPathString!, { $0 }) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections, and a closure to transform the value for the key path to an appropriate section name + + - Important: Some utilities (such as `ListMonitor`s) may keep `SectionBy`s in memory and may thus introduce retain cycles if reference captures are not handled properly. + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> SectionMonitorChainBuilder { @@ -237,73 +257,159 @@ public extension From where D: NSManagedObject { public extension From where D: CoreStoreObject { + /** + Creates a `FetchChainBuilder` that starts with the specified `Where` clause + + - parameter clause: a closure that returns a `Where` clause + - returns: a `FetchChainBuilder` that starts with the specified `Where` clause + */ public func `where`(_ clause: (D) -> T) -> FetchChainBuilder { return self.fetchChain(appending: clause(D.meta)) } + /** + Creates a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + + - parameter keyPath: the keyPath to query the value for + - returns: a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + */ public func select(_ keyPath: KeyPath.Required>) -> QueryChainBuilder { return self.select(R.self, [SelectTerm.attribute(keyPath)]) } + /** + Creates a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + + - parameter keyPath: the keyPath to query the value for + - returns: a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + */ public func select(_ keyPath: KeyPath.Optional>) -> QueryChainBuilder { return self.select(R.self, [SelectTerm.attribute(keyPath)]) } + /** + Creates a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + + - parameter keyPath: the keyPath to query the value for + - returns: a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + */ public func select(_ keyPath: KeyPath.Required>) -> QueryChainBuilder { return self.select(R.self, [SelectTerm.attribute(keyPath)]) } + /** + Creates a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + + - parameter keyPath: the keyPath to query the value for + - returns: a `QueryChainBuilder` that starts with a `Select` clause created from the specified key path + */ public func select(_ keyPath: KeyPath.Optional>) -> QueryChainBuilder { return self.select(R.self, [SelectTerm.attribute(keyPath)]) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections + + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Required>) -> SectionMonitorChainBuilder { return self.sectionBy(D.meta[keyPath: sectionKeyPath].keyPath, { $0 }) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections + + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Optional>) -> SectionMonitorChainBuilder { return self.sectionBy(D.meta[keyPath: sectionKeyPath].keyPath, { $0 }) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections + + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Required>) -> SectionMonitorChainBuilder { return self.sectionBy(D.meta[keyPath: sectionKeyPath].keyPath, { $0 }) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections + + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Optional>) -> SectionMonitorChainBuilder { return self.sectionBy(D.meta[keyPath: sectionKeyPath].keyPath, { $0 }) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections, and a closure to transform the value for the key path to an appropriate section name + + - Important: Some utilities (such as `ListMonitor`s) may keep `SectionBy`s in memory and may thus introduce retain cycles if reference captures are not handled properly. + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Required>, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> SectionMonitorChainBuilder { return self.sectionBy(D.meta[keyPath: sectionKeyPath].keyPath, sectionIndexTransformer) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections, and a closure to transform the value for the key path to an appropriate section name + + - Important: Some utilities (such as `ListMonitor`s) may keep `SectionBy`s in memory and may thus introduce retain cycles if reference captures are not handled properly. + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Optional>, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> SectionMonitorChainBuilder { return self.sectionBy(D.meta[keyPath: sectionKeyPath].keyPath, sectionIndexTransformer) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections, and a closure to transform the value for the key path to an appropriate section name + + - Important: Some utilities (such as `ListMonitor`s) may keep `SectionBy`s in memory and may thus introduce retain cycles if reference captures are not handled properly. + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Required>, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> SectionMonitorChainBuilder { return self.sectionBy(D.meta[keyPath: sectionKeyPath].keyPath, sectionIndexTransformer) } + /** + Creates a `SectionMonitorChainBuilder` with the key path to use to group `ListMonitor` objects into sections, and a closure to transform the value for the key path to an appropriate section name + + - Important: Some utilities (such as `ListMonitor`s) may keep `SectionBy`s in memory and may thus introduce retain cycles if reference captures are not handled properly. + - parameter sectionKeyPath: the `KeyPath` to use to group the objects into sections + - parameter sectionIndexTransformer: a closure to transform the value for the key path to an appropriate section name + - returns: a `SectionMonitorChainBuilder` that is sectioned by the specified key path + */ @available(OSX 10.12, *) public func sectionBy(_ sectionKeyPath: KeyPath.Optional>, _ sectionIndexTransformer: @escaping (_ sectionName: String?) -> String?) -> SectionMonitorChainBuilder { @@ -313,36 +419,81 @@ public extension From where D: CoreStoreObject { public extension FetchChainBuilder { + /** + Adds a `Where` clause to the `FetchChainBuilder` + + - parameter clause: a `Where` clause to add to the fetch builder + - returns: a new `FetchChainBuilder` containing the `Where` clause + */ public func `where`(_ clause: Where) -> FetchChainBuilder { return self.fetchChain(appending: clause) } + /** + Adds a `Where` clause to the `FetchChainBuilder` + + - parameter format: the format string for the predicate + - parameter args: the arguments for `format` + - returns: a new `FetchChainBuilder` containing the `Where` clause + */ public func `where`(format: String, _ args: Any...) -> FetchChainBuilder { return self.fetchChain(appending: Where(format, argumentArray: args)) } + /** + Adds a `Where` clause to the `FetchChainBuilder` + + - parameter format: the format string for the predicate + - parameter argumentArray: the arguments for `format` + - returns: a new `FetchChainBuilder` containing the `Where` clause + */ public func `where`(format: String, argumentArray: [Any]?) -> FetchChainBuilder { return self.fetchChain(appending: Where(format, argumentArray: argumentArray)) } + /** + Adds an `OrderBy` clause to the `FetchChainBuilder` + + - parameter sortKey: a single `SortKey` + - parameter sortKeys: a series of other `SortKey`s + - returns: a new `FetchChainBuilder` containing the `OrderBy` clause + */ public func orderBy(_ sortKey: OrderBy.SortKey, _ sortKeys: OrderBy.SortKey...) -> FetchChainBuilder { return self.fetchChain(appending: OrderBy([sortKey] + sortKeys)) } + /** + Adds a `Tweak` clause to the `FetchChainBuilder` with a closure where the `NSFetchRequest` may be configured + + - parameter fetchRequest: the block to customize the `NSFetchRequest` + - returns: a new `FetchChainBuilder` containing the `Tweak` clause + */ public func tweak(_ fetchRequest: @escaping (NSFetchRequest) -> Void) -> FetchChainBuilder { return self.fetchChain(appending: Tweak(fetchRequest)) } + /** + Appends a `FetchClause` to the `FetchChainBuilder` + + - parameter clause: the `FetchClause` to add to the `FetchChainBuilder` + - returns: a new `FetchChainBuilder` containing the `FetchClause` + */ public func appending(_ clause: FetchClause) -> FetchChainBuilder { return self.fetchChain(appending: clause) } + /** + Appends a series of `FetchClause`s to the `FetchChainBuilder` + + - parameter clauses: the `FetchClause`s to add to the `FetchChainBuilder` + - returns: a new `FetchChainBuilder` containing the `FetchClause`s + */ public func appending(contentsOf clauses: S) -> FetchChainBuilder where S.Element == FetchClause { return self.fetchChain(appending: clauses) @@ -378,51 +529,115 @@ public extension FetchChainBuilder where D: CoreStoreObject { public extension QueryChainBuilder { + /** + Adds a `Where` clause to the `QueryChainBuilder` + + - parameter clause: a `Where` clause to add to the query builder + - returns: a new `QueryChainBuilder` containing the `Where` clause + */ public func `where`(_ clause: Where) -> QueryChainBuilder { return self.queryChain(appending: clause) } + /** + Adds a `Where` clause to the `QueryChainBuilder` + + - parameter format: the format string for the predicate + - parameter args: the arguments for `format` + - returns: a new `QueryChainBuilder` containing the `Where` clause + */ public func `where`(format: String, _ args: Any...) -> QueryChainBuilder { return self.queryChain(appending: Where(format, argumentArray: args)) } + /** + Adds a `Where` clause to the `QueryChainBuilder` + + - parameter format: the format string for the predicate + - parameter argumentArray: the arguments for `format` + - returns: a new `QueryChainBuilder` containing the `Where` clause + */ public func `where`(format: String, argumentArray: [Any]?) -> QueryChainBuilder { return self.queryChain(appending: Where(format, argumentArray: argumentArray)) } + /** + Adds an `OrderBy` clause to the `QueryChainBuilder` + + - parameter sortKey: a single `SortKey` + - parameter sortKeys: a series of other `SortKey`s + - returns: a new `QueryChainBuilder` containing the `OrderBy` clause + */ public func orderBy(_ sortKey: OrderBy.SortKey, _ sortKeys: OrderBy.SortKey...) -> QueryChainBuilder { return self.queryChain(appending: OrderBy([sortKey] + sortKeys)) } + /** + Adds a `Tweak` clause to the `QueryChainBuilder` with a closure where the `NSFetchRequest` may be configured + + - parameter fetchRequest: the block to customize the `NSFetchRequest` + - returns: a new `QueryChainBuilder` containing the `Tweak` clause + */ public func tweak(_ fetchRequest: @escaping (NSFetchRequest) -> Void) -> QueryChainBuilder { return self.queryChain(appending: Tweak(fetchRequest)) } + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter clause: a `GroupBy` clause to add to the query builder + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ clause: GroupBy) -> QueryChainBuilder { return self.queryChain(appending: clause) } + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter keyPath: a key path to group the query results with + - parameter keyPaths: other key paths to group the query results with + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ keyPath: KeyPathString, _ keyPaths: KeyPathString...) -> QueryChainBuilder { return self.groupBy(GroupBy([keyPath] + keyPaths)) } + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter keyPaths: a series of key paths to group the query results with + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ keyPaths: [KeyPathString]) -> QueryChainBuilder { return self.queryChain(appending: GroupBy(keyPaths)) } + /** + Appends a `QueryClause` to the `QueryChainBuilder` + + - parameter clause: the `QueryClause` to add to the `QueryChainBuilder` + - returns: a new `QueryChainBuilder` containing the `QueryClause` + */ public func appending(_ clause: QueryClause) -> QueryChainBuilder { return self.queryChain(appending: clause) } + /** + Appends a series of `QueryClause`s to the `QueryChainBuilder` + + - parameter clauses: the `QueryClause`s to add to the `QueryChainBuilder` + - returns: a new `QueryChainBuilder` containing the `QueryClause`s + */ public func appending(contentsOf clauses: S) -> QueryChainBuilder where S.Element == QueryClause { return self.queryChain(appending: clauses) @@ -452,6 +667,12 @@ public extension QueryChainBuilder { public extension QueryChainBuilder where D: NSManagedObject { + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter keyPath: a key path to group the query results with + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ keyPath: KeyPath) -> QueryChainBuilder { return self.groupBy(GroupBy(keyPath)) @@ -460,26 +681,56 @@ public extension QueryChainBuilder where D: NSManagedObject { public extension QueryChainBuilder where D: CoreStoreObject { + /** + Adds a `Where` clause to the `QueryChainBuilder` + + - parameter clause: a `Where` clause to add to the query builder + - returns: a new `QueryChainBuilder` containing the `Where` clause + */ public func `where`(_ clause: (D) -> T) -> QueryChainBuilder { return self.queryChain(appending: clause(D.meta)) } + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter keyPath: a key path to group the query results with + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ keyPath: KeyPath.Required>) -> QueryChainBuilder { return self.groupBy(GroupBy(keyPath)) } + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter keyPath: a key path to group the query results with + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ keyPath: KeyPath.Optional>) -> QueryChainBuilder { return self.groupBy(GroupBy(keyPath)) } + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter keyPath: a key path to group the query results with + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ keyPath: KeyPath.Required>) -> QueryChainBuilder { return self.groupBy(GroupBy(keyPath)) } + /** + Adds a `GroupBy` clause to the `QueryChainBuilder` + + - parameter keyPath: a key path to group the query results with + - returns: a new `QueryChainBuilder` containing the `GroupBy` clause + */ public func groupBy(_ keyPath: KeyPath.Optional>) -> QueryChainBuilder { return self.groupBy(GroupBy(keyPath)) @@ -489,36 +740,81 @@ public extension QueryChainBuilder where D: CoreStoreObject { @available(OSX 10.12, *) public extension SectionMonitorChainBuilder { + /** + Adds a `Where` clause to the `SectionMonitorChainBuilder` + + - parameter clause: a `Where` clause to add to the fetch builder + - returns: a new `SectionMonitorChainBuilder` containing the `Where` clause + */ public func `where`(_ clause: Where) -> SectionMonitorChainBuilder { return self.sectionMonitorChain(appending: clause) } + /** + Adds a `Where` clause to the `SectionMonitorChainBuilder` + + - parameter format: the format string for the predicate + - parameter args: the arguments for `format` + - returns: a new `SectionMonitorChainBuilder` containing the `Where` clause + */ public func `where`(format: String, _ args: Any...) -> SectionMonitorChainBuilder { return self.sectionMonitorChain(appending: Where(format, argumentArray: args)) } + /** + Adds a `Where` clause to the `SectionMonitorChainBuilder` + + - parameter format: the format string for the predicate + - parameter argumentArray: the arguments for `format` + - returns: a new `SectionMonitorChainBuilder` containing the `Where` clause + */ public func `where`(format: String, argumentArray: [Any]?) -> SectionMonitorChainBuilder { return self.sectionMonitorChain(appending: Where(format, argumentArray: argumentArray)) } + /** + Adds an `OrderBy` clause to the `SectionMonitorChainBuilder` + + - parameter sortKey: a single `SortKey` + - parameter sortKeys: a series of other `SortKey`s + - returns: a new `SectionMonitorChainBuilder` containing the `OrderBy` clause + */ public func orderBy(_ sortKey: OrderBy.SortKey, _ sortKeys: OrderBy.SortKey...) -> SectionMonitorChainBuilder { return self.sectionMonitorChain(appending: OrderBy([sortKey] + sortKeys)) } + /** + Adds a `Tweak` clause to the `SectionMonitorChainBuilder` with a closure where the `NSFetchRequest` may be configured + + - parameter fetchRequest: the block to customize the `NSFetchRequest` + - returns: a new `SectionMonitorChainBuilder` containing the `Tweak` clause + */ public func tweak(_ fetchRequest: @escaping (NSFetchRequest) -> Void) -> SectionMonitorChainBuilder { return self.sectionMonitorChain(appending: Tweak(fetchRequest)) } + /** + Appends a `QueryClause` to the `SectionMonitorChainBuilder` + + - parameter clause: the `QueryClause` to add to the `SectionMonitorChainBuilder` + - returns: a new `SectionMonitorChainBuilder` containing the `QueryClause` + */ public func appending(_ clause: FetchClause) -> SectionMonitorChainBuilder { return self.sectionMonitorChain(appending: clause) } + /** + Appends a series of `QueryClause`s to the `SectionMonitorChainBuilder` + + - parameter clauses: the `QueryClause`s to add to the `SectionMonitorChainBuilder` + - returns: a new `SectionMonitorChainBuilder` containing the `QueryClause`s + */ public func appending(contentsOf clauses: S) -> SectionMonitorChainBuilder where S.Element == FetchClause { return self.sectionMonitorChain(appending: clauses) @@ -549,6 +845,12 @@ public extension SectionMonitorChainBuilder { @available(OSX 10.12, *) public extension SectionMonitorChainBuilder where D: CoreStoreObject { + /** + Adds a `Where` clause to the `SectionMonitorChainBuilder` + + - parameter clause: a `Where` clause to add to the fetch builder + - returns: a new `SectionMonitorChainBuilder` containing the `Where` clause + */ public func `where`(_ clause: (D) -> T) -> SectionMonitorChainBuilder { return self.sectionMonitorChain(appending: clause(D.meta)) diff --git a/Sources/GroupBy.swift b/Sources/GroupBy.swift index 88fdb5d..8c9c929 100644 --- a/Sources/GroupBy.swift +++ b/Sources/GroupBy.swift @@ -105,6 +105,11 @@ public struct GroupBy: GroupByClause, QueryClause, Hashable { public extension GroupBy where D: NSManagedObject { + /** + Initializes a `GroupBy` clause with a key path + + - parameter keyPath: a key path to group results with + */ public init(_ keyPath: KeyPath) { self.init([keyPath._kvcKeyPathString!]) @@ -113,21 +118,41 @@ public extension GroupBy where D: NSManagedObject { public extension GroupBy where D: CoreStoreObject { + /** + Initializes a `GroupBy` clause with a key path + + - parameter keyPath: a key path to group results with + */ public init(_ keyPath: KeyPath.Required>) { self.init([D.meta[keyPath: keyPath].keyPath]) } + /** + Initializes a `GroupBy` clause with a key path + + - parameter keyPath: a key path to group results with + */ public init(_ keyPath: KeyPath.Optional>) { self.init([D.meta[keyPath: keyPath].keyPath]) } + /** + Initializes a `GroupBy` clause with a key path + + - parameter keyPath: a key path to group results with + */ public init(_ keyPath: KeyPath.Required>) { self.init([D.meta[keyPath: keyPath].keyPath]) } + /** + Initializes a `GroupBy` clause with a key path + + - parameter keyPath: a key path to group results with + */ public init(_ keyPath: KeyPath.Optional>) { self.init([D.meta[keyPath: keyPath].keyPath]) diff --git a/Sources/KeyPath+Querying.swift b/Sources/KeyPath+Querying.swift index 9893789..f9a120c 100644 --- a/Sources/KeyPath+Querying.swift +++ b/Sources/KeyPath+Querying.swift @@ -29,16 +29,34 @@ import Foundation // MARK: - KeyPath where Root: NSManagedObject, Value: QueryableAttributeType & Equatable +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname == "John")) + ``` + */ public func == (_ keyPath: KeyPath, _ value: V) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: value) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname != "John")) + ``` + */ public func != (_ keyPath: KeyPath, _ value: V) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: value) } +/** + Creates a `Where` clause by checking if a sequence contains the value of a property + ``` + let dog = CoreStore.fetchOne(From().where(["Pluto", "Snoopy", "Scooby"] ~= \.nickname)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath) -> Where where S.Iterator.Element == V { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) @@ -47,16 +65,34 @@ public func ~= +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname == "John")) + ``` + */ public func == (_ keyPath: KeyPath>, _ value: V?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: value) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname != "John")) + ``` + */ public func != (_ keyPath: KeyPath>, _ value: V?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: value) } +/** + Creates a `Where` clause by checking if a sequence contains the value of a property + ``` + let dog = CoreStore.fetchOne(From().where(["Pluto", "Snoopy", "Scooby"] ~= \.nickname)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath>) -> Where where S.Iterator.Element == V { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) @@ -65,21 +101,45 @@ public func ~= ().where(\.age < 20)) + ``` + */ public func < (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K < %@", keyPath._kvcKeyPathString!, value) } +/** + Creates a `Where` clause by comparing if a property is greater than a value + ``` + let person = CoreStore.fetchOne(From().where(\.age > 20)) + ``` + */ public func > (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K > %@", keyPath._kvcKeyPathString!, value) } +/** + Creates a `Where` clause by comparing if a property is less than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age <= 20)) + ``` + */ public func <= (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K <= %@", keyPath._kvcKeyPathString!, value) } +/** + Creates a `Where` clause by comparing if a property is greater than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age >= 20)) + ``` + */ public func >= (_ keyPath: KeyPath, _ value: V) -> Where { return Where("%K >= %@", keyPath._kvcKeyPathString!, value) @@ -88,6 +148,12 @@ public func >= (_ ke // MARK: - KeyPath where Root: NSManagedObject, Value: Optional +/** + Creates a `Where` clause by comparing if a property is less than a value + ``` + let person = CoreStore.fetchOne(From().where(\.age < 20)) + ``` + */ public func < (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { @@ -100,6 +166,12 @@ public func < (_ key } } +/** + Creates a `Where` clause by comparing if a property is greater than a value + ``` + let person = CoreStore.fetchOne(From().where(\.age > 20)) + ``` + */ public func > (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { @@ -112,6 +184,12 @@ public func > (_ key } } +/** + Creates a `Where` clause by comparing if a property is less than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age <= 20)) + ``` + */ public func <= (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { @@ -124,6 +202,12 @@ public func <= (_ ke } } +/** + Creates a `Where` clause by comparing if a property is greater than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age >= 20)) + ``` + */ public func >= (_ keyPath: KeyPath>, _ value: V?) -> Where { if let value = value { @@ -139,31 +223,67 @@ public func >= (_ ke // MARK: - KeyPath where Root: NSManagedObject, Value: NSManagedObject +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master == john)) + ``` + */ public func == (_ keyPath: KeyPath, _ object: D) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: object) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master != john)) + ``` + */ public func != (_ keyPath: KeyPath, _ object: D) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: object) } +/** + Creates a `Where` clause by checking if a sequence contains a value of a property + ``` + let dog = CoreStore.fetchOne(From().where([john, bob, joe] ~= \.master)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath) -> Where where S.Iterator.Element == D { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master == john)) + ``` + */ public func == (_ keyPath: KeyPath, _ objectID: NSManagedObjectID?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master != john)) + ``` + */ public func != (_ keyPath: KeyPath, _ objectID: NSManagedObjectID?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } +/** + Creates a `Where` clause by checking if a sequence contains a value of a property + ``` + let dog = CoreStore.fetchOne(From().where([john, bob, joe] ~= \.master)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath) -> Where where S.Iterator.Element == NSManagedObjectID { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) @@ -172,31 +292,67 @@ public func ~= (_ sequence: // MARK: - KeyPath where Root: NSManagedObject, Value: Optional +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master == john)) + ``` + */ public func == (_ keyPath: KeyPath>, _ object: D?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: object) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master != john)) + ``` + */ public func != (_ keyPath: KeyPath>, _ object: D?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: object) } +/** + Creates a `Where` clause by checking if a sequence contains a value of a property + ``` + let dog = CoreStore.fetchOne(From().where([john, bob, joe] ~= \.master)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath>) -> Where where S.Iterator.Element == D { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) } +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master == john)) + ``` + */ public func == (_ keyPath: KeyPath>, _ objectID: NSManagedObjectID?) -> Where { return Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master != john)) + ``` + */ public func != (_ keyPath: KeyPath>, _ objectID: NSManagedObjectID?) -> Where { return !Where(keyPath._kvcKeyPathString!, isEqualTo: objectID) } +/** + Creates a `Where` clause by checking if a sequence contains a value of a property + ``` + let dog = CoreStore.fetchOne(From().where([john, bob, joe] ~= \.master)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath>) -> Where where S.Iterator.Element == NSManagedObjectID { return Where(keyPath._kvcKeyPathString!, isMemberOf: sequence) @@ -205,16 +361,34 @@ public func ~= (_ sequence: // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Required +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname == "John")) + ``` + */ public func == (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname != "John")) + ``` + */ public func != (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } +/** + Creates a `Where` clause by checking if a sequence contains the value of a property + ``` + let dog = CoreStore.fetchOne(From().where(["Pluto", "Snoopy", "Scooby"] ~= \.nickname)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath.Required>) -> Where where S.Iterator.Element == V { return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) @@ -223,16 +397,34 @@ public func ~= (_ sequence: S, _ keyPath: KeyPath.Optional +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname == "John")) + ``` + */ public func == (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.nickname != "John")) + ``` + */ public func != (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: value) } +/** + Creates a `Where` clause by checking if a sequence contains the value of a property + ``` + let dog = CoreStore.fetchOne(From().where(["Pluto", "Snoopy", "Scooby"] ~= \.nickname)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath.Optional>) -> Where where S.Iterator.Element == V { return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) @@ -241,21 +433,45 @@ public func ~= (_ sequence: S, _ keyPath: KeyPath.Required +/** + Creates a `Where` clause by comparing if a property is less than a value + ``` + let person = CoreStore.fetchOne(From().where(\.age < 20)) + ``` + */ public func < (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K < %@", O.meta[keyPath: keyPath].keyPath, value) } +/** + Creates a `Where` clause by comparing if a property is greater than a value + ``` + let person = CoreStore.fetchOne(From().where(\.age > 20)) + ``` + */ public func > (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K > %@", O.meta[keyPath: keyPath].keyPath, value) } +/** + Creates a `Where` clause by comparing if a property is less than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age <= 20)) + ``` + */ public func <= (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K <= %@", O.meta[keyPath: keyPath].keyPath, value) } +/** + Creates a `Where` clause by comparing if a property is greater than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age >= 20)) + ``` + */ public func >= (_ keyPath: KeyPath.Required>, _ value: V) -> Where { return Where("%K >= %@", O.meta[keyPath: keyPath].keyPath, value) @@ -264,6 +480,12 @@ public func >= (_ keyPath: KeyPath.Requir // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Optional +/** + Creates a `Where` clause by comparing if a property is less than a value + ``` + let person = CoreStore.fetchOne(From().where(\.age < 20)) + ``` + */ public func < (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { @@ -276,6 +498,12 @@ public func < (_ keyPath: KeyPath.Optional>, _ val } } +/** + Creates a `Where` clause by comparing if a property is greater than a value + ``` + let person = CoreStore.fetchOne(From().where(\.age > 20)) + ``` + */ public func > (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { @@ -288,6 +516,12 @@ public func > (_ keyPath: KeyPath.Optional>, _ val } } +/** + Creates a `Where` clause by comparing if a property is less than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age <= 20)) + ``` + */ public func <= (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { @@ -300,6 +534,12 @@ public func <= (_ keyPath: KeyPath.Optional>, _ va } } +/** + Creates a `Where` clause by comparing if a property is greater than or equal to a value + ``` + let person = CoreStore.fetchOne(From().where(\.age >= 20)) + ``` + */ public func >= (_ keyPath: KeyPath.Optional>, _ value: V?) -> Where { if let value = value { @@ -315,26 +555,56 @@ public func >= (_ keyPath: KeyPath.Optional>, _ va // MARK: - KeyPath where Root: CoreStoreObject, Value: RelationshipContainer.ToOne +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master == john)) + ``` + */ public func == (_ keyPath: KeyPath.ToOne>, _ object: D) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } +/** + Creates a `Where` clause by comparing if a property is equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master == john)) + ``` + */ public func == (_ keyPath: KeyPath.ToOne>, _ object: D?) -> Where { return Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master != john)) + ``` + */ public func != (_ keyPath: KeyPath.ToOne>, _ object: D) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let dog = CoreStore.fetchOne(From().where(\.master != john)) + ``` + */ public func != (_ keyPath: KeyPath.ToOne>, _ object: D?) -> Where { return !Where(O.meta[keyPath: keyPath].keyPath, isEqualTo: object) } +/** + Creates a `Where` clause by checking if a sequence contains a value of a property + ``` + let dog = CoreStore.fetchOne(From().where([john, bob, joe] ~= \.master)) + ``` + */ public func ~= (_ sequence: S, _ keyPath: KeyPath.ToOne>) -> Where where S.Iterator.Element == D { return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) diff --git a/Sources/Where.swift b/Sources/Where.swift index 96eb888..9ec9329 100644 --- a/Sources/Where.swift +++ b/Sources/Where.swift @@ -502,6 +502,16 @@ public extension Where where D: CoreStoreObject { self.init(D.meta[keyPath: keyPath].keyPath, isMemberOf: list) } + + /** + Initializes a `Where` clause from a closure + + - parameter condition: closure that returns the `Where` clause + */ + public init(_ condition: (D) -> Where) { + + self = condition(D.meta) + } }