-
Notifications
You must be signed in to change notification settings - Fork 3
/
Item.j
185 lines (154 loc) · 4.34 KB
/
Item.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
ItemAttachmentsDidChangeNotification = "ItemAttachmentsDidChangeNotification";
@implementation Item : CPObject
{
CPString name @accessors; //
CPString make @accessors; //
CPString model @accessors; //
CPString notes @accessors;
CPString serial @accessors;
CPDate dateAcquired @accessors; //
double price @accessors; //
CPArray attachments @accessors;
CPArray tags @accessors; //
CPDictionary extras @accessors; //
}
- (id)initWithName:(CPString)aName
{
self = [super init];
if (self)
{
name = aName;
make = "Unknown";
model = "";
notes = "";
serial = ""
dateAcquired = [CPDate date];
attachments = [ ];
tags = [ ];
extras = [CPDictionary dictionary];
}
return self;
}
- (void)addAttachment:(ItemAttachment)anAttachment
{
[attachments addObject:anAttachment];
}
- (void)removeAttachment:(ItemAttachment)anAttachment
{
[attachments removeObject:anAttachment];
[[CPNotificationCenter defaultCenter] postNotificationName:ItemAttachmentsDidChangeNotification object:self userInfo:nil];
saveAppData();
// FIX ME: delete it from the server too
}
- (void)setObject:(id)aValue forExtrasKey:(CPString)aKey
{
[extras setObject:aValue forKey:aKey];
}
- (id)valueForExtrasWithKey:(CPString)aKey
{
return [extras objectValueForKey:aKey];
}
- (BOOL)isEqual:(id)anObject
{
if ([anObject class] === [self class])
{
var one = ([name isEqual:[anObject name]] && [make isEqual:[anObject make]] && [model isEqual:[anObject model]] && [notes isEqual:[anObject notes]] && [make isEqual:[anObject make]] && price === [anObject price] && [attachments isEqual:[anObject attachments]] && [tags isEqual:[anObject tags]] && [extras isEqual:[anObject extras]]);
if (one)
{
if (dateAcquired)
return [dateAcquired isEqual:[anObject dateAcquired]];
else
return YES;
}
}
return NO;
}
- (Item)copy
{
var newItem = [[Item alloc] initWithName:name];
[newItem setMake:make];
[newItem setModel:model];
[newItem setNotes:notes];
[newItem setDateAcquired:[dateAcquired copy]];
[newItem setPrice:price];
[newItem setSerial:serial];
[newItem setAttachments:[attachments copy]];
[newItem setTags:[tags copy]];
[newItem setExtras:[extras copy]];
return newItem;
}
- (CPString)toJSON
{
var object = { };
object.name = name;
object.attachments = attachments;
object.make = make;
object.model = model;
object.price = price;
object.serial = serial;
object.notes = notes;
object.dateAcquired = [dateAcquired toJSON];
object.tags = tags;
object.extras = [extras toJSON];
return object;
}
+ (Location)decodeJSON:(Object)anObject
{
var item = [[Item alloc] initWithName:anObject.name];
[item setMake:anObject.make];
[item setModel:anObject.model];
[item setPrice:anObject.price];
[item setNotes:anObject.notes];
[item setSerial:anObject.serial];
[item setDateAcquired:[CPDate decodeJSON:anObject.dateAcquired]];
if (anObject.tags)
[item setTags:anObject.tags];
else
[item setTags:[ ]];
if (anObject.attachments)
{
var decoded = [ ];
for (var i = 0; i < anObject.attachments.length; i++)
{
var anAttachment = [ItemAttachment decodeJSON:anObject.attachments[i]];
decoded.push(anAttachment);
}
[item setAttachments:decoded];
}
else
[item setAttachments:[ ]];
[item setExtras:[CPDictionary dictionaryWithJSObject:anObject.extras]];
return item;
}
@end
@implementation CPDate (json)
- (CPString)toJSON
{
return self.getTime();
}
+ (CPDate)decodeJSON:(Object)anObject
{
self = [CPDate date];
self.setTime(anObject);
return self;
}
@end
@implementation CPDictionary (json)
- (CPString)toJSON
{
var object = { },
keys = [self allKeys],
values = [self allValues];
for (var i = 0; i < [keys count] ; i++)
object[keys[i]] = values[i];
return object;
}
@end
/*objj_class.prototype.toJSON = objj_object.prototype.toJSON = function()
{
if (this.isa && class_getInstanceMethod(this.isa, "toJSON") != NULL)
return [this toJSON];
else
return String(this) + " (-toJSON not implemented)";
}
*/