Sha256: eca0027cd923dc1c5939ec2e7b94a9b61a27bdd047e364c8c8c42d67a1096f58

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

class Redis
  class Store < self
    module Namespace
      def set(key, val, options = nil)
        namespace(key) { |key| super(key, val, options) }
      end

      def setnx(key, val, options = nil)
        namespace(key) { |key| super(key, val, options) }
      end

      def get(key, options = nil)
        namespace(key) { |key| super(key, options) }
      end

      def exists(key)
        namespace(key) { |key| super(key) }
      end

      def incrby(key, increment)
        namespace(key) { |key| super(key, increment) }
      end

      def decrby(key, increment)
        namespace(key) { |key| super(key, increment) }
      end

      def keys(pattern = "*")
        namespace(pattern) { |pattern| super(pattern).map{|key| strip_namespace(key) } }
      end

      def del(*keys)
        super *keys.map {|key| interpolate(key) } if keys.any?
      end

      def mget(*keys)
        super *keys.map {|key| interpolate(key) } if keys.any?
      end

      def to_s
        "#{super} with namespace #{@namespace}"
      end

      def flushdb
        del *keys
      end

      private
        def namespace(key)
          yield interpolate(key)
        end

        def interpolate(key)
          key.match(namespace_regexp) ? key : "#{@namespace}:#{key}"
        end

        def strip_namespace(key)
          key.gsub namespace_regexp, ""
        end

        def namespace_regexp
          @namespace_regexp ||= %r{^#{@namespace}\:}
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 4 rubygems

Version Path
redis-store-1.1.4 lib/redis/store/namespace.rb
redis-store-json-3.0.0 redis-store-json/lib/redis/store/namespace.rb
redis-rack-json-1.5.2 redis-store-json/lib/redis/store/namespace.rb
redis-actionpack-json-4.0.0 redis-store-json/lib/redis/store/namespace.rb
redis-store-1.1.3 lib/redis/store/namespace.rb