Sha256: a770d13ab63a2861fc8e1da737fef286fe924f48ec190233b7f30c338aafe71e

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

module MongoMapper
  module Plugins
    module IdentityMap
      module ClassMethods
        def identity_map
          Thread.current[:mongo_mapper_identity_map] ||= {}
        end

        def identity_map=(v)
          Thread.current[:mongo_mapper_identity_map] = v
        end

        def identity_map_key(id)
          "#{self}:#{id}"
        end

        def find_one(options={})
          criteria, options = to_finder_options(options)
          key = identity_map_key(criteria[:_id])

          if document = identity_map[key]
            document
          else
            document = super
          end
          
          document
        end

        def load(attrs)
          id = attrs[:_id] || attrs[:id] || attrs['_id'] || attrs['id']
          key = identity_map_key(id)

          unless document = identity_map[key]
            document = super
            identity_map[document.identity_map_key] = document
          end

          document
        end
      end

      module InstanceMethods
        def identity_map_key
          @identity_map_key ||= self.class.identity_map_key(_id)
        end

        def identity_map
          self.class.identity_map
        end

        def save
          if result = super
            identity_map[identity_map_key] = self
          end
          result
        end

        def delete
          identity_map.delete(identity_map_key)
          super
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
jmonteiro-mongo_mapper-0.1.7 lib/mongo_mapper/plugins/identity_map.rb
mongo_mapper-unstable-2010.1.18 lib/mongo_mapper/plugins/identity_map.rb
jmonteiro-mongo_mapper-0.1.6 lib/mongo_mapper/plugins/identity_map.rb
jmonteiro-mongo_mapper-0.1.5 lib/mongo_mapper/plugins/identity_map.rb
mongo_mapper-unstable-2010.1.17 lib/mongo_mapper/plugins/identity_map.rb
jmonteiro-mongo_mapper-0.1.4 lib/mongo_mapper/plugins/identity_map.rb
jmonteiro-mongo_mapper-0.1.2 lib/mongo_mapper/plugins/identity_map.rb
jmonteiro-mongo_mapper-0.1.1 lib/mongo_mapper/plugins/identity_map.rb
jmonteiro-mongo_mapper-0.1.0 lib/mongo_mapper/plugins/identity_map.rb
mongo_mapper-unstable-2010.1.12 lib/mongo_mapper/plugins/identity_map.rb