Module: StorageRoom::IdentityMap::ClassMethods

Defined in:
lib/storage_room/identity_map.rb

Instance Method Summary collapse

Instance Method Details

#identity_mapObject



59
60
61
# File 'lib/storage_room/identity_map.rb', line 59

def identity_map
  @identity_map ||= {}
end

#identity_map=(v) ⇒ Object



63
64
65
# File 'lib/storage_room/identity_map.rb', line 63

def identity_map=(v)
  @identity_map = v
end

#identity_map_offObject



75
76
77
# File 'lib/storage_room/identity_map.rb', line 75

def identity_map_off
  @identity_map_status = false
end

#identity_map_off?Boolean

Returns:

  • (Boolean)


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

def identity_map_off?
  !identity_map_on?
end

#identity_map_onObject



71
72
73
# File 'lib/storage_room/identity_map.rb', line 71

def identity_map_on
  @identity_map_status = true
end

#identity_map_on?Boolean

Returns:

  • (Boolean)


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

def identity_map_on?
  identity_map_status
end

#identity_map_statusObject



67
68
69
# File 'lib/storage_room/identity_map.rb', line 67

def identity_map_status
  defined?(@identity_map_status) ? @identity_map_status : true
end

#inherited(descendant) ⇒ Object



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

def inherited(descendant)
  descendant.identity_map = identity_map
  super
end

#load(url, parameters = {}) ⇒ Object

Load an object from the identity map if the key (URL) exists



25
26
27
28
29
30
31
32
33
34
# File 'lib/storage_room/identity_map.rb', line 25

def load(url, parameters = {})
  return nil if url.nil?
  
  if identity_map_on? && object = identity_map[url]
    StorageRoom.log("Loaded #{object} (#{url}) from identity map (load)")
    object
  else
    super
  end
end

#new_from_response_data(response_data) ⇒ Object

Load an object from the identity map or create it



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/storage_room/identity_map.rb', line 37

def new_from_response_data(response_data)        
  url = response_data['@url'] || response_data['url']
  object = url ? identity_map[url] : nil
          
  if object.present? && identity_map_on?
    if object.loaded? && response_data_is_association?(response_data)
      StorageRoom.log("Loaded #{object} (#{url}) from identity map (new_from_response_data)")
    else
      StorageRoom.log("Loaded #{object} (#{url}) from identity map and updated attributes (new_from_response_data)")
      object.set_from_response_data(response_data) 
    end
  else
    object = super
    if identity_map_on? && url.present? && object.is_a?(Model)
      identity_map[url] = object 
      StorageRoom.log("Storing #{url} in identity map: #{object}")
    end
  end
  
  object
end

#without_identity_map(&block) ⇒ Object



87
88
89
90
91
92
# File 'lib/storage_room/identity_map.rb', line 87

def without_identity_map(&block)
  identity_map_off
  yield
ensure
  identity_map_on
end