Sha256: 8e6254b70a73dd8298f5e24449f90c3f38efbba7d1ba544536c84d05078bcdc8

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 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 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

3 entries across 3 versions & 1 rubygems

Version Path
mongoo-0.2.2 lib/mongoo/identity_map.rb
mongoo-0.2.1 lib/mongoo/identity_map.rb
mongoo-0.2.0 lib/mongoo/identity_map.rb