Class: TrailGuide::Adapters::Participants::Redis::Adapter

Inherits:
Base::Adapter
  • Object
show all
Defined in:
lib/trail_guide/adapters/participants/redis.rb

Instance Attribute Summary collapse

Attributes inherited from Base::Adapter

#config, #context

Instance Method Summary collapse

Constructor Details

#initialize(context, config, key: nil) ⇒ Adapter

Returns a new instance of Adapter.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trail_guide/adapters/participants/redis.rb', line 22

def initialize(context, config, key: nil)
  super(context, config)

  if key
    @storage_key = "#{config.namespace}:#{key}"
  elsif config.lookup
    if config.lookup.respond_to?(:call)
      key = config.lookup.call(context)
    else
      key = context.send(config.lookup)
    end
    @storage_key = "#{config.namespace}:#{key}"
  else
    raise ArgumentError, "You must configure a `lookup` proc to use the redis adapter."
  end
end

Instance Attribute Details

#storage_keyObject (readonly)

Returns the value of attribute storage_key.



20
21
22
# File 'lib/trail_guide/adapters/participants/redis.rb', line 20

def storage_key
  @storage_key
end

Instance Method Details

#[](field) ⇒ Object



39
40
41
# File 'lib/trail_guide/adapters/participants/redis.rb', line 39

def [](field)
  TrailGuide.redis.hget(storage_key, field.to_s)
end

#[]=(field, value) ⇒ Object



43
44
45
46
# File 'lib/trail_guide/adapters/participants/redis.rb', line 43

def []=(field, value)
  TrailGuide.redis.hset(storage_key, field.to_s, value)
  TrailGuide.redis.expire(storage_key, config.expiration) if config.expiration
end

#delete(field) ⇒ Object



48
49
50
# File 'lib/trail_guide/adapters/participants/redis.rb', line 48

def delete(field)
  TrailGuide.redis.hdel(storage_key, field.to_s)
end

#destroy!Object



52
53
54
# File 'lib/trail_guide/adapters/participants/redis.rb', line 52

def destroy!
  TrailGuide.redis.del(storage_key)
end

#key?(field) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/trail_guide/adapters/participants/redis.rb', line 60

def key?(field)
  TrailGuide.redis.hexists(storage_key, field.to_s)
end

#keysObject



56
57
58
# File 'lib/trail_guide/adapters/participants/redis.rb', line 56

def keys
  TrailGuide.redis.hkeys(storage_key)
end

#to_hObject



64
65
66
# File 'lib/trail_guide/adapters/participants/redis.rb', line 64

def to_h
  TrailGuide.redis.hgetall(storage_key)
end