Sha256: 3817fe6ca391d98a0af581509ece57ac2992021009c613a094536d0131819878

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

module Split
  module Persistence
    class RedisAdapter
      DEFAULT_CONFIG = {:namespace => 'persistence'}.freeze

      attr_reader :redis_key

      def initialize(context)
        if lookup_by = self.class.config[:lookup_by]
          if lookup_by.respond_to?(:call)
            key_frag = lookup_by.call(context)
          else
            key_frag = context.send(lookup_by)
          end
          @redis_key = "#{self.class.config[:namespace]}:#{key_frag}"
        else
          raise "Please configure lookup_by"
        end
      end

      def [](field)
        Split.redis.hget(redis_key, field)
      end

      def []=(field, value)
        Split.redis.hset(redis_key, field, value)
      end

      def delete(field)
        Split.redis.hdel(redis_key, field)
      end

      def keys
        Split.redis.hkeys(redis_key)
      end

      def self.with_config(options={})
        self.config.merge!(options)
        self
      end

      def self.config
        @config ||= DEFAULT_CONFIG.dup
      end

      def self.reset_config!
        @config = DEFAULT_CONFIG.dup
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
split-1.0.0 lib/split/persistence/redis_adapter.rb
split-0.8.0 lib/split/persistence/redis_adapter.rb
split-0.7.3 lib/split/persistence/redis_adapter.rb
split-0.7.2 lib/split/persistence/redis_adapter.rb
split-0.7.1 lib/split/persistence/redis_adapter.rb
split-0.7.0 lib/split/persistence/redis_adapter.rb
split-0.6.6 lib/split/persistence/redis_adapter.rb
split-0.6.5 lib/split/persistence/redis_adapter.rb