Module: MongoDoc::Document::ClassMethods

Defined in:
lib/mongo_doc/document.rb

Instance Method Summary collapse

Instance Method Details

#bson_create(bson_hash, options = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mongo_doc/document.rb', line 131

def bson_create(bson_hash, options = {})
  allocate.tap do |obj|
    bson_hash.each do |name, value|
      if MongoDoc::Configuration.dynamic_attributes
        if _attributes.include?(name.to_sym) || name == '_id'
          obj.send("#{name}=", MongoDoc::BSON.decode(value, options))
        else
          obj.send(:dynamic_attributes)[name] = MongoDoc::BSON.decode(value, options)
          obj.singleton_class.module_eval ("def #{name}; dynamic_attributes[#{name.inspect}]; end")
        end
      else
        obj.send("#{name}=", MongoDoc::BSON.decode(value, options))
      end
    end
  end
end

#collectionObject



148
149
150
# File 'lib/mongo_doc/document.rb', line 148

def collection
  @collection ||= MongoDoc::Collection.new(collection_name)
end

#create(attrs = {}) ⇒ Object



152
153
154
155
156
# File 'lib/mongo_doc/document.rb', line 152

def create(attrs = {})
  instance = new(attrs)
  instance.save(true)
  instance
end

#create!(attrs = {}) ⇒ Object



158
159
160
161
162
# File 'lib/mongo_doc/document.rb', line 158

def create!(attrs = {})
  instance = new(attrs)
  instance.save!
  instance
end