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

Methods inherited from Base::Adapter

#subject

Constructor Details

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

Returns a new instance of Adapter.



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

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.



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

def storage_key
  @storage_key
end

Instance Method Details

#[](field) ⇒ Object



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

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

#[]=(field, value) ⇒ Object



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

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



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

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

#destroy!Object



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

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

#key?(field) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#keysObject



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

def keys
  TrailGuide.redis.hkeys(storage_key)
end

#to_hObject



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

def to_h
  TrailGuide.redis.hgetall(storage_key)
end