-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.coffee
94 lines (71 loc) · 2.09 KB
/
model.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
egg.model = (klass)->
klass.init (opts)->
@_attrs = opts.attrs || {}
klass.instances().add(@)
klass.emit 'add', instance: @
klass.destroy (opts)->
klass.instances().remove(@)
klass.emit 'remove', instance: @
klass.extend
instances: ->
@classInstanceVars().instances ?= new egg.Set
loadFrom: (storage, opts={})->
storage.load(@, opts).done (instances) =>
@emit('load', from: storage, instances: instances, opts: opts)
filter: (callback)->
set = new egg.Set
@instances().forEach (instance)=> set.add instance if callback(instance)
set
where: (attrs)->
index = egg.Index.for(@, Object.keys(attrs))
if index
index.where(attrs)
else
@filter (model)->
for key, value of attrs
return false if model.get(key) != value
return true
find: (attrs)->
@where(attrs).one()
findOrCreate: (attrs)->
@find(attrs) || @create(attrs: attrs)
all: -> @classInstanceVars().all ?= egg.Scope.create(parent: @)
destroyAll: ->
@instances().forEach (model)-> model.destroy()
index: (attrNames...)->
egg.Index.create(modelClass: @, attrNames: attrNames)
count: ->
@all().count()
sample: (attr)->
@all().sample(attr)
orderBy: (attr)->
@all().orderBy(attr)
klass.include
get: (attr)->
@_attrs[attr]
attrs: (keys...)->
if keys.length
Object.slice(@_attrs, keys)
else
Object.extend({}, @_attrs)
set: (args...)->
from = @attrs()
if args.length == 1
for attr, value of args[0]
@setOne(attr, value)
else
[attr, value] = args
@setOne(attr, value)
to = @attrs()
@emit 'change', instance: @, from: from, to: to
setOne: (attr, value)->
from = @get(attr)
@_attrs[attr] = value
@emit "change.#{attr}", instance: @, from: from, to: value
update: (args...)->
@set(args...)
@save()
save: ->
@emit('save', instance: @)
toJSON: ->
Object.extend {}, @_attrs