Sha256: 04d14414b012a87190bf338603f40d91fc1435fb8e38f5e668bda3660755083e

Contents?: true

Size: 1.34 KB

Versions: 18

Compression:

Stored size: 1.34 KB

Contents

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

      attr_reader :redis_key

      def initialize(context, key = nil)
        if key
          @redis_key = "#{self.class.config[:namespace]}:#{key}"
        elsif 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)
        expire_seconds = self.class.config[:expire_seconds]
        Split.redis.expire(redis_key, expire_seconds) if expire_seconds
      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

18 entries across 18 versions & 2 rubygems

Version Path
ab-split-1.0.2 lib/split/persistence/redis_adapter.rb
ab-split-1.0.1 lib/split/persistence/redis_adapter.rb
ab-split-1.0.0 lib/split/persistence/redis_adapter.rb
split-3.4.1 lib/split/persistence/redis_adapter.rb
split-3.4.0 lib/split/persistence/redis_adapter.rb
split-3.3.2 lib/split/persistence/redis_adapter.rb
split-3.3.1 lib/split/persistence/redis_adapter.rb
split-3.3.0 lib/split/persistence/redis_adapter.rb
split-3.2.0 lib/split/persistence/redis_adapter.rb
split-3.1.1 lib/split/persistence/redis_adapter.rb
split-3.1.0 lib/split/persistence/redis_adapter.rb
split-3.0.0 lib/split/persistence/redis_adapter.rb
split-2.2.0 lib/split/persistence/redis_adapter.rb
split-2.1.0 lib/split/persistence/redis_adapter.rb
split-2.0.0 lib/split/persistence/redis_adapter.rb
split-1.7.0 lib/split/persistence/redis_adapter.rb
split-1.6.0 lib/split/persistence/redis_adapter.rb
split-1.5.0 lib/split/persistence/redis_adapter.rb