Sha256: a41f3a89852ad70ee78eb83c35b23c26febfa75cbb8357f2f3eda88a45560c11

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module Mongoo
  class IdentityMap

    class << self

      def on!
        @on = true
      end

      def on?
        @on == true
      end

      def off!
        @on = false
        if Thread.current[:mongoo]
          Thread.current[:mongoo][:identity_map] = nil
        end; true
      end

      def off?
        @on == false
      end

      def store
        return nil unless on?
        Thread.current[:mongoo] ||= {}
        Thread.current[:mongoo][:identity_map] ||= {}
        Thread.current[:mongoo][:identity_map][:store] ||= {}
      end

      def simple_query?(query, opts)
        return false if query.nil?
        return false unless opts.blank?
        return true if query.is_a?(BSON::ObjectId)
        return true if [[:_id], ["_id"]].include?(query.keys)
        false
      end

      def read(id)
        if store
          if id.is_a?(BSON::ObjectId)
            store[id.to_s]
          elsif id.is_a?(Hash)
            store[(id[:_id] || id["_id"]).to_s]
          end
        end
      end

      def write(doc)
        if store
          store[doc.id.to_s] = doc
        end
      end

      def flush!
        if store
          Thread.current[:mongoo][:identity_map][:store] = {}
          true
        end
      end

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoo-0.3.1 lib/mongoo/identity_map.rb
mongoo-0.3.0 lib/mongoo/identity_map.rb
mongoo-0.2.4 lib/mongoo/identity_map.rb
mongoo-0.2.3 lib/mongoo/identity_map.rb