Sha256: 22c55d0ad8e1ca432271eea52c5455a4e9114cc597ec19f6f1ae86e3378c700d

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

require 'rod/rest/exception'

module Rod
  module Rest
    # Cache used to store proxy objects.
    class ProxyCache
      # Initializes empty cache.
      def initialize()
        @cache_implementation = {}
      end

      # Returns true if the described object is in the cache.
      def has_key?(description)
        check_description(description)
        @cache_implementation.has_key?(description_signature(description))
      end

      # Returns the object stored in the cache. Raises CacheMissed exception if
      # the result is nil.
      def [](description)
        check_description(description)
        begin
          @cache_implementation.fetch(description_signature(description))
        rescue KeyError
          raise CacheMissed.new(missing_entry_message(description))
        end
      end

      # Store the +object+ in the cache.
      def store(object)
        check_object(object)
        @cache_implementation[description_signature(rod_id: object.rod_id,type: object.type)] = object
      end

      private
      def description_signature(description)
        "#{description[:rod_id]},#{description[:type]}"
      end

      def check_object(object)
        if !object.respond_to?(:rod_id) || !object.respond_to?(:type)
          raise InvalidData.new(invalid_object_message(object))
        end
      end

      def check_description(description)
        if !description.respond_to?(:has_key?) || !description.has_key?(:rod_id) || !description.has_key?(:type)
          raise InvalidData.new(invalid_description_message(description))
        end
      end

      def missing_entry_message(description)
        "No entry for object rod_id:#{description[:rod_id]} type:#{description[:type]}"
      end

      def invalid_object_message(object)
        "The object cannot be stored in the cache: #{object}."
      end

      def invalid_description_message(description)
        "The description of the object is invalid: #{description}."
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rod-rest-0.5.1 lib/rod/rest/proxy_cache.rb
rod-rest-0.5.0 lib/rod/rest/proxy_cache.rb