Sha256: e971343c5df4eced7189a7d9a19795b8635e6129465414238e1a4e969ebd8957

Contents?: true

Size: 1.27 KB

Versions: 19

Compression:

Stored size: 1.27 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.has_key?(doc.id.to_s)
          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

19 entries across 19 versions & 1 rubygems

Version Path
mongoo-0.5.7 lib/mongoo/identity_map.rb
mongoo-0.5.6 lib/mongoo/identity_map.rb
mongoo-0.5.5 lib/mongoo/identity_map.rb
mongoo-0.5.4 lib/mongoo/identity_map.rb
mongoo-0.5.3 lib/mongoo/identity_map.rb
mongoo-0.5.2 lib/mongoo/identity_map.rb
mongoo-0.5.1 lib/mongoo/identity_map.rb
mongoo-0.5.0 lib/mongoo/identity_map.rb
mongoo-0.4.10 lib/mongoo/identity_map.rb
mongoo-0.4.9 lib/mongoo/identity_map.rb
mongoo-0.4.8 lib/mongoo/identity_map.rb
mongoo-0.4.7 lib/mongoo/identity_map.rb
mongoo-0.4.6 lib/mongoo/identity_map.rb
mongoo-0.4.5 lib/mongoo/identity_map.rb
mongoo-0.4.4 lib/mongoo/identity_map.rb
mongoo-0.4.3 lib/mongoo/identity_map.rb
mongoo-0.4.2 lib/mongoo/identity_map.rb
mongoo-0.4.1 lib/mongoo/identity_map.rb
mongoo-0.4.0 lib/mongoo/identity_map.rb