From 06635c9d2fb50ef2c5ab2b7d189edec3a2d5e139 Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Mon, 2 Oct 2017 08:04:28 +0900 Subject: [PATCH] orderby utilities --- CoreStoreTests/DynamicModelTests.swift | 1 + Sources/OrderBy.swift | 67 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/CoreStoreTests/DynamicModelTests.swift b/CoreStoreTests/DynamicModelTests.swift index 30e97cd..4d68ddb 100644 --- a/CoreStoreTests/DynamicModelTests.swift +++ b/CoreStoreTests/DynamicModelTests.swift @@ -237,6 +237,7 @@ class DynamicModelTests: BaseTestDataTestCase { _ = transaction.fetchAll( From() .where(\.age == 10 && \Animal.species == "Dog") + .orderBy(.ascending({ $0.species })) ) _ = transaction.fetchAll( From(), diff --git a/Sources/OrderBy.swift b/Sources/OrderBy.swift index f5199c4..247b66a 100644 --- a/Sources/OrderBy.swift +++ b/Sources/OrderBy.swift @@ -257,6 +257,73 @@ public struct OrderBy: OrderByClause, FetchClause, QueryClause } } +public extension OrderBy where D: CoreStoreObject { + + /** + Indicates that the `KeyPathString` should be sorted in ascending order + */ + public static func ascending(_ attribute: (D) -> A) -> SortKey where A: ValueContainer.Required { + + return .ascending(attribute(D.meta).keyPath) + } + + /** + Indicates that the `KeyPathString` should be sorted in ascending order + */ + public static func ascending(_ attribute: (D) -> A) -> SortKey where A: ValueContainer.Optional { + + return .ascending(attribute(D.meta).keyPath) + } + + /** + Indicates that the `KeyPathString` should be sorted in ascending order + */ + public static func ascending(_ attribute: (D) -> A) -> SortKey where A: TransformableContainer.Required { + + return .ascending(attribute(D.meta).keyPath) + } + + /** + Indicates that the `KeyPathString` should be sorted in ascending order + */ + public static func ascending(_ attribute: (D) -> A) -> SortKey where A: TransformableContainer.Optional { + + return .ascending(attribute(D.meta).keyPath) + } + + /** + Indicates that the `KeyPathString` should be sorted in descending order + */ + public static func descending(_ attribute: (D) -> A) -> SortKey where A: ValueContainer.Required { + + return .descending(attribute(D.meta).keyPath) + } + + /** + Indicates that the `KeyPathString` should be sorted in descending order + */ + public static func descending(_ attribute: (D) -> A) -> SortKey where A: ValueContainer.Optional { + + return .descending(attribute(D.meta).keyPath) + } + + /** + Indicates that the `KeyPathString` should be sorted in descending order + */ + public static func descending(_ attribute: (D) -> A) -> SortKey where A: TransformableContainer.Required { + + return .descending(attribute(D.meta).keyPath) + } + + /** + Indicates that the `KeyPathString` should be sorted in descending order + */ + public static func descending(_ attribute: (D) -> A) -> SortKey where A: TransformableContainer.Optional { + + return .descending(attribute(D.meta).keyPath) + } +} + // MARK: - OrderByClause