Module: Mongoid::UnitOfWork

Included in:
Mongoid
Defined in:
lib/mongoid/unit_of_work.rb

Overview

This module handles unit of work functionality with regards to the identity map.

Instance Method Summary collapse

Instance Method Details

#unit_of_work(options = {}) ⇒ Object

We can process a unit of work in Mongoid and have the identity map automatically clear itself out after the work is complete.

Examples:

Process a unit of work.

Mongoid.unit_of_work do
  Person.create(title: "Sir")
end

Process with identity map disabled on the current thread.

Mongoid.unit_of_work(disable: :current) do
  Person.create(title: "Sir")
end

Process with identity map disabled on all threads.

Mongoid.unit_of_work(disable: :all) do
  Person.create(title: "Sir")
end

Parameters:

  • options (Hash) (defaults to: {})

    The disabling options.

  • [ (Hash)

    a customizable set of options

Returns:

  • (Object)

    The result of the block.

Since:

  • 2.1.0



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mongoid/unit_of_work.rb', line 35

def unit_of_work(options = {})
  disable = options[:disable]
  begin
    Threaded.disable_identity_map(disable) if disable
    yield if block_given?
  ensure
    if disable
      Threaded.enable_identity_map(disable)
    else
      IdentityMap.clear
    end
  end
end

#using_identity_map?true, false

Are we currently using the identity map?

Examples:

Is the identity map currently enabled?

Mongoid.using_identity_map?

Returns:

  • (true, false)

    If the identity map is in use.

Since:

  • 3.0.0



57
58
59
# File 'lib/mongoid/unit_of_work.rb', line 57

def using_identity_map?
  Mongoid.identity_map_enabled? && Threaded.identity_map_enabled?
end