Module: Hoodie::IdentityMap

Defined in:
lib/hoodie/identity_map.rb

Overview

Author: Stefano Harding <[email protected]>

Copyright © 2014 Stefano Harding

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject



37
38
39
# File 'lib/hoodie/identity_map.rb', line 37

def self.clear
  repository.clear
end

.enabledObject



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

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

.enabled=(flag) ⇒ Object



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

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

.enabled?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/hoodie/identity_map.rb', line 29

def self.enabled?
  enabled == true
end

.include?(object) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/hoodie/identity_map.rb', line 41

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

.repositoryObject



33
34
35
# File 'lib/hoodie/identity_map.rb', line 33

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

.useObject



45
46
47
48
49
50
51
# File 'lib/hoodie/identity_map.rb', line 45

def self.use
  old, self.enabled = enabled, true
  yield if block_given?
ensure
  self.enabled = old
  clear
end

.withoutObject



53
54
55
56
57
58
# File 'lib/hoodie/identity_map.rb', line 53

def self.without
  old, self.enabled = enabled, false
  yield if block_given?
ensure
  self.enabled = old
end

Instance Method Details

#add_to_identity_mapObject



87
88
89
# File 'lib/hoodie/identity_map.rb', line 87

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

#deleteObject



83
84
85
# File 'lib/hoodie/identity_map.rb', line 83

def delete
  super.tap { remove_from_identity_map }
end

#remove_from_identity_mapObject



91
92
93
# File 'lib/hoodie/identity_map.rb', line 91

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

#save(options = {}) ⇒ Object



79
80
81
# File 'lib/hoodie/identity_map.rb', line 79

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