fix tests and demo app

This commit is contained in:
John Rommel Estropia
2017-06-17 02:13:54 +09:00
parent 1a99fea820
commit f5b3901caa
2 changed files with 32 additions and 22 deletions

View File

@@ -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
} }
} }

View File

@@ -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 })
}
private static func setName(_ partialObject: PartialObject<Person>, _ newValue: String) {
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 cached return displayName
} }
let primitiveValue = "\(instance.title.value) \(instance.name.value)" let title = partialObject.value(for: { $0.title })
instance.displayName .= primitiveValue let name = partialObject.value(for: { $0.name })
return primitiveValue let displayName = "\(title) \(name)"
partialObject.setPrimitiveValue(displayName, for: { $0.displayName })
return displayName
} }
static func keyPathsAffectingDisplayName() -> Set<String> { static func keyPathsAffectingDisplayName() -> Set<String> {