Module: MongoDoc::Document::ClassMethods
- Defined in:
- lib/mongodoc/document.rb
Instance Method Summary collapse
- #bson_create(bson_hash, options = {}) ⇒ Object
- #collection ⇒ Object
- #collection_name ⇒ Object
- #create(attrs = {}) ⇒ Object
- #create!(attrs = {}) ⇒ Object
Instance Method Details
#bson_create(bson_hash, options = {}) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/mongodoc/document.rb', line 97 def bson_create(bson_hash, = {}) new.tap do |obj| bson_hash.each do |name, value| obj.send("#{name}=", MongoDoc::BSON.decode(value, )) end end end |
#collection ⇒ Object
105 106 107 |
# File 'lib/mongodoc/document.rb', line 105 def collection @collection ||= MongoDoc::Collection.new(collection_name) end |
#collection_name ⇒ Object
109 110 111 |
# File 'lib/mongodoc/document.rb', line 109 def collection_name self.to_s.tableize.gsub('/', '.') end |
#create(attrs = {}) ⇒ Object
113 114 115 116 117 |
# File 'lib/mongodoc/document.rb', line 113 def create(attrs = {}) instance = new(attrs) _create(instance, false) if instance.valid? instance end |
#create!(attrs = {}) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/mongodoc/document.rb', line 119 def create!(attrs = {}) instance = new(attrs) raise MongoDoc::DocumentInvalidError unless instance.valid? _create(instance, true) instance end |