Module: Croesus::IdentityMap
- Included in:
- Croesus
- Defined in:
- lib/croesus/identity_map.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .clear ⇒ Object
- .enabled ⇒ Object
- .enabled=(flag) ⇒ Object
- .enabled? ⇒ Boolean
- .include?(object) ⇒ Boolean
- .repository ⇒ Object
- .use ⇒ Object
- .without ⇒ Object
Instance Method Summary collapse
- #add_to_identity_map ⇒ Object
- #delete ⇒ Object
- #remove_from_identity_map ⇒ Object
- #save(options = {}) ⇒ Object
Class Method Details
.clear ⇒ Object
38 39 40 |
# File 'lib/croesus/identity_map.rb', line 38 def self.clear repository.clear end |
.enabled ⇒ Object
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
30 31 32 |
# File 'lib/croesus/identity_map.rb', line 30 def self.enabled? enabled == true end |
.include?(object) ⇒ Boolean
42 43 44 |
# File 'lib/croesus/identity_map.rb', line 42 def self.include?(object) repository.keys.include?(object.id) end |
.repository ⇒ Object
34 35 36 |
# File 'lib/croesus/identity_map.rb', line 34 def self.repository Thread.current[:identity_map] ||= {} end |
.use ⇒ Object
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 |
.without ⇒ Object
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_map ⇒ Object
90 91 92 |
# File 'lib/croesus/identity_map.rb', line 90 def add_to_identity_map IdentityMap.repository[id] = self if IdentityMap.enabled? end |
#delete ⇒ Object
86 87 88 |
# File 'lib/croesus/identity_map.rb', line 86 def delete super.tap { remove_from_identity_map } end |
#remove_from_identity_map ⇒ Object
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(={}) super.tap { |result| add_to_identity_map if result } end |