forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 22
/
modifiable.rb
209 lines (196 loc) · 6.83 KB
/
modifiable.rb
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# encoding: utf-8
module Mongoid
class Criteria
module Modifiable
# Build a document given the selector and return it.
# Complex criteria, such as $in and $or operations will get ignored.
#
# @example build the document.
# Person.where(:title => "Sir").build
#
# @example Build with selectors getting ignored.
# Person.where(:age.gt => 5).build
#
# @return [ Document ] A non-persisted document.
#
# @since 2.0.0
def build(attrs = {}, &block)
create_document(:new, attrs, &block)
end
alias :new :build
# Create a document in the database given the selector and return it.
# Complex criteria, such as $in and $or operations will get ignored.
#
# @example Create the document.
# Person.where(:title => "Sir").create
#
# @example Create with selectors getting ignored.
# Person.where(:age.gt => 5).create
#
# @return [ Document ] A newly created document.
#
# @since 2.0.0.rc.1
def create(attrs = {}, &block)
create_document(:create, attrs, &block)
end
# Create a document in the database given the selector and return it.
# Complex criteria, such as $in and $or operations will get ignored.
# If validation fails, an error will be raised.
#
# @example Create the document.
# Person.where(:title => "Sir").create
#
# @example Create with selectors getting ignored.
# Person.where(:age.gt => 5).create
#
# @raise [ Errors::Validations ] on a validation error.
#
# @return [ Document ] A newly created document.
#
# @since 3.0.0
def create!(attrs = {}, &block)
create_document(:create!, attrs, &block)
end
# Find the first +Document+ given the conditions, or creates a new document
# with the conditions that were supplied.
#
# @example Find or create the document.
# Person.find_or_create_by(:attribute => "value")
#
# @param [ Hash ] attrs The attributes to check.
#
# @return [ Document ] A matching or newly created document.
def find_or_create_by(attrs = {}, &block)
find_or(:create, attrs, &block)
end
# Find the first +Document+ given the conditions, or creates a new document
# with the conditions that were supplied. If validation fails an
# exception will be raised.
#
# @example Find or create the document.
# Person.find_or_create_by!(:attribute => "value")
#
# @param [ Hash ] attrs The attributes to check.
#
# @raise [ Errors::Validations ] on validation error.
#
# @return [ Document ] A matching or newly created document.
def find_or_create_by!(attrs = {}, &block)
find_or(:create!, attrs, &block)
end
# Find the first +Document+ given the conditions, or initializes a new document
# with the conditions that were supplied.
#
# @example Find or initialize the document.
# Person.find_or_initialize_by(:attribute => "value")
#
# @param [ Hash ] attrs The attributes to check.
#
# @return [ Document ] A matching or newly initialized document.
def find_or_initialize_by(attrs = {}, &block)
find_or(:new, attrs, &block)
end
# Find the first +Document+, or creates a new document
# with the conditions that were supplied plus attributes.
#
# @example First or create the document.
# Person.where(name: "Jon").first_or_create(attribute: "value")
#
# @param [ Hash ] attrs The additional attributes to add.
#
# @return [ Document ] A matching or newly created document.
#
# @since 3.1.0
def first_or_create(attrs = nil, &block)
first_or(:create, attrs, &block)
end
# Find the first +Document+, or creates a new document
# with the conditions that were supplied plus attributes and will
# raise an error if validation fails.
#
# @example First or create the document.
# Person.where(name: "Jon").first_or_create!(attribute: "value")
#
# @param [ Hash ] attrs The additional attributes to add.
#
# @return [ Document ] A matching or newly created document.
#
# @since 3.1.0
def first_or_create!(attrs = nil, &block)
first_or(:create!, attrs, &block)
end
# Find the first +Document+, or initializes a new document
# with the conditions that were supplied plus attributes.
#
# @example First or initialize the document.
# Person.where(name: "Jon").first_or_initialize(attribute: "value")
#
# @param [ Hash ] attrs The additional attributes to add.
#
# @return [ Document ] A matching or newly initialized document.
#
# @since 3.1.0
def first_or_initialize(attrs = nil, &block)
first_or(:new, attrs, &block)
end
private
# Create a document given the provided method and attributes from the
# existing selector.
#
# @api private
#
# @example Create a new document.
# criteria.create_document(:new, {})
#
# @param [ Symbol ] method Either :new or :create.
# @param [ Hash ] attrs Additional attributes to use.
#
# @return [ Document ] The new or saved document.
#
# @since 3.0.0
def create_document(method, attrs = nil, &block)
attributes = selector.reduce(attrs || {}) do |hash, (key, value)|
unless key.to_s =~ /\$/ || value.is_a?(Hash)
hash[key] = value
end
hash
end
if embedded?
attributes[:_parent] = parent_document
attributes[:__metadata] = metadata
end
klass.__send__(method, attributes, &block)
end
# Find the first object or create/initialize it.
#
# @api private
#
# @example Find or perform an action.
# Person.find_or(:create, :name => "Dev")
#
# @param [ Symbol ] method The method to invoke.
# @param [ Hash ] attrs The attributes to query or set.
#
# @return [ Document ] The first or new document.
def find_or(method, attrs = {}, &block)
where(attrs).first || send(method, attrs, &block)
end
# Find the first document or create/initialize it.
#
# @api private
#
# @example First or perform an action.
# Person.first_or(:create, :name => "Dev")
#
# @param [ Symbol ] method The method to invoke.
# @param [ Hash ] attrs The attributes to query or set.
#
# @return [ Document ] The first or new document.
#
# @since 3.1.0
def first_or(method, attrs = {}, &block)
first || create_document(method, attrs, &block)
end
end
end
end