Module: MongoMutation
- Defined in:
- lib/mongo_mutation.rb
Defined Under Namespace
Modules: CursorMutation, MutateClass
Constant Summary
collapse
- ACCENTS_FROM =
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž"
- ACCENTS_TO =
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
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_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_slug ⇒ Object
-
#update_doc(fields) ⇒ Object
-
#valid? ⇒ Boolean
-
#validate ⇒ Object
Instance Attribute Details
#doc ⇒ Object
Returns the value of attribute doc.
88
89
90
|
# File 'lib/mongo_mutation.rb', line 88
def doc
@doc
end
|
#errors ⇒ Object
Returns the value of attribute errors.
88
89
90
|
# File 'lib/mongo_mutation.rb', line 88
def errors
@errors
end
|
#is_new ⇒ Object
Returns the value of attribute is_new.
88
89
90
|
# File 'lib/mongo_mutation.rb', line 88
def is_new
@is_new
end
|
#old_doc ⇒ Object
Returns the value of attribute old_doc.
88
89
90
|
# File 'lib/mongo_mutation.rb', line 88
def old_doc
@old_doc
end
|
Class Method Details
.included(weak) ⇒ Object
7
8
9
10
11
12
|
# File 'lib/mongo_mutation.rb', line 7
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
98
|
# File 'lib/mongo_mutation.rb', line 98
def [](field); @doc[field]; end
|
#[]=(field, val) ⇒ Object
99
|
# File 'lib/mongo_mutation.rb', line 99
def []=(field,val); @doc[field] = val; end
|
#after_create ⇒ Object
250
|
# File 'lib/mongo_mutation.rb', line 250
def after_create; @is_new = false; end
|
#after_delete ⇒ Object
Also known as:
after_destroy
178
179
180
181
182
|
# File 'lib/mongo_mutation.rb', line 178
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
249
|
# File 'lib/mongo_mutation.rb', line 249
def after_save; end
|
#after_update ⇒ Object
251
|
# File 'lib/mongo_mutation.rb', line 251
def after_update; end
|
#after_validation ⇒ Object
205
|
# File 'lib/mongo_mutation.rb', line 205
def after_validation; end
|
#auto_slug ⇒ Object
103
104
105
106
|
# File 'lib/mongo_mutation.rb', line 103
def auto_slug
s = self.to_label.tr(ACCENTS_FROM,ACCENTS_TO).tr(' .,;:?!/\'"()[]{}<>','-').gsub(/&/, 'and')
defined?(::Rack::Utils) ? ::Rack::Utils.escape(s) : s
end
|
#before_create ⇒ Object
247
|
# File 'lib/mongo_mutation.rb', line 247
def before_create; end
|
#before_delete ⇒ Object
Also known as:
before_destroy
176
|
# File 'lib/mongo_mutation.rb', line 176
def before_delete; @old_doc = @doc.dup; end
|
#before_save ⇒ Object
246
|
# File 'lib/mongo_mutation.rb', line 246
def before_save; end
|
#before_update ⇒ Object
248
|
# File 'lib/mongo_mutation.rb', line 248
def before_update; end
|
#before_validation ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/mongo_mutation.rb', line 190
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
146
147
148
149
150
|
# File 'lib/mongo_mutation.rb', line 146
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
156
157
158
159
160
|
# File 'lib/mongo_mutation.rb', line 156
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
90
91
92
93
94
95
|
# File 'lib/mongo_mutation.rb', line 90
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
163
164
165
166
167
|
# File 'lib/mongo_mutation.rb', line 163
def delete
before_delete
model.delete(@doc['_id'])
after_delete
end
|
#errors_on(col, message = nil) ⇒ Object
173
174
175
|
# File 'lib/mongo_mutation.rb', line 173
def errors_on(col,message=nil)
message.nil? ? @errors[col] : @errors[col] = (@errors[col]||[]) << message
end
|
#field_id_for(col) ⇒ Object
109
|
# File 'lib/mongo_mutation.rb', line 109
def field_id_for(col); "%s-%s-%s" % [id||'new',model.name,col]; end
|
#first_child(k, opts = {}) ⇒ Object
151
152
153
154
155
|
# File 'lib/mongo_mutation.rb', line 151
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
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/mongo_mutation.rb', line 136
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
207
|
# File 'lib/mongo_mutation.rb', line 207
def fix_type_boolean(k,v); @doc[k] = (v=='true'||v==true) ? true : false; end
|
#fix_type_date(k, v) ⇒ Object
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/mongo_mutation.rb', line 209
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
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/mongo_mutation.rb', line 219
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
206
|
# File 'lib/mongo_mutation.rb', line 206
def fix_type_integer(k,v); @doc[k] = v.to_i; end
|
#fix_type_slug(k, v) ⇒ Object
208
|
# File 'lib/mongo_mutation.rb', line 208
def fix_type_slug(k,v); @doc[k] = self.auto_slug if v.to_s==''; end
|
#id ⇒ Object
97
|
# File 'lib/mongo_mutation.rb', line 97
def id; @doc['_id']; end
|
#initialize(document = nil) ⇒ Object
89
|
# File 'lib/mongo_mutation.rb', line 89
def initialize(document=nil); @errors={}; @doc = document || default_doc; end
|
#model ⇒ Object
96
|
# File 'lib/mongo_mutation.rb', line 96
def model; self.class; end
|
#new? ⇒ Boolean
170
|
# File 'lib/mongo_mutation.rb', line 170
def new?; @is_new ||= !@doc.key?('_id'); end
|
#parent(k, opts = {}) ⇒ Object
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/mongo_mutation.rb', line 113
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
112
|
# File 'lib/mongo_mutation.rb', line 112
def resolve_class(k); k.kind_of?(Class) ? k : Kernel.const_get(k); end
|
#save ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/mongo_mutation.rb', line 230
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
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/mongo_mutation.rb', line 123
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
ids = (@doc[key]||[]).map{|i| BSON::ObjectId.from_string(i) }
selector = {'_id'=>{'$in'=>ids}}
sort_proc = proc{ |a,b| ids.index(a['_id'])<=>ids.index(b['_id']) }
klass.find(selector, opts).to_a.sort(&sort_proc)
end
|
#to_label ⇒ Object
100
|
# File 'lib/mongo_mutation.rb', line 100
def to_label; @doc[model.label_column].to_s.tr("\n\r", ' '); end
|
#to_param ⇒ Object
108
|
# File 'lib/mongo_mutation.rb', line 108
def to_param; "#{@doc['_id']}-#{to_label.scan(/\w+/).join('-')}"; end
|
#to_slug ⇒ Object
107
|
# File 'lib/mongo_mutation.rb', line 107
def to_slug; @doc[model.column_slug]||self.auto_slug; end
|
#update_doc(fields) ⇒ Object
171
|
# File 'lib/mongo_mutation.rb', line 171
def update_doc(fields); @old_doc = @doc.dup; @doc.update(fields); @is_new = false; self; end
|
#valid? ⇒ Boolean
184
185
186
187
188
189
|
# File 'lib/mongo_mutation.rb', line 184
def valid?
before_validation
validate
after_validation
@errors.empty?
end
|
#validate ⇒ Object
204
|
# File 'lib/mongo_mutation.rb', line 204
def validate; end
|