Class: Split::Persistence::RedisAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/split/persistence/redis_adapter.rb

Constant Summary collapse

DEFAULT_CONFIG =
{:namespace => 'persistence'}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, key = nil) ⇒ RedisAdapter

Returns a new instance of RedisAdapter



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/split/persistence/redis_adapter.rb', line 8

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

Instance Attribute Details

#redis_keyObject (readonly)

Returns the value of attribute redis_key



6
7
8
# File 'lib/split/persistence/redis_adapter.rb', line 6

def redis_key
  @redis_key
end

Class Method Details

.configObject



44
45
46
# File 'lib/split/persistence/redis_adapter.rb', line 44

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

.reset_config!Object



48
49
50
# File 'lib/split/persistence/redis_adapter.rb', line 48

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

.with_config(options = {}) ⇒ Object



39
40
41
42
# File 'lib/split/persistence/redis_adapter.rb', line 39

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

Instance Method Details

#[](field) ⇒ Object



23
24
25
# File 'lib/split/persistence/redis_adapter.rb', line 23

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

#[]=(field, value) ⇒ Object



27
28
29
# File 'lib/split/persistence/redis_adapter.rb', line 27

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

#delete(field) ⇒ Object



31
32
33
# File 'lib/split/persistence/redis_adapter.rb', line 31

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

#keysObject



35
36
37
# File 'lib/split/persistence/redis_adapter.rb', line 35

def keys
  Split.redis.hkeys(redis_key)
end