Sha256: c992bbe1fb2b22043c44f22e7755205b9253db00273bfc18d16db9493f87e109

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8

module ROM
  class Session

    # @api private
    class Mapper
      include Charlatan.new(:mapper)

      attr_reader :tracker, :identity_map

      def initialize(mapper, tracker, identity_map)
        super
        @tracker = tracker
        @identity_map = identity_map
      end

      # @api private
      def self.build(mapper, tracker)
        new(mapper, tracker, IdentityMap.build)
      end

      # @api public
      def identity(object)
        keys = mapper.identity(object)

        if keys.empty?
          object.__id__
        else
          keys
        end
      end

      # @api private
      def dirty?(object)
        identity_map.fetch_tuple(identity(object)) != dump(object)
      end

      # @api private
      def load(tuple)
        identity = mapper.identity_from_tuple(tuple)
        identity_map.fetch_object(identity) { load_and_track(identity, tuple) }
      end

      # @api private
      def store_in_identity_map(object)
        identity_map.store(identity(object), object, dump(object))
      end

      private

      # @api private
      def load_and_track(identity, tuple)
        object = mapper.load(tuple)
        tracker.store_persisted(object, self)
        identity_map.store(identity, object, tuple)[identity]
      end

    end # Mapper

  end # Session
end # ROM

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.2.0 lib/rom/session/mapper.rb