module MongoCollection::ClassMethods
Attributes
after_destroy_block[RW]
after_save_block[RW]
before_save_block[RW]
defaults[RW]
has_many_assosiations[RW]
has_one_assosiations[RW]
mongo_collection[RW]
props[RW]
referenced_many_assosiations[RW]
referenced_one_assosiations[RW]
references_many_assosiations[RW]
references_one_assosiations[RW]
simple_props[RW]
Public Instance Methods
after_destroy(&block)
click to toggle source
# File lib/mongo_collection.rb, line 306 def after_destroy(&block) @after_destroy = block end
after_save(&block)
click to toggle source
# File lib/mongo_collection.rb, line 302 def after_save(&block) @after_save_block = block end
all()
click to toggle source
# File lib/mongo_collection.rb, line 313 def all self.collection.find().all end
before_save(&block)
click to toggle source
# File lib/mongo_collection.rb, line 298 def before_save(&block) @before_save_block = block end
collection()
click to toggle source
# File lib/mongo_collection.rb, line 357 def collection MongoCollectionProxy.new(DB[self.mongo_collection], self) end
count(*args)
click to toggle source
# File lib/mongo_collection.rb, line 310 def count(*args) self.collection.count(*args) end
count_all_by_children_ids(fields={})
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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={})
click to toggle source
# 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)
click to toggle source
# 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={})
click to toggle source
# 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)
click to toggle source
# 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
first()
click to toggle source
# File lib/mongo_collection.rb, line 317 def first self.collection.find().first end
from_bson(bson)
click to toggle source
end
# 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)
click to toggle source
# File lib/mongo_collection.rb, line 530 def has_defaults(d) @defaults = d end
has_many(*args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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