-
Notifications
You must be signed in to change notification settings - Fork 5
/
ISRepository.j
187 lines (153 loc) · 5.11 KB
/
ISRepository.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* ISRepository.j
* GithubIssues
*
* Created by Alexander Ljungberg on April 18, 2011.
* Copyright 2011, WireLoad Inc. All rights reserved.
*/
@import "CPArray+extensions.j"
/*!
Represent a GitHub repo.
*/
@implementation ISRepository : CPObject
{
CPString name @accessors;
CPString owner @accessors;
BOOL isPrivate @accessors;
int numberOfOpenIssues @accessors;
// TODO Come up with a better name.
int issuesAssignedToCurrentUser @accessors;
CPArray open @accessors;
CPArray closed @accessors;
CPArray collaborators;
CPArray collaboratorNames @accessors(readonly);
CPArray labels @accessors;
CPArray milestones @accessors;
}
+ (CPSet)keyPathsForValuesAffectingSidebarRepresentation
{
return [CPSet setWithObjects:"identifier", "isPrivate", "numberOfOpenIssues", "issuesAssignedToCurrentUser"];
}
+ (CPSet)keyPathsForValuesAffectingIdentifier
{
return [CPSet setWithObjects:"name", "owner"];
}
+ (id)repositoryWithJSObject:(JSObject)anObject
{
var newRepo = [ISRepository new];
[newRepo setIdentifier:( typeof anObject.owner === "string" ? anObject.owner : anObject.owner.login) + "/" + anObject.name];
[newRepo setIsPrivate:anObject["private"]];
[newRepo setNumberOfOpenIssues:anObject.open_issues];
[newRepo setIssuesAssignedToCurrentUser:0];
[newRepo setLabels:anObject['labels']];
return newRepo;
}
- (id)init
{
if (self = [super init])
{
labels = [];
}
return self;
}
- (CPArray)labels
{
return labels;
}
- (void)setLabels:(CPArray)someLabels
{
// Prevent mystery errors.
if (someLabels === nil || someLabels === undefined || ![someLabels isKindOfClass:CPArray])
[CPException raise:CPInvalidArgumentException reason:@"Labels must be an array."];
labels = someLabels;
}
- (void)setOpen:(CPArray)openIssues
{
open = openIssues;
numberOfOpenIssues = [open count];
[self setIssuesAssignedToCurrentUser:[open countUsingPredicate:[ISIssue issueAssignedToCurrentUserPredicate]]];
}
- (void)updateWithJSObject:(JSObject)anObject
{
[self setIdentifier:( typeof anObject.owner === "string" ? anObject.owner : anObject.owner.login) + "/" + anObject.name];
[self setIsPrivate:anObject["private"]];
[self setNumberOfOpenIssues:anObject.open_issues];
if (anObject.labels)
{
var labels = [],
i = 0,
c = anObject.labels.length;
for (; i < c; i++)
labels.push([ISLabel labelWithJSObject:anObject.labels[i]]);
anObject.lables = labels;
}
else
anObject.labels = [];
[self setLabels:anObject.labels];
// FIX ME: can we do this, but fast?
//[self setIssuesAssignedToCurrentUser:0];
}
- (void)addIssue:(CPDictionary)anIssue
{
[open addObject:anIssue];
[self setNumberOfOpenIssues:numberOfOpenIssues + 1];
if ([[ISIssue issueAssignedToCurrentUserPredicate] evaluateWithObject:anIssue])
[self setIssuesAssignedToCurrentUser:issuesAssignedToCurrentUser + 1];
}
- (void)setCollaborators:(CPArray)newCollabs
{
collaborators = [];
collaboratorNames = [];
for (var i = 0, c = newCollabs.length; i < c; i++)
{
collaborators.push([CPDictionary dictionaryWithJSObject:newCollabs[i] recursively:YES]);
collaboratorNames.push(newCollabs[i].login);
}
}
/*!
This is a proxy used to be able to bind the main repository sidebar column to a keypath which properly
notifies when any of the values in the compound dataview change.
*/
- (ISRepository)sidebarRepresentation
{
return self;
}
- (CPString)identifier
{
return owner + "/" + name;
}
- (void)setIdentifier:(CPString)aString
{
var pos = aString.indexOf("/");
[self setOwner:[aString substringToIndex:pos]];
[self setName:[aString substringFromIndex:pos+1]];
}
- (void)load
{
var controller = [ISGithubAPIController sharedController];
[[controller repositoriesByIdentifier] setObject:self forKey:[self identifier]];
[controller loadRepositoryWithIdentifier:[self identifier] callback:nil];
}
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super init];
[self setIdentifier:[aCoder decodeObjectForKey:"identifier"]];
isPrivate = [aCoder decodeObjectForKey:"isPrivate"];
numberOfOpenIssues = [aCoder decodeObjectForKey:"numberOfOpenIssues"];
issuesAssignedToCurrentUser = [aCoder decodeObjectForKey:"issuesAssignedToCurrentUser"];
labels = [aCoder decodeObjectForKey:"labels"] || [];
// open = [aCoder decodeObjectForKey:"open"];
// closed = [aCoder decodeObjectForKey:"closed"];
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[aCoder encodeObject:[self identifier] forKey:"identifier"];
[aCoder encodeObject:isPrivate forKey:"isPrivate"];
[aCoder encodeObject:numberOfOpenIssues forKey:"numberOfOpenIssues"];
[aCoder encodeObject:issuesAssignedToCurrentUser forKey:"issuesAssignedToCurrentUser"];
// [aCoder encodeObject:labels forKey:"labels"];
// [aCoder encodeObject:open forKey:"open"];
// [aCoder encodeObject:closed forKey:"closed"];
}
@end