-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_context.coffee
80 lines (65 loc) · 1.88 KB
/
app_context.coffee
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
String.prototype.underscorize = ->
@replace /([A-Z])/g, (letter) -> "_#{letter.toLowerCase()}".substr(1)
class AppContext
constructor: ->
@_models = {}
exists: (uri) ->
model = @_modelForURI(uri)
!!model.exists(uri.id)
updateOrCreate: (uri, data) ->
if @exists(uri)
@update uri, data
else
@create uri, data
create: (uri, data) ->
model = @_modelForURI(uri)
record = new model(data)
record.id = uri.id if uri.id?
record.save()
uri.id = record.id
model.fetch()
record
update: (uri, data) ->
record = @objectAtURI(uri)
record.updateAttributes(data)
record
changeID: (uri, id) ->
record = @objectAtURI(uri)
console.log "changing id from #{record.id} to #{id}"
record.changeID(id)
relation: (name, sourceURI, targetURI) ->
source = @objectAtURI(sourceURI)
target = @objectAtURI(targetURI)
hash = {}
hash[name] = target
source.updateAttributes(hash)
source.save()
objectAtURI: (uri) ->
model = @_modelForURI(uri)
model.find(uri.id)
dataForURI: (uri) ->
@dataForObject(@objectAtURI(uri))
dataForObject: (object) ->
object.attributes()
_modelForURI: (uri) ->
model = @_models[uri.collection]
unless model
console.log "Initializing model", uri.collection
model = require("models/#{uri.collection.underscorize()}")
model.fetch()
@_models[uri.collection] = model
model
objectURI: (object) ->
{collection: object.constructor.className, id: object.id}
allURIs: (collection, predicate) ->
uri = {collection:collection}
model = @_modelForURI(uri)
# model.fetch() # TODO: Fetch maybe?
objects = if predicate?
model.select(predicate)
else
model.all()
@objectURI(object) for object in objects
destroy: (uri) ->
@objectAtURI(uri).destroy()
module.exports = AppContext