Module: MongoidVersionedAtomic::VAtomic::ClassMethods

Defined in:
lib/mongoid_versioned_atomic/v_atomic.rb

Instance Method Summary collapse

Instance Method Details

#after_persist(doc, instance) ⇒ Object

return [Boolean] : always returns true.



127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Array

Returns : returns the options, and update.

Returns:

  • (Array)

    : returns the options, and update.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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) ⇒ Object

converts a bson_doc to the target klass instance. return [Object] : either the document in the target class or nil.

Parameters:

  • bson_doc (BSON Document)

    : a bson_document instance

  • klass (klass)

    : klass of the target document.



41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object

logs



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# 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) ⇒ Object

Returns 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.

Returns:

  • 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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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