Sha256: 8e96081d3f16295d0f5fbc4e883789e703594283b1d9ae5299822a78159ef512

Contents?: true

Size: 1.71 KB

Versions: 36

Compression:

Stored size: 1.71 KB

Contents

# encoding: utf-8
module Mongoid

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

    # We can process a unit of work in Mongoid and have the identity map
    # automatically clear itself out after the work is complete.
    #
    # @example Process a unit of work.
    #   Mongoid.unit_of_work do
    #     Person.create(title: "Sir")
    #   end
    #
    # @example Process with identity map disabled on the current thread.
    #   Mongoid.unit_of_work(disable: :current) do
    #     Person.create(title: "Sir")
    #   end
    #
    # @example Process with identity map disabled on all threads.
    #   Mongoid.unit_of_work(disable: :all) do
    #     Person.create(title: "Sir")
    #   end
    #
    # @param [ Hash ] options The disabling options.
    #
    # @option [ Symbol ] :disable Either :all or :current to indicate whether
    #   to temporarily disable the identity map on the current thread or all
    #   threads.
    #
    # @return [ Object ] The result of the block.
    #
    # @since 2.1.0
    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

    # Are we currently using the identity map?
    #
    # @example Is the identity map currently enabled?
    #   Mongoid.using_identity_map?
    #
    # @return [ true, false ] If the identity map is in use.
    #
    # @since 3.0.0
    def using_identity_map?
      Mongoid.identity_map_enabled? && Threaded.identity_map_enabled?
    end
  end
end

Version data entries

36 entries across 36 versions & 5 rubygems

Version Path
mongoid-3.1.7 lib/mongoid/unit_of_work.rb
mongoid-3.1.6 lib/mongoid/unit_of_work.rb
sepastian-mongoid-rails4-4.0.1.alpha lib/mongoid/unit_of_work.rb
sepastian-mongoid-rails4-4.0.0.alpha lib/mongoid/unit_of_work.rb
mongoid-3.1.5 lib/mongoid/unit_of_work.rb
mongoid_heroku_stable-4.0.0 lib/mongoid/unit_of_work.rb
mongoid_rails4-4.0.0 lib/mongoid/unit_of_work.rb
mongoid-3.1.4 lib/mongoid/unit_of_work.rb
mongoid-3.1.3 lib/mongoid/unit_of_work.rb
mongoid-3.1.2 lib/mongoid/unit_of_work.rb
mongoid-3.0.23 lib/mongoid/unit_of_work.rb
mongoid-3.1.1 lib/mongoid/unit_of_work.rb
mongoid-3.1.0 lib/mongoid/unit_of_work.rb
mongoid-3.0.22 lib/mongoid/unit_of_work.rb
mongoid-3.0.21 lib/mongoid/unit_of_work.rb
mongoid-3.0.20 lib/mongoid/unit_of_work.rb
mongoid-3.0.19 lib/mongoid/unit_of_work.rb
mongoid-3.0.18 lib/mongoid/unit_of_work.rb
mongoid-3.0.17 lib/mongoid/unit_of_work.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.16/lib/mongoid/unit_of_work.rb