Skip to content

Commit 88856a0

Browse files
committed
Reorganization, documentation
1 parent d49307a commit 88856a0

File tree

6 files changed

+79
-31
lines changed

6 files changed

+79
-31
lines changed

CoreDataQueryInterface/Attribute.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class Attribute: CustomStringConvertible, CustomExpressionConvertible, Pr
4343
_parent = parent
4444
_type = type
4545
}
46+
/**
47+
Returns the attribute as a keypath.
48+
*/
4649
public private(set) lazy var description: String = {
4750
if let parent = self._parent {
4851
let parentName = String(parent)
@@ -52,6 +55,11 @@ public class Attribute: CustomStringConvertible, CustomExpressionConvertible, Pr
5255
return self._name ?? ""
5356
}
5457
}()
58+
/**
59+
The `NSExpression` to which this attribute resolves.
60+
61+
- note: This is usually a keypath expression.
62+
*/
5563
public private(set) lazy var expression: NSExpression = {
5664
let keyPath = String(self)
5765
if keyPath.hasPrefix("$") {
@@ -62,6 +70,10 @@ public class Attribute: CustomStringConvertible, CustomExpressionConvertible, Pr
6270
return NSExpression(forKeyPath: keyPath)
6371
}
6472
}()
73+
74+
/**
75+
76+
*/
6577
public func named(name: String, type: NSAttributeType? = nil) -> NSExpressionDescription {
6678
let e = NSExpressionDescription()
6779
e.expression = expression
@@ -99,9 +111,3 @@ extension Aggregable where Self: Attribute {
99111
return AggregateType("@min", parent: self)
100112
}
101113
}
102-
103-
extension Attribute: CustomPropertyConvertible {
104-
public var property: AnyObject {
105-
return String(self)
106-
}
107-
}

CoreDataQueryInterface/BooleanAttribute.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ SOFTWARE.
2525
import CoreData
2626
import Foundation
2727

28-
extension Bool: TypedExpressionConvertible {
29-
public typealias ExpressionValueType = Bool
30-
public var expression: NSExpression {
31-
return NSExpression(forConstantValue: self)
32-
}
33-
}
34-
3528
public class BooleanAttribute: Attribute, TypedExpressionConvertible {
3629
public typealias ExpressionValueType = Bool
3730
public required init(_ name: String, parent: Attribute? = nil, type: NSAttributeType? = nil) {
@@ -42,3 +35,9 @@ public class BooleanAttribute: Attribute, TypedExpressionConvertible {
4235
}
4336
}
4437

38+
extension Bool: TypedExpressionConvertible {
39+
public typealias ExpressionValueType = Bool
40+
public var expression: NSExpression {
41+
return NSExpression(forConstantValue: self)
42+
}
43+
}

CoreDataQueryInterface/CustomPropertyConvertible.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ SOFTWARE.
2525
import Foundation
2626
import CoreData
2727

28+
/**
29+
Represents a type that can be used with `NSFetchRequest`'s `propertiesToFetch`
30+
and `propertiesToGroupBy` properties.
31+
32+
- note: In Swift, these properties have the type `[AnyObject]` (i.e., `NSArray`), but
33+
in practice only strings and subtypes of `NSPropertyDescription` can be used.
34+
*/
2835
public protocol CustomPropertyConvertible {
2936
var property: AnyObject { get }
3037
}
@@ -41,3 +48,8 @@ extension NSPropertyDescription: CustomPropertyConvertible {
4148
}
4249
}
4350

51+
extension Attribute: CustomPropertyConvertible {
52+
public var property: AnyObject {
53+
return String(self)
54+
}
55+
}

CoreDataQueryInterface/DataAttribute.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ SOFTWARE.
2525
import CoreData
2626
import Foundation
2727

28-
extension NSData: TypedExpressionConvertible {
29-
public typealias ExpressionValueType = NSData
30-
public var expression: NSExpression {
31-
return NSExpression(forConstantValue: self)
32-
}
33-
}
34-
3528
public class DataAttribute: Attribute, TypedExpressionConvertible {
3629
public typealias ExpressionValueType = NSData
3730
public required init(_ name: String, parent: Attribute? = nil, type: NSAttributeType? = nil) {
@@ -42,3 +35,10 @@ public class DataAttribute: Attribute, TypedExpressionConvertible {
4235
}
4336
}
4437

38+
extension NSData: TypedExpressionConvertible {
39+
public typealias ExpressionValueType = NSData
40+
public var expression: NSExpression {
41+
return NSExpression(forConstantValue: self)
42+
}
43+
}
44+

CoreDataQueryInterface/EntityAttribute.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ SOFTWARE.
2525
import Foundation
2626
import CoreData
2727

28-
extension NSManagedObject: TypedExpressionConvertible, ComparableExpression {
29-
public typealias ExpressionValueType = NSManagedObject
30-
public var expression: NSExpression {
31-
return NSExpression(forConstantValue: self)
32-
}
33-
}
34-
3528
public class EntityAttribute: Attribute, TypedExpressionConvertible {
3629
public typealias ExpressionValueType = NSManagedObject
3730
public required init(_ name: String, parent: Attribute? = nil, type: NSAttributeType? = nil) {
@@ -41,3 +34,11 @@ public class EntityAttribute: Attribute, TypedExpressionConvertible {
4134
super.init()
4235
}
4336
}
37+
38+
extension NSManagedObject: TypedExpressionConvertible, ComparableExpression {
39+
public typealias ExpressionValueType = NSManagedObject
40+
public var expression: NSExpression {
41+
return NSExpression(forConstantValue: self)
42+
}
43+
}
44+

CoreDataQueryInterface/Selecting.swift

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,68 @@ import Foundation
2727

2828
extension ExpressionQueryType {
2929

30+
/**
31+
Projection, i.e., select attributes of an entity rather than the entities themselves.
32+
33+
`CustomPropertyConvertible` is implemented by `String`, `Attribute` and `NSPropertyDescription`,
34+
so any of these can be used here.
35+
*/
3036
public func select(expressions: [CustomPropertyConvertible]) -> ExpressionQuery<QueryEntityType> {
3137
var builder = self.builder
3238
builder.expressions.appendContentsOf(expressions)
3339
return ExpressionQuery(builder: builder)
3440
}
35-
41+
42+
/**
43+
Projection, i.e., select attributes of an entity rather than the entities themselves.
44+
45+
`CustomPropertyConvertible` is implemented by `String`, `Attribute` and `NSPropertyDescription`,
46+
so any of these can be used here.
47+
*/
3648
public func select(expressions: CustomPropertyConvertible...) -> ExpressionQuery<QueryEntityType> {
3749
return select(expressions)
3850
}
3951

52+
/**
53+
Projection, i.e., select attributes of an entity rather than the entities themselves.
54+
55+
.select(employee in [employee.firstName, employee.lastName])
56+
*/
4057
public func select(expressions: QueryEntityType.EntityAttributeType -> [CustomPropertyConvertible]) -> ExpressionQuery<QueryEntityType> {
4158
let attribute = QueryEntityType.EntityAttributeType()
4259
return select(expressions(attribute))
4360
}
44-
61+
62+
/**
63+
Projection, i.e., select attributes of an entity rather than the entities themselves.
64+
65+
.select([{$0.firstName}, {$0.lastName}])
66+
*/
4567
public func select(expressions: [QueryEntityType.EntityAttributeType -> CustomPropertyConvertible]) -> ExpressionQuery<QueryEntityType> {
4668
let attribute = QueryEntityType.EntityAttributeType()
4769
return select(expressions.map() { $0(attribute) })
4870
}
49-
71+
72+
/**
73+
Projection, i.e., select attributes of an entity rather than the entities themselves.
74+
75+
.select({$0.firstName}, {$0.lastName})
76+
*/
5077
public func select(expressions: (QueryEntityType.EntityAttributeType -> CustomPropertyConvertible)...) -> ExpressionQuery<QueryEntityType> {
5178
return select(expressions)
5279
}
5380

81+
/**
82+
Return only the distinct attributes requested in the fetch.
83+
*/
5484
public func distinct() -> ExpressionQuery<QueryEntityType> {
5585
var builder = self.builder
5686
builder.returnsDistinctResults = true
5787
return ExpressionQuery(builder: builder)
5888
}
5989

6090
/**
61-
Resets the list of selected expressions
91+
Resets the list of selected expressions
6292
*/
6393
public func reselect() -> ExpressionQuery<QueryEntityType> {
6494
var builder = self.builder

0 commit comments

Comments
 (0)