Class: DataMapper::IdentityMap

  • Object
    • DataMapper::IdentityMap

Tracks objects to help ensure that each object gets loaded only once. See: http://www.martinfowler.com/eaaCatalog/identityMap.html

Constructor Summary

private initialize(second_level_cache = nil)
[View source]


36
37
38
39
40
41
42
43
44
45
# File 'dm-core/lib/dm-core/identity_map.rb', line 36

def initialize(second_level_cache = nil)
  @cache = if @second_level_cache = second_level_cache



    Hash.new { |h,key| h[key] = @second_level_cache.get(key) }
  else
    Hash.new
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

private method_missing(method, *args, &block)
[View source]


48
49
50
# File 'dm-core/lib/dm-core/identity_map.rb', line 48

def method_missing(method, *args, &block)
  cache.__send__(method, *args, &block)
end

Public Visibility

Public Instance Method Summary

#delete(key)

Remove a resource from the IdentityMap.

#get(key)

Get a resource from the IdentityMap.

#set(key, resource)

Add a resource to the IdentityMap.

Public Instance Methods Inherited from Object

validatable?

Public Instance Method Details

delete

public delete(key)

Remove a resource from the IdentityMap

[View source]


27
28
29
30
31
32
# File 'dm-core/lib/dm-core/identity_map.rb', line 27

def delete(key)
  raise ArgumentError, "+key+ is not an Array, but was #{key.class}" unless Array  === key

  @second_level_cache.delete(key) if @second_level_cache
  @cache.delete(key)
end

get

public get(key)

Get a resource from the IdentityMap

[View source]


7
8
9
10
11
# File 'dm-core/lib/dm-core/identity_map.rb', line 7

def get(key)
  raise ArgumentError, "+key+ is not an Array, but was #{key.class}" unless Array  === key

  @cache[key]
end

set

public set(key, resource)

Add a resource to the IdentityMap

[View source]


16
17
18
19
20
21
22
# File 'dm-core/lib/dm-core/identity_map.rb', line 16

def set(key, resource)
  raise ArgumentError, "+key+ is not an Array, but was #{key.class}"                            unless Array  === key
  raise ArgumentError, "+resource+ should be a DataMapper::Resource, but was #{resource.class}" unless Resource === resource

  @second_level_cache.set(key, resource) if @second_level_cache
  @cache[key] = resource
end