Module: Toy::IdentityMap

Extended by:
ActiveSupport::Concern
Included in:
Store
Defined in:
lib/toy/identity_map.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject



21
22
23
# File 'lib/toy/identity_map.rb', line 21

def self.clear
  repository.clear
end

.enabledObject



9
10
11
# File 'lib/toy/identity_map.rb', line 9

def self.enabled
  Thread.current[:toystore_identity_map_enabled]
end

.enabled=(flag) ⇒ Object



5
6
7
# File 'lib/toy/identity_map.rb', line 5

def self.enabled=(flag)
  Thread.current[:toystore_identity_map_enabled] = flag
end

.enabled?Boolean

Returns:



13
14
15
# File 'lib/toy/identity_map.rb', line 13

def self.enabled?
  enabled == true
end

.include?(object) ⇒ Boolean

Returns:



25
26
27
# File 'lib/toy/identity_map.rb', line 25

def self.include?(object)
  repository.keys.include?(object.id)
end

.repositoryObject



17
18
19
# File 'lib/toy/identity_map.rb', line 17

def self.repository
  Thread.current[:toystore_identity_map] ||= {}
end

.useObject



29
30
31
32
33
34
35
36
# File 'lib/toy/identity_map.rb', line 29

def self.use
  old, self.enabled = enabled, true

  yield if block_given?
ensure
  self.enabled = old
  clear
end

.withoutObject



38
39
40
41
42
43
44
# File 'lib/toy/identity_map.rb', line 38

def self.without
  old, self.enabled = enabled, false

  yield if block_given?
ensure
  self.enabled = old
end

Instance Method Details

#add_to_identity_mapObject



73
74
75
# File 'lib/toy/identity_map.rb', line 73

def add_to_identity_map
  IdentityMap.repository[id] = self if IdentityMap.enabled?
end

#deleteObject



69
70
71
# File 'lib/toy/identity_map.rb', line 69

def delete
  super.tap { remove_from_identity_map }
end

#remove_from_identity_mapObject



77
78
79
# File 'lib/toy/identity_map.rb', line 77

def remove_from_identity_map
  IdentityMap.repository.delete(id) if IdentityMap.enabled?
end

#save(options = {}) ⇒ Object



65
66
67
# File 'lib/toy/identity_map.rb', line 65

def save(options={})
  super.tap { |result| add_to_identity_map if result }
end