Method: Spider::Model::IdentityMapper#put
- Defined in:
- lib/spiderfw/model/identity_mapper.rb
#put(obj, check = false, fail_if_exists = false) ⇒ Object
Puts an object into the identity mapper. If check is true, it will first check if the object exists, and if found merge it with the given obj; if check is false, if a object with the same primary keys exists it will be overwritten.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/spiderfw/model/identity_mapper.rb', line 83 def put(obj, check=false, fail_if_exists=false) return nil unless obj return obj if obj._no_identity_mapper if (obj.is_a?(QuerySet)) obj.each_current_index{ |i| obj[i] = put(obj[i], check) } return obj else return obj if @pks[obj.object_id] raise IdentityMapperException, "Can't get without all primary keys" unless obj.primary_keys_set? pks = {} obj.class.primary_keys.each{ |key| pks[key.name] = obj.get(key) } pks = normalize_pks(obj.class, pks) pks.extend(HashComparison) @objects[obj.class] ||= {} if (check && (existent = @objects[obj.class][pks]) && existent.object_id != obj.object_id) if fail_if_exists #debugger raise IdentityMapperException, "A different instance of the same object #{obj.class}(#{obj.primary_keys.inspect}) already exists in the identity mapper" end existent.merge!(obj) return existent else @objects[obj.class][pks] = obj @pks[obj.object_id] = pks traverse(obj) uow = Spider::Model.unit_of_work uow.add(obj) if uow && !uow.running? return obj end end end |