add Where comparison operators for optional values

This commit is contained in:
John Rommel Estropia
2017-06-05 23:09:19 +09:00
parent fc7df671de
commit ddd83da434

View File

@@ -276,6 +276,82 @@ public extension ValueContainer.Optional {
return !Where(attribute.keyPath, isEqualTo: value)
}
/**
Creates a `Where` clause from a `CoreStoreObject.Value` property.
```
let person = CoreStore.fetchOne(From<Person>(), Person.where { $0.age < 20 })
```
*/
@inline(__always)
public static func < (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
if let value = value {
return Where("%K < %@", attribute.keyPath, value)
}
else {
return Where("%K < nil", attribute.keyPath)
}
}
/**
Creates a `Where` clause from a `CoreStoreObject.Value` property.
```
let person = CoreStore.fetchOne(From<Person>(), Person.where { $0.age > 20 })
```
*/
@inline(__always)
public static func > (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
if let value = value {
return Where("%K > %@", attribute.keyPath, value)
}
else {
return Where("%K > nil", attribute.keyPath)
}
}
/**
Creates a `Where` clause from a `CoreStoreObject.Value` property.
```
let person = CoreStore.fetchOne(From<Person>(), Person.where { $0.age <= 20 })
```
*/
@inline(__always)
public static func <= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
if let value = value {
return Where("%K <= %@", attribute.keyPath, value)
}
else {
return Where("%K <= nil", attribute.keyPath)
}
}
/**
Creates a `Where` clause from a `CoreStoreObject.Value` property.
```
let person = CoreStore.fetchOne(From<Person>(), Person.where { $0.age >= 20 })
```
*/
@inline(__always)
public static func >= (_ attribute: ValueContainer<O>.Optional<V>, _ value: V?) -> Where {
if let value = value {
return Where("%K >= %@", attribute.keyPath, value)
}
else {
return Where("%K >= nil", attribute.keyPath)
}
}
/**
Creates a `Where` clause from a `CoreStoreObject.Value` property.
```