Module: MongoCollection::ClassMethods

Defined in:
lib/mongo_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_destroy_blockObject

Returns the value of attribute after_destroy_block.



253
254
255
# File 'lib/mongo_collection.rb', line 253

def after_destroy_block
  @after_destroy_block
end

#after_save_blockObject

Returns the value of attribute after_save_block.



252
253
254
# File 'lib/mongo_collection.rb', line 252

def after_save_block
  @after_save_block
end

#before_save_blockObject

Returns the value of attribute before_save_block.



251
252
253
# File 'lib/mongo_collection.rb', line 251

def before_save_block
  @before_save_block
end

#defaultsObject

Returns the value of attribute defaults.



256
257
258
# File 'lib/mongo_collection.rb', line 256

def defaults
  @defaults
end

#has_many_assosiationsObject

Returns the value of attribute has_many_assosiations.



250
251
252
# File 'lib/mongo_collection.rb', line 250

def has_many_assosiations
  @has_many_assosiations
end

#has_one_assosiationsObject

Returns the value of attribute has_one_assosiations.



249
250
251
# File 'lib/mongo_collection.rb', line 249

def has_one_assosiations
  @has_one_assosiations
end

#mongo_collectionObject

Returns the value of attribute mongo_collection.



246
247
248
# File 'lib/mongo_collection.rb', line 246

def mongo_collection
  @mongo_collection
end

#propsObject

Returns the value of attribute props.



247
248
249
# File 'lib/mongo_collection.rb', line 247

def props
  @props
end

#referenced_many_assosiationsObject

Returns the value of attribute referenced_many_assosiations.



255
256
257
# File 'lib/mongo_collection.rb', line 255

def referenced_many_assosiations
  @referenced_many_assosiations
end

#referenced_one_assosiationsObject

Returns the value of attribute referenced_one_assosiations.



255
256
257
# File 'lib/mongo_collection.rb', line 255

def referenced_one_assosiations
  @referenced_one_assosiations
end

#references_many_assosiationsObject

Returns the value of attribute references_many_assosiations.



254
255
256
# File 'lib/mongo_collection.rb', line 254

def references_many_assosiations
  @references_many_assosiations
end

#references_one_assosiationsObject

Returns the value of attribute references_one_assosiations.



254
255
256
# File 'lib/mongo_collection.rb', line 254

def references_one_assosiations
  @references_one_assosiations
end

#simple_propsObject

Returns the value of attribute simple_props.



248
249
250
# File 'lib/mongo_collection.rb', line 248

def simple_props
  @simple_props
end

Instance Method Details

#after_destroy(&block) ⇒ Object



306
307
308
# File 'lib/mongo_collection.rb', line 306

def after_destroy(&block)
    @after_destroy = block
end

#after_save(&block) ⇒ Object



302
303
304
# File 'lib/mongo_collection.rb', line 302

def after_save(&block)
    @after_save_block = block
end

#allObject



313
314
315
# File 'lib/mongo_collection.rb', line 313

def all 
    self.collection.find().all
end

#before_save(&block) ⇒ Object



298
299
300
# File 'lib/mongo_collection.rb', line 298

def before_save(&block)
    @before_save_block = block
end

#collectionObject



357
358
359
# File 'lib/mongo_collection.rb', line 357

def collection
    MongoCollectionProxy.new(DB[self.mongo_collection], self)
end

#count(*args) ⇒ Object



310
311
312
# File 'lib/mongo_collection.rb', line 310

def count(*args)
    self.collection.count(*args)               
end

#count_all_by_children_ids(fields = {}) ⇒ Object



342
343
344
345
# File 'lib/mongo_collection.rb', line 342

def count_all_by_children_ids(fields={})                
    query = Hash[fields.map{|k,_| [k.to_s+"._id",_]}]
    self.collection.find(query).count
end

#db(&block) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/mongo_collection.rb', line 361

def db(&block)
    collection = DB[self.mongo_collection]
    result = block.call(collection)
    if result.is_a?(Array) || result.is_a?(Mongo::Collection::View)                    
        return result.map{|bson| self.from_bson(bson)}                
    end

    if result.is_a?(Fixnum)
        return result
    end
    return self.from_bson(result)     
end

#find_all(limit = nil, sort = nil, direction = 1) ⇒ Object



321
322
323
324
325
326
327
328
329
330
# File 'lib/mongo_collection.rb', line 321

def find_all(limit=nil, sort=nil, direction=1)
    req = DB[self.mongo_collection].find()
    if sort
        req = req.sort({sort.to_sym => direction})
    end
    if limit 
        req = req.limit(limit)
    end
    req.map{|bson| self.from_bson(bson)}
end

#find_all_by_children_ids(fields = {}) ⇒ Object



332
333
334
335
# File 'lib/mongo_collection.rb', line 332

def find_all_by_children_ids(fields={})                
    query = Hash[fields.map{|k,_| [k.to_s+"._id",_]}]
    self.collection.find(query).all
end

#find_by_id(id) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mongo_collection.rb', line 284

def find_by_id(id)
    if id.is_a?(String)
        id = BSON::ObjectId(id)
    end

    bson = DB[mongo_collection].find(_id: id).limit(1).first

    if !bson 
        return nil
    end

    self.from_bson(bson) 
end

#find_one_by_children_ids(fields = {}) ⇒ Object



337
338
339
340
# File 'lib/mongo_collection.rb', line 337

def find_one_by_children_ids(fields={})                
    query = Hash[fields.map{|k,_| [k.to_s+"._id",_]}]
    self.collection.find(query).first
end

#find_or_create_by_name(name) ⇒ Object



347
348
349
350
351
352
353
354
355
# File 'lib/mongo_collection.rb', line 347

def find_or_create_by_name(name)
    existing_obj = self.find_by_name(name)
    if existing_obj
      return existing_obj 
    end            
    new_obj = self.new(name: name)
    new_obj.save 
    new_obj
end

#firstObject



317
318
319
# File 'lib/mongo_collection.rb', line 317

def first
    self.collection.find().first
end

#from_bson(bson) ⇒ Object

end



518
519
520
521
# File 'lib/mongo_collection.rb', line 518

def from_bson(bson)
    bson["_id"] = bson["_id"].to_s            
    return self.new(bson.to_h)              
end

#has_defaults(d) ⇒ Object



530
531
532
# File 'lib/mongo_collection.rb', line 530

def has_defaults(d)
    @defaults = d
end

#has_many(*args) ⇒ Object



395
396
397
398
399
400
401
# File 'lib/mongo_collection.rb', line 395

def has_many(*args) 
    assosiation, class_name = args
    @props << assosiation
    class_name ||= assosiation.to_s.sub(/s$/, "").camelize            
    self.has_many_assosiations[assosiation.to_sym] = class_name
    instance_eval { attr_accessor assosiation}
end

#has_mongo_collection(*args) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/mongo_collection.rb', line 258

def has_mongo_collection (*args)
    
    class_eval <<-EOV
    include MongoCollection::InstanceMethods
    EOV

    collection = args[0]
    properties = args[1..-1]

    @props = [:_id] + properties
    
    @simple_props = @props.clone
    @has_one_assosiations = {}
    @has_many_assosiations = {}
    @references_one_assosiations = {}
    @references_many_assosiations = {}
    @referenced_one_assosiations = {} 
    @referenced_many_assosiations = {}            
    @before_save_block = nil
    @after_save_block = nil
    @after_destroy_block = nil
    @defaults = {}
    instance_eval { attr_accessor *@props }                                                
                                        
    @mongo_collection = collection
    
    def find_by_id(id)
        if id.is_a?(String)
            id = BSON::ObjectId(id)
        end

        bson = DB[mongo_collection].find(_id: id).limit(1).first

        if !bson 
            return nil
        end

        self.from_bson(bson) 
    end

    def before_save(&block)
        @before_save_block = block
    end
    
    def after_save(&block)
        @after_save_block = block
    end
    
    def after_destroy(&block)
        @after_destroy = block
    end

    def count(*args)
        self.collection.count(*args)               
    end
    def all 
        self.collection.find().all
    end

    def first
        self.collection.find().first
    end

    def find_all(limit=nil, sort=nil, direction=1)
        req = DB[self.mongo_collection].find()
        if sort
            req = req.sort({sort.to_sym => direction})
        end
        if limit 
            req = req.limit(limit)
        end
        req.map{|bson| self.from_bson(bson)}
    end

    def find_all_by_children_ids(fields={})                
        query = Hash[fields.map{|k,_| [k.to_s+"._id",_]}]
        self.collection.find(query).all
    end

    def find_one_by_children_ids(fields={})                
        query = Hash[fields.map{|k,_| [k.to_s+"._id",_]}]
        self.collection.find(query).first
    end

    def count_all_by_children_ids(fields={})                
        query = Hash[fields.map{|k,_| [k.to_s+"._id",_]}]
        self.collection.find(query).count
    end

    def find_or_create_by_name(name)
        existing_obj = self.find_by_name(name)
        if existing_obj
          return existing_obj 
        end            
        new_obj = self.new(name: name)
        new_obj.save 
        new_obj
    end

    def collection
        MongoCollectionProxy.new(DB[self.mongo_collection], self)
    end

    def db(&block)
        collection = DB[self.mongo_collection]
        result = block.call(collection)
        if result.is_a?(Array) || result.is_a?(Mongo::Collection::View)                    
            return result.map{|bson| self.from_bson(bson)}                
        end

        if result.is_a?(Fixnum)
            return result
        end
        return self.from_bson(result)     
    end            

    properties.each do |property|
        method = "find_by_" + property.to_s
        metaclass.send(:define_method, method) do |query|
            bson = DB[self.mongo_collection].find({property.to_sym => query}).limit(1).first 
            if bson
                return self.from_bson(bson)
            end
        end
    end


end

#has_one(*args) ⇒ Object



387
388
389
390
391
392
393
# File 'lib/mongo_collection.rb', line 387

def has_one(*args)  
    assosiation, class_name = args
    @props << assosiation
    class_name ||= assosiation.to_s.camelize
    self.has_one_assosiations[assosiation.to_sym] = class_name            
    instance_eval { attr_accessor assosiation}                                                                             
end

#to_bson(item) ⇒ Object



523
524
525
526
527
528
529
# File 'lib/mongo_collection.rb', line 523

def to_bson(item)        
    bson = item.to_h
    if !bson[:_id].blank?
        bson[:_id] = BSON::ObjectId(item._id)  
    end        
    bson
end