Class: Strobe::IdentityMap

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/identity_map.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIdentityMap

Returns a new instance of IdentityMap.



32
33
34
# File 'lib/strobe/identity_map.rb', line 32

def initialize
  reset!
end

Class Method Details

.currentObject



28
29
30
# File 'lib/strobe/identity_map.rb', line 28

def self.current
  Thread.current[:__strobe_im]
end

.forget(klass, key) ⇒ Object



20
21
22
# File 'lib/strobe/identity_map.rb', line 20

def self.forget(klass, key)
  current && key && current[klass, key] = nil
end

.identify(klass, key) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/strobe/identity_map.rb', line 10

def self.identify(klass, key)
  return yield unless current && key

  inst = current[klass, key]

  return inst if inst

  current[klass, key] = yield if block_given?
end

.reset!Object



24
25
26
# File 'lib/strobe/identity_map.rb', line 24

def self.reset!
  current && current.reset!
end

.wrapObject



3
4
5
6
7
8
# File 'lib/strobe/identity_map.rb', line 3

def self.wrap
  Thread.current[:__strobe_im] = new
  yield
ensure
  Thread.current[:__strobe_im] = nil
end

Instance Method Details

#[](klass, key) ⇒ Object



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

def [](klass, key)
  key = klass.key.typecast(key) if klass.respond_to?(:key)
  @map[klass.to_s][key]
end

#[]=(klass, key, val) ⇒ Object



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

def []=(klass, key, val)
  key = klass.key.typecast(key) if klass.respond_to?(:key)
  @map[klass.to_s][key] = val
end

#reset!Object



46
47
48
# File 'lib/strobe/identity_map.rb', line 46

def reset!
  @map = Hash.new { |h,k| h[k] = {} }
end