Sha256: 2d3d4776c4b2d89bc0d6d32bb298ee9a493d34d3d4d3417171e967ea5208af08

Contents?: true

Size: 1.68 KB

Versions: 13

Compression:

Stored size: 1.68 KB

Contents

module Dynamoid
  module IdentityMap
    extend ActiveSupport::Concern

    def self.clear
      Dynamoid.included_models.each { |m| m.identity_map.clear }
    end

    module ClassMethods
      def identity_map
        @identity_map ||= {}
      end

      def from_database(attrs = {})
        return super if identity_map_off?

        key = identity_map_key(attrs)
        document = identity_map[key]

        if document.nil?
          document = super
          identity_map[key] = document
        else
          document.load(attrs)
        end

        document
      end

      def find_by_id(id, options = {})
        return super if identity_map_off?

        key = id.to_s

        if range_key = options[:range_key]
          key += "::#{range_key}"
        end

        if identity_map[key]
          identity_map[key]
        else
          super
        end
      end

      def identity_map_key(attrs)
        key = attrs[hash_key].to_s
        if range_key
          key += "::#{attrs[range_key]}"
        end
        key
      end

      def identity_map_on?
        Dynamoid::Config.identity_map
      end

      def identity_map_off?
        !identity_map_on?
      end
    end

    def identity_map
      self.class.identity_map
    end

    def save(*args)
      return super if self.class.identity_map_off?

      if result = super
        identity_map[identity_map_key] = self
      end
      result
    end

    def delete
      return super if self.class.identity_map_off?

      identity_map.delete(identity_map_key)
      super
    end


    def identity_map_key
      key = hash_key.to_s
      if self.class.range_key
        key += "::#{range_value}"
      end
      key
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
dynamoid-1.3.4 lib/dynamoid/identity_map.rb
dynamoid-1.3.3 lib/dynamoid/identity_map.rb
dynamoid-1.3.2 lib/dynamoid/identity_map.rb
dynamoid-1.3.1 lib/dynamoid/identity_map.rb
dynamoid-1.3.0 lib/dynamoid/identity_map.rb
synamoid-1.2.1 lib/dynamoid/identity_map.rb
dynamoid-1.2.1 lib/dynamoid/identity_map.rb
dynamoid-1.2.0 lib/dynamoid/identity_map.rb
dynamoid-edge-1.1.2 lib/dynamoid/identity_map.rb
dynamoid-edge-1.1.1 lib/dynamoid/identity_map.rb
dynamoid-edge-1.1.0 lib/dynamoid/identity_map.rb
dynamoid-1.1.0 lib/dynamoid/identity_map.rb
dynamoid-1.0.0 lib/dynamoid/identity_map.rb