forked from ream88/minimongoid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimongoid.coffee
More file actions
148 lines (116 loc) · 3.63 KB
/
Copy pathminimongoid.coffee
File metadata and controls
148 lines (116 loc) · 3.63 KB
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
class Minimongoid
id: undefined
attributes: {}
_saved_attributes: undefined
@fields: {}
constructor: (attributes = {}) ->
@attributes = attributes
if attributes._id?
@id = attributes._id
for field, opts of @constructor.fields
unless @attributes.hasOwnProperty field
@attributes[field] = opts.default
for field, value of @attributes
@makeProperty field
isPersisted: -> @id?
isValid: -> true
hasChanged: ->
changed = false
if @_saved_attributes
for el of @_saved_attributes
unless @_saved_attributes[el] is @attributes[el]
changed = true
return changed
else
true
resetChanges: ->
@attributes = clone(@_saved_attributes)
was: (field) ->
if field
@_saved_attributes[field]
else
@_saved_attributes
get: (field) ->
@attributes[field]
set: (field,value) ->
@attributes[field] = value
@makeProperty field
insert: (attributes) ->
$.extend(@attributes, attributes)
for field, value of attributes
@makeProperty field
this
# Add a field as a property. Note that if you assign a non-existing property
# this method will not be called and hence any assignment will not be reflected
# in the attributes.
makeProperty: (field) ->
if this[field] == undefined && field != "id"
Object.defineProperty this, field,
get: -> @get field
set: (value) ->
@set(field,value)
save: ->
return false unless @isValid()
@_saved_attributes = clone(@attributes)
attributes = @attributes
attributes['_type'] = @constructor._type if @constructor._type?
if @isPersisted()
@constructor._collection.update @id, { $set: @mongoize(attributes) }
# @constructor._collection.insert attributes
else if @attributes.id?
@attributes._id = @attributes.id
@id = @constructor._collection.insert attributes
else #if attributes.id?
# attributes._id = attributes.id
@id = @constructor._collection.insert attributes
this
update: (@attributes) ->
@save()
destroy: ->
if @isPersisted()
@constructor._collection.remove @id
@id = null
mongoize: (attributes) ->
taken = {}
for name, value of attributes
continue if name.match(/^_/)
taken[name] = value
taken
demongoize: (attributes) ->
taken = {}
for name, value of attributes
continue if name.match(/^_/)
taken[name] = value
taken
@_collection: undefined
@_type: undefined
# Rubists will love me, everyone else will burn me!
#
# Allows calls like User.new firstname: 'John'
@new: (attributes) ->
new @(attributes)
@create: (attributes) ->
@new(attributes).save()
@find: (selector = {}, options = {}) ->
@_collection.find(selector, options)
@findOne: (selector = {}, options = {}) ->
obj = @new(@_collection.findOne(selector, options))
obj._saved_attributes = clone(obj.attributes)
obj
@where: (selector = {}, options = {}) ->
@all(selector,options)
@all: (selector = {}, options = {}) ->
docs = []
for i, obj of @_collection.find(selector, options).fetch()
new_obj = @new(obj)
new_obj._saved_attributes = clone(new_obj.attributes)
docs.push(new_obj)
return docs
@toArray: (selector = {}, options = {}) ->
for attributes in @where(selector, options).fetch()
# eval is ok, because _type is never entered by user
new(eval(attributes._type) ? @)(attributes)
@count: (selector = {}, options = {}) ->
@where(selector, options).count()
@destroyAll: (selector = {}) ->
@_collection.remove(selector)