From 58629bc1dfe87e7c8c3d8b52b81863478e4acb02 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Wed, 19 Feb 2020 13:58:14 +0900 Subject: [PATCH] add missing predicate operator overloads --- Sources/Field.Coded.swift | 2 +- Sources/Field.Stored.swift | 9 ++------- Sources/Field.Virtual.swift | 2 +- Sources/Field.swift | 4 ++-- Sources/KeyPath+Querying.swift | 24 +++++++++++++++++++++++- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Sources/Field.Coded.swift b/Sources/Field.Coded.swift index 8b9df88..c79699a 100644 --- a/Sources/Field.Coded.swift +++ b/Sources/Field.Coded.swift @@ -43,7 +43,7 @@ extension FieldContainer { @Field.Virtual("pluralName", customGetter: Animal.pluralName(_:)) var pluralName: String = "" - @Field.PlistCoded("color") + @Field.Coded("color", coder: FieldCoders.Plist.self) var color: UIColor? } ``` diff --git a/Sources/Field.Stored.swift b/Sources/Field.Stored.swift index 09bd681..a3bc8dd 100644 --- a/Sources/Field.Stored.swift +++ b/Sources/Field.Stored.swift @@ -43,7 +43,7 @@ extension FieldContainer { @Field.Virtual("pluralName", customGetter: Animal.pluralName(_:)) var pluralName: String = "" - @Field.PlistCoded("color") + @Field.Coded("color", coder: FieldCoders.Plist.self) var color: UIColor? } ``` @@ -65,12 +65,7 @@ extension FieldContainer { @Field.Virtual( "displayName", customGetter: { (object, field) in - if let cached = field.primitiveValue, !cached.isEmpty { - return cached - } - let value = "\(object.$title.value) \(object.$name.value)" - field.primitiveValue = value - return value + return "\(object.$title.value) \(object.$name.value)" } ) var displayName: String = "" diff --git a/Sources/Field.Virtual.swift b/Sources/Field.Virtual.swift index 67f6aa8..fe16fa6 100644 --- a/Sources/Field.Virtual.swift +++ b/Sources/Field.Virtual.swift @@ -43,7 +43,7 @@ extension FieldContainer { @Field.Virtual("pluralName", customGetter: Animal.pluralName(_:)) var pluralName: String = "" - @Field.PlistCoded("color") + @Field.Coded("color", coder: FieldCoders.Plist.self) var color: UIColor? } ``` diff --git a/Sources/Field.swift b/Sources/Field.swift index 19f4df6..7ed8018 100644 --- a/Sources/Field.swift +++ b/Sources/Field.swift @@ -41,7 +41,7 @@ extension DynamicObject where Self: CoreStoreObject { @Field.Stored("nickname") var nickname: String? - @Field.PlistCoded("color") + @Field.Coded("color", coder: FieldCoders.Plist.self) var color: UIColor? } ``` @@ -63,7 +63,7 @@ extension DynamicObject where Self: CoreStoreObject { @Field.Stored("nickname") var nickname: String? - @Field.PlistCoded("color") + @Field.Coded("color", coder: FieldCoders.Plist.self) var color: UIColor? } ``` diff --git a/Sources/KeyPath+Querying.swift b/Sources/KeyPath+Querying.swift index 4cdbb88..1fd8ed2 100644 --- a/Sources/KeyPath+Querying.swift +++ b/Sources/KeyPath+Querying.swift @@ -364,7 +364,7 @@ public func ~= (_ sequence: /** Creates a `Where` clause by comparing if a property is equal to a value ``` - let person = dataStack.fetchOne(From().where(\.nickname == "John")) + let person = dataStack.fetchOne(From().where(\.$nickname == "John")) ``` */ public func == (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { @@ -372,6 +372,28 @@ public func == (_ keyPath: KeyPath.Stored>, _ valu return Where(keyPath, isEqualTo: value) } +/** + Creates a `Where` clause by comparing if a property is not equal to a value + ``` + let person = dataStack.fetchOne(From().where(\.$nickname != "John")) + ``` + */ +public func != (_ keyPath: KeyPath.Stored>, _ value: V) -> Where { + + return !Where(keyPath, isEqualTo: value) +} + +/** + Creates a `Where` clause by checking if a sequence contains the value of a property + ``` + let dog = dataStack.fetchOne(From().where(["Pluto", "Snoopy", "Scooby"] ~= \.nickname)) + ``` + */ +public func ~= (_ sequence: S, _ keyPath: KeyPath.Stored>) -> Where where S.Iterator.Element == V { + + return Where(O.meta[keyPath: keyPath].keyPath, isMemberOf: sequence) +} + // MARK: - KeyPath where Root: CoreStoreObject, Value: ValueContainer.Required