mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-18 07:24:09 +01:00
fix tests and demo app
This commit is contained in:
@@ -23,18 +23,18 @@ final class Palette: CoreStoreObject {
|
|||||||
let colorName = Value.Optional<String>(
|
let colorName = Value.Optional<String>(
|
||||||
"colorName",
|
"colorName",
|
||||||
isTransient: true,
|
isTransient: true,
|
||||||
customGetter: Palette.getCachedColorName
|
customGetter: Palette.getColorName
|
||||||
)
|
)
|
||||||
|
|
||||||
private static func getCachedColorName(_ instance: Palette, _ getValue: () -> String?) -> String? {
|
private static func getColorName(_ partialObject: PartialObject<Palette>) -> String? {
|
||||||
|
|
||||||
if let colorName = getValue() {
|
if let colorName = partialObject.primitiveValue(for: { $0.colorName }) {
|
||||||
|
|
||||||
return colorName
|
return colorName
|
||||||
}
|
}
|
||||||
|
|
||||||
let colorName: String
|
let colorName: String
|
||||||
switch instance.hue.value % 360 {
|
switch partialObject.value(for: { $0.hue }) % 360 {
|
||||||
|
|
||||||
case 0 ..< 20: colorName = "Lower Reds"
|
case 0 ..< 20: colorName = "Lower Reds"
|
||||||
case 20 ..< 57: colorName = "Oranges and Browns"
|
case 20 ..< 57: colorName = "Oranges and Browns"
|
||||||
@@ -47,7 +47,7 @@ final class Palette: CoreStoreObject {
|
|||||||
default: colorName = "Upper Reds"
|
default: colorName = "Upper Reds"
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.colorName.primitiveValue = colorName
|
partialObject.setPrimitiveValue(colorName, for: { $0.colorName })
|
||||||
return colorName
|
return colorName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,40 +51,50 @@ class Dog: Animal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Person: CoreStoreObject {
|
class Person: CoreStoreObject {
|
||||||
|
|
||||||
let title = Value.Required<String>(
|
let title = Value.Required<String>(
|
||||||
"title",
|
"title",
|
||||||
default: "Mr.",
|
default: "Mr.",
|
||||||
customSetter: { (`self`, setValue, originalNewValue) in
|
customSetter: Person.setTitle
|
||||||
|
|
||||||
setValue(originalNewValue)
|
|
||||||
self.displayName .= nil
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
let name = Value.Required<String>(
|
let name = Value.Required<String>(
|
||||||
"name",
|
"name",
|
||||||
customSetter: { (`self`, setValue, originalNewValue) in
|
customSetter: Person.setName
|
||||||
|
|
||||||
setValue(originalNewValue)
|
|
||||||
self.displayName .= nil
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
let displayName = Value.Optional<String>(
|
let displayName = Value.Optional<String>(
|
||||||
"displayName",
|
"displayName",
|
||||||
isTransient: true,
|
isTransient: true,
|
||||||
customGetter: Person.cachedDisplayName(_:_:),
|
customGetter: Person.getDisplayName(_:),
|
||||||
affectedByKeyPaths: Person.keyPathsAffectingDisplayName()
|
affectedByKeyPaths: Person.keyPathsAffectingDisplayName()
|
||||||
)
|
)
|
||||||
|
|
||||||
let pets = Relationship.ToManyUnordered<Animal>("pets", inverse: { $0.master })
|
let pets = Relationship.ToManyUnordered<Animal>("pets", inverse: { $0.master })
|
||||||
|
|
||||||
static func cachedDisplayName(_ instance: Person, _ getValue: () -> String?) -> String? {
|
private static func setTitle(_ partialObject: PartialObject<Person>, _ newValue: String) {
|
||||||
|
|
||||||
if let cached = getValue() {
|
partialObject.setPrimitiveValue(newValue, for: { $0.title })
|
||||||
|
partialObject.setPrimitiveValue(nil, for: { $0.displayName })
|
||||||
return cached
|
|
||||||
}
|
}
|
||||||
let primitiveValue = "\(instance.title.value) \(instance.name.value)"
|
|
||||||
instance.displayName .= primitiveValue
|
private static func setName(_ partialObject: PartialObject<Person>, _ newValue: String) {
|
||||||
return primitiveValue
|
|
||||||
|
partialObject.setPrimitiveValue(newValue, for: { $0.name })
|
||||||
|
partialObject.setPrimitiveValue(nil, for: { $0.displayName })
|
||||||
|
}
|
||||||
|
|
||||||
|
static func getDisplayName(_ partialObject: PartialObject<Person>) -> String? {
|
||||||
|
|
||||||
|
if let displayName = partialObject.primitiveValue(for: { $0.displayName }) {
|
||||||
|
|
||||||
|
return displayName
|
||||||
|
}
|
||||||
|
let title = partialObject.value(for: { $0.title })
|
||||||
|
let name = partialObject.value(for: { $0.name })
|
||||||
|
let displayName = "\(title) \(name)"
|
||||||
|
partialObject.setPrimitiveValue(displayName, for: { $0.displayName })
|
||||||
|
return displayName
|
||||||
}
|
}
|
||||||
|
|
||||||
static func keyPathsAffectingDisplayName() -> Set<String> {
|
static func keyPathsAffectingDisplayName() -> Set<String> {
|
||||||
|
|||||||
Reference in New Issue
Block a user