Module: ActiveRecord::Base::ThreadIdentityMap::ClassMethods

Included in:
ActiveRecord::Base
Defined in:
lib/identity_map/cache.rb

Instance Method Summary collapse

Instance Method Details

#create_identity_mapObject

Creates new identity map and put it in thread local storage

For most use cases with_id_map should be used instead.

Usage:

ActiveRecord::Base.create_identity_map


50
51
52
# File 'lib/identity_map/cache.rb', line 50

def create_identity_map
  set_thread_id_map IdMap.new
end

#drop_identity_mapObject

Remove identity map from thread local storage

For most use cases without_id_map should be used instead.

Usage:

ActiveRecord::Base.drop_identity_map


60
61
62
# File 'lib/identity_map/cache.rb', line 60

def drop_identity_map
  set_thread_id_map nil
end

#with_id_map(fresh = true) ⇒ Object

Execute block with identity map.

If it called with true, then new instance of identity map would be used regardless of any present.

If it called with false, then it only ensures that identity map created if absent.

Default is true

Usage:

ActiveRecord::Base.with_id_map do
  #do_some_actions
end


77
78
79
80
81
82
83
# File 'lib/identity_map/cache.rb', line 77

def with_id_map( fresh = true)
  old = thread_id_map
  create_identity_map if fresh || old.nil?
  yield
ensure
  set_thread_id_map old
end

#without_id_mapObject

Execute block with identity map turned off.

Usage:

ActiveRecord::Base.without_id_map do
  #do_some_actions
end


91
92
93
94
95
96
97
# File 'lib/identity_map/cache.rb', line 91

def without_id_map
  old = thread_id_map
  drop_identity_map
  yield
ensure
  set_thread_id_map old
end