module MongoidVersionedAtomic::VAtomic::ClassMethods

Public Instance Methods

after_persist(doc,instance) click to toggle source

return [Boolean] : always returns true.

# File lib/mongoid_versioned_atomic/v_atomic.rb, line 127
def after_persist(doc,instance)
                doc.keys.each do |f|
                        if f == "version"
                                instance.send("#{f}=",doc[f])
                        else
                                if instance.send("#{f}") != doc[f]
                                        instance.send("#{f}=",doc[f])
                                end
                        end
                end
        return true
end
before_persist(options,update,bypass_versioning=false) click to toggle source

@return [Array] : returns the options, and update.

# File lib/mongoid_versioned_atomic/v_atomic.rb, line 101
def before_persist(options,update,bypass_versioning=false)

        options[:return_document] = :after

        if !bypass_versioning
                      if update["$inc"].nil?
                                 update["$inc"] = {
                                         "version" => 1 }
                         else
                                 update["$inc"]["version"] = 1
                         end
                 end

                 return options,update

end
bson_to_mongoid(bson_doc,klass) click to toggle source

@param bson_doc[BSON Document] : a bson_document instance @param klass : klass of the target document. converts a bson_doc to the target klass instance. return [Object] : either the document in the target class or nil.

# File lib/mongoid_versioned_atomic/v_atomic.rb, line 41
def bson_to_mongoid(bson_doc,klass)
              if !bson_doc.nil?
                      
                      t = Mongoid::Factory.from_db(klass,bson_doc)
                      return t

              else

                      return nil

              end

       end
log_opts(query,update,options,create_or_update,log) click to toggle source

logs

# File lib/mongoid_versioned_atomic/v_atomic.rb, line 141
def log_opts(query,update,options,create_or_update,log)

        if log

                puts "doing-------------------- #{create_or_update}"


                puts "the query is :"
                puts JSON.pretty_generate(query)

                puts "the update is"
                puts JSON.pretty_generate(update)

                puts "the options are"
                puts JSON.pretty_generate(options)
        
        end

end
versioned_upsert_one(query={},update={},klass=nil,upsert=true,log=false,bypass_versioning=false) click to toggle source

@return mongoid document instance or nil(if the update hash was empty). You need to check the document to see whether it has the changes you requested.

# File lib/mongoid_versioned_atomic/v_atomic.rb, line 75
def versioned_upsert_one(query={},update={},klass=nil,upsert=true,log=false,bypass_versioning=false)
        
        options = {}

        if query.empty?
                bypass_versioning = true
        end

        options,update = before_persist(options,update,bypass_versioning)
        
                 options[:upsert] = upsert
                 if !update.empty?
                         return bson_to_mongoid(collection.find_one_and_update(query,update,options),klass)
                 end

                 return nil

end