Module: PopulateMe::Mongo::Mutation
- Defined in:
- lib/populate_me/mongo/mutation.rb
Defined Under Namespace
Modules: CursorMutation, MutateClass
Constant Summary
collapse
- ACCENTS_FROM =
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞ"
- ACCENTS_TO =
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssT"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](field) ⇒ Object
-
#[]=(field, val) ⇒ Object
-
#after_create ⇒ Object
-
#after_delete ⇒ Object
(also: #after_destroy)
-
#after_save ⇒ Object
-
#after_update ⇒ Object
-
#after_validation ⇒ Object
-
#auto_slug ⇒ Object
-
#before_create ⇒ Object
-
#before_delete ⇒ Object
(also: #before_destroy)
-
#before_save ⇒ Object
-
#before_update ⇒ Object
-
#before_validation ⇒ Object
-
#children(k, opts = {}) ⇒ Object
-
#children_count(k, sel = {}) ⇒ Object
-
#default_doc ⇒ Object
-
#delete ⇒ Object
-
#errors_on(col, message = nil) ⇒ Object
Getter and setter in one.
-
#field_id_for(col) ⇒ Object
-
#first_child(k, opts = {}) ⇒ Object
-
#first_slot_child(k, opts = {}) ⇒ Object
-
#fix_type_boolean(k, v) ⇒ Object
-
#fix_type_date(k, v) ⇒ Object
-
#fix_type_datetime(k, v) ⇒ Object
-
#fix_type_integer(k, v) ⇒ Object
-
#fix_type_price(k, v) ⇒ Object
-
#fix_type_slug(k, v) ⇒ Object
-
#id ⇒ Object
-
#initialize(document = nil) ⇒ Object
-
#model ⇒ Object
-
#new? ⇒ Boolean
-
#parent(k, opts = {}) ⇒ Object
-
#resolve_class(k) ⇒ Object
-
#save ⇒ Object
-
#slot_children(k, opts = {}) ⇒ Object
-
#to_label ⇒ Object
-
#to_param ⇒ Object
To param will be deprecated Use a URL like …/<id>/<slug> instead.
-
#to_slug ⇒ Object
-
#update_doc(fields) ⇒ Object
-
#valid? ⇒ Boolean
-
#validate ⇒ Object
Instance Attribute Details
#doc ⇒ Object
100
101
102
|
# File 'lib/populate_me/mongo/mutation.rb', line 100
def doc
@doc
end
|
#errors ⇒ Object
100
101
102
|
# File 'lib/populate_me/mongo/mutation.rb', line 100
def errors
@errors
end
|
#is_new ⇒ Object
100
101
102
|
# File 'lib/populate_me/mongo/mutation.rb', line 100
def is_new
@is_new
end
|
#old_doc ⇒ Object
100
101
102
|
# File 'lib/populate_me/mongo/mutation.rb', line 100
def old_doc
@old_doc
end
|
Class Method Details
.included(weak) ⇒ Object
Most important MongoDB module It defines the ODM
9
10
11
12
13
14
|
# File 'lib/populate_me/mongo/mutation.rb', line 9
def self.included(weak)
weak.extend(MutateClass)
weak.db = DB if defined?(DB)
weak.schema = BSON::OrderedHash.new
weak.relationships = BSON::OrderedHash.new
end
|
Instance Method Details
#[](field) ⇒ Object
110
|
# File 'lib/populate_me/mongo/mutation.rb', line 110
def [](field); @doc[field]; end
|
#[]=(field, val) ⇒ Object
111
|
# File 'lib/populate_me/mongo/mutation.rb', line 111
def []=(field,val); @doc[field] = val; end
|
#after_create ⇒ Object
266
|
# File 'lib/populate_me/mongo/mutation.rb', line 266
def after_create; @is_new = false; end
|
#after_delete ⇒ Object
Also known as:
after_destroy
191
192
193
194
195
|
# File 'lib/populate_me/mongo/mutation.rb', line 191
def after_delete
model.relationships.each do |k,v|
Kernel.const_get(k).find({model.foreign_key_name=>@old_doc['_id'].to_s}).each{|m| m.delete} unless v[:independent]
end
end
|
#after_save ⇒ Object
265
|
# File 'lib/populate_me/mongo/mutation.rb', line 265
def after_save; end
|
#after_update ⇒ Object
267
|
# File 'lib/populate_me/mongo/mutation.rb', line 267
def after_update; end
|
#after_validation ⇒ Object
218
|
# File 'lib/populate_me/mongo/mutation.rb', line 218
def after_validation; end
|
#auto_slug ⇒ Object
117
118
119
120
|
# File 'lib/populate_me/mongo/mutation.rb', line 117
def auto_slug
s = self.to_label.tr(ACCENTS_FROM,ACCENTS_TO).tr(' .,;:?!/\'"()[]{}<>','-').gsub(/&/, 'and').gsub(/-+/,'-').gsub(/(^-|-$)/,'')
defined?(::Rack::Utils) ? ::Rack::Utils.escape(s) : s
end
|
#before_create ⇒ Object
263
|
# File 'lib/populate_me/mongo/mutation.rb', line 263
def before_create; end
|
#before_delete ⇒ Object
Also known as:
before_destroy
189
|
# File 'lib/populate_me/mongo/mutation.rb', line 189
def before_delete; @old_doc = @doc.dup; end
|
#before_save ⇒ Object
262
|
# File 'lib/populate_me/mongo/mutation.rb', line 262
def before_save; end
|
#before_update ⇒ Object
264
|
# File 'lib/populate_me/mongo/mutation.rb', line 264
def before_update; end
|
#before_validation ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/populate_me/mongo/mutation.rb', line 203
def before_validation
@errors = {}
@doc.each do |k,v|
next unless model.schema.key?(k)
type = k=='_id' ? :primary_key : model.schema[k][:type]
fix_method = "fix_type_#{type}"
if v==''
default = model.schema[k][:default]
@doc[k] = default.is_a?(Proc) ? default.call : default
else
self.__send__(fix_method, k, v) if self.respond_to?(fix_method)
end
end
end
|
#children(k, opts = {}) ⇒ Object
159
160
161
162
163
|
# File 'lib/populate_me/mongo/mutation.rb', line 159
def children(k,opts={})
k = resolve_class(k)
slot_name = opts.delete(:slot_name) || model.foreign_key_name
k.find({slot_name=>@doc['_id'].to_s}, opts)
end
|
#children_count(k, sel = {}) ⇒ Object
169
170
171
172
173
|
# File 'lib/populate_me/mongo/mutation.rb', line 169
def children_count(k,sel={})
k = resolve_class(k)
slot_name = sel.delete(:slot_name) || model.foreign_key_name
k.collection.count(:query => {slot_name=>@doc['_id'].to_s}.update(sel))
end
|
#default_doc ⇒ Object
102
103
104
105
106
107
|
# File 'lib/populate_me/mongo/mutation.rb', line 102
def default_doc
@is_new = true
out = {}
model.schema.each { |k,v| out.store(k,v[:default].is_a?(Proc) ? v[:default].call : v[:default]) }
out
end
|
#delete ⇒ Object
176
177
178
179
180
|
# File 'lib/populate_me/mongo/mutation.rb', line 176
def delete
before_delete
model.delete(@doc['_id'])
after_delete
end
|
#errors_on(col, message = nil) ⇒ Object
186
187
188
|
# File 'lib/populate_me/mongo/mutation.rb', line 186
def errors_on(col,message=nil)
message.nil? ? @errors[col] : @errors[col] = (@errors[col]||[]) << message
end
|
#field_id_for(col) ⇒ Object
125
|
# File 'lib/populate_me/mongo/mutation.rb', line 125
def field_id_for(col); "%s-%s-%s" % [id||'new',model.name,col]; end
|
#first_child(k, opts = {}) ⇒ Object
164
165
166
167
168
|
# File 'lib/populate_me/mongo/mutation.rb', line 164
def first_child(k,opts={})
k = resolve_class(k)
slot_name = opts.delete(:slot_name) || model.foreign_key_name
d = k.find_one({slot_name=>@doc['_id'].to_s}, opts)
end
|
#first_slot_child(k, opts = {}) ⇒ Object
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/populate_me/mongo/mutation.rb', line 149
def first_slot_child(k, opts={})
if k.kind_of?(String)
key = k
klass = resolve_class(model.schema[k][:children_class])
else
klass = resolve_class(k)
key = klass.foreign_key_name(true)
end
klass.get((@doc[key]||[])[0], opts)
end
|
#fix_type_boolean(k, v) ⇒ Object
223
|
# File 'lib/populate_me/mongo/mutation.rb', line 223
def fix_type_boolean(k,v); @doc[k] = (v=='true'||v==true) ? true : false; end
|
#fix_type_date(k, v) ⇒ Object
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/populate_me/mongo/mutation.rb', line 225
def fix_type_date(k,v)
if v.is_a?(String)
if v[/\d\d\d\d-\d\d-\d\d/]
@doc[k] = ::Time.utc(*v.split('-'))
else
default = model.schema[k][:default]
@doc[k] = default.is_a?(Proc) ? default.call : default
end
end
end
|
#fix_type_datetime(k, v) ⇒ Object
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/populate_me/mongo/mutation.rb', line 235
def fix_type_datetime(k,v)
if v.is_a?(String)
if v[/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/]
@doc[k] = ::Time.utc(*v.split(/[-:\s]/))
else
default = model.schema[k][:default]
@doc[k] = default.is_a?(Proc) ? default.call : default
end
end
end
|
#fix_type_integer(k, v) ⇒ Object
219
|
# File 'lib/populate_me/mongo/mutation.rb', line 219
def fix_type_integer(k,v); @doc[k] = v.to_i; end
|
#fix_type_price(k, v) ⇒ Object
220
221
222
|
# File 'lib/populate_me/mongo/mutation.rb', line 220
def fix_type_price(k,v)
@doc[k] = v.respond_to?(:to_price_integer) ? v.to_price_integer : v
end
|
#fix_type_slug(k, v) ⇒ Object
224
|
# File 'lib/populate_me/mongo/mutation.rb', line 224
def fix_type_slug(k,v); @doc[k] = self.auto_slug if v.to_s==''; end
|
#id ⇒ Object
109
|
# File 'lib/populate_me/mongo/mutation.rb', line 109
def id; @doc['_id']; end
|
#initialize(document = nil) ⇒ Object
101
|
# File 'lib/populate_me/mongo/mutation.rb', line 101
def initialize(document=nil); @errors={}; @doc = document || default_doc; end
|
#model ⇒ Object
108
|
# File 'lib/populate_me/mongo/mutation.rb', line 108
def model; self.class; end
|
#new? ⇒ Boolean
183
|
# File 'lib/populate_me/mongo/mutation.rb', line 183
def new?; @is_new ||= !@doc.key?('_id'); end
|
#parent(k, opts = {}) ⇒ Object
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/populate_me/mongo/mutation.rb', line 129
def parent(k, opts={})
if k.kind_of?(String)
key = k
klass = resolve_class(model.schema[k][:parent_class])
else
klass = resolve_class(k)
key = klass.foreign_key_name
end
klass.get(@doc[key], opts)
end
|
#resolve_class(k) ⇒ Object
128
|
# File 'lib/populate_me/mongo/mutation.rb', line 128
def resolve_class(k); k.kind_of?(Class) ? k : Kernel.const_get(k); end
|
#save ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/populate_me/mongo/mutation.rb', line 246
def save
return nil unless valid?
before_save
if new?
before_create
id = model.collection.insert(@doc)
@doc['_id'] = id
after_create
else
before_update
id = model.collection.update({'_id'=>@doc['_id']}, @doc)
after_update
end
after_save
id.nil? ? nil : self
end
|
#slot_children(k, opts = {}) ⇒ Object
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/populate_me/mongo/mutation.rb', line 139
def slot_children(k, opts={})
if k.kind_of?(String)
key = k
klass = resolve_class(model.schema[k][:children_class])
else
klass = resolve_class(k)
key = klass.foreign_key_name(true)
end
klass.get_multiple((@doc[key]||[]), opts)
end
|
#to_label ⇒ Object
112
|
# File 'lib/populate_me/mongo/mutation.rb', line 112
def to_label; @doc[model.label_column].to_s.tr("\n\r", ' '); end
|
#to_param ⇒ Object
To param will be deprecated Use a URL like …/<id>/<slug> instead
124
|
# File 'lib/populate_me/mongo/mutation.rb', line 124
def to_param; "#{@doc['_id']}-#{to_label.scan(/\w+/).join('-')}"; end
|
#to_slug ⇒ Object
121
|
# File 'lib/populate_me/mongo/mutation.rb', line 121
def to_slug; @doc[model.slug_column]||self.auto_slug; end
|
#update_doc(fields) ⇒ Object
184
|
# File 'lib/populate_me/mongo/mutation.rb', line 184
def update_doc(fields); @old_doc = @doc.dup; @doc.update(fields); @is_new = false; self; end
|
#valid? ⇒ Boolean
197
198
199
200
201
202
|
# File 'lib/populate_me/mongo/mutation.rb', line 197
def valid?
before_validation
validate
after_validation
@errors.empty?
end
|
#validate ⇒ Object
217
|
# File 'lib/populate_me/mongo/mutation.rb', line 217
def validate; end
|