Module: Croesus::IdentityMap

Included in:
Croesus
Defined in:
lib/croesus/identity_map.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject



38
39
40
# File 'lib/croesus/identity_map.rb', line 38

def self.clear
  repository.clear
end

.enabledObject



26
27
28
# File 'lib/croesus/identity_map.rb', line 26

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

.enabled=(flag) ⇒ Object



22
23
24
# File 'lib/croesus/identity_map.rb', line 22

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

.enabled?Boolean

Returns:



30
31
32
# File 'lib/croesus/identity_map.rb', line 30

def self.enabled?
  enabled == true
end

.include?(object) ⇒ Boolean

Returns:



42
43
44
# File 'lib/croesus/identity_map.rb', line 42

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

.repositoryObject



34
35
36
# File 'lib/croesus/identity_map.rb', line 34

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

.useObject



46
47
48
49
50
51
52
53
# File 'lib/croesus/identity_map.rb', line 46

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

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

.withoutObject



55
56
57
58
59
60
61
# File 'lib/croesus/identity_map.rb', line 55

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

  yield if block_given?
ensure
  self.enabled = old
end

Instance Method Details

#add_to_identity_mapObject



90
91
92
# File 'lib/croesus/identity_map.rb', line 90

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

#deleteObject



86
87
88
# File 'lib/croesus/identity_map.rb', line 86

def delete
  super.tap { remove_from_identity_map }
end

#remove_from_identity_mapObject



94
95
96
# File 'lib/croesus/identity_map.rb', line 94

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

#save(options = {}) ⇒ Object



82
83
84
# File 'lib/croesus/identity_map.rb', line 82

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