Sha256: 39b981b7fc8ab972d538859d22c9f334ab203e9ece8017d3fb3526585390059d

Contents?: true

Size: 921 Bytes

Versions: 3

Compression:

Stored size: 921 Bytes

Contents

class Morlock
  class UnknownGemClient < StandardError; end

  class GemClient
    GEM_CLIENTS = []

    def initialize(client)
      @client = client
    end

    def self.wrap(client)
      GEM_CLIENTS.each do |gem, gem_client|
        if (eval(gem) rescue false) && client.is_a?(eval(gem))
          return gem_client.new(client)
        end
      end

      raise UnknownGemClient.new("You provided Morlock a memcached client of an unknown type: #{client.class}")
    end

    def delete(key)
      @client.delete(key)
    end
  end

  class DalliGemClient < GemClient
    def add(key, expiration)
      @client.add(key, 1, expiration)
    end
  end
  GemClient::GEM_CLIENTS << ["Dalli::Client", DalliGemClient]

  class MemcacheGemClient < GemClient
    def add(key, expiration)
      @client.add(key, 1, expiration, true) !~ /NOT_STORED/
    end
  end
  GemClient::GEM_CLIENTS << ["MemCache", MemcacheGemClient]
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
morlock-0.0.3 lib/morlock/gem_client.rb
morlock-0.0.2 lib/morlock/gem_client.rb
morlock-0.0.1 lib/morlock/gem_client.rb