Module Sequel::Plugins::IdentityMap::ClassMethods
In: lib/sequel/plugins/identity_map.rb

Methods

Public Instance methods

Returns the current thread-local identity map. Should be a hash if there is an active identity map, and nil otherwise.

[Source]

    # File lib/sequel/plugins/identity_map.rb, line 42
42:         def identity_map
43:           Thread.current[:sequel_identity_map]
44:         end

The identity map key for an object of the current class with the given pk. May not always be correct for a class which uses STI.

[Source]

    # File lib/sequel/plugins/identity_map.rb, line 48
48:         def identity_map_key(pk)
49:           "#{self}:#{pk ? Array(pk).join(',') : "nil:#{rand}"}"
50:         end

If the identity map is in use, check it for a current copy of the object. If a copy does not exist, create a new object and add it to the identity map. If a copy exists, add any values in the given row that aren‘t currently in the object to the object‘s values. This allows you to only request certain fields in an initial query, make modifications to some of those fields and request other, potentially overlapping fields in a new query, and not have the second query override fields you modified.

[Source]

    # File lib/sequel/plugins/identity_map.rb, line 59
59:         def load(row)
60:           return super unless idm = identity_map
61:           if o = idm[identity_map_key(Array(primary_key).map{|x| row[x]})]
62:             o.merge_db_update(row)
63:           else
64:             o = super
65:             idm[identity_map_key(o.pk)] = o
66:           end
67:           o
68:         end

Take a block and inside that block use an identity map to ensure a 1-1 correspondence of objects to the database row they represent.

[Source]

    # File lib/sequel/plugins/identity_map.rb, line 72
72:         def with_identity_map
73:           return yield if identity_map
74:           begin
75:             self.identity_map = {}
76:             yield
77:           ensure
78:             self.identity_map = nil
79:           end
80:         end

[Validate]