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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Adapter.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/trail_guide/adapters/participants/redis.rb', line 32

def initialize(context, config, key: nil)
  @context = context
  @config = 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

#configObject (readonly)

Returns the value of attribute config.



30
31
32
# File 'lib/trail_guide/adapters/participants/redis.rb', line 30

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context.



30
31
32
# File 'lib/trail_guide/adapters/participants/redis.rb', line 30

def context
  @context
end

#storage_keyObject (readonly)

Returns the value of attribute storage_key.



30
31
32
# File 'lib/trail_guide/adapters/participants/redis.rb', line 30

def storage_key
  @storage_key
end

Instance Method Details

#[](field) ⇒ Object



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

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

#[]=(field, value) ⇒ Object



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

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



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

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

#destroy!Object



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

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

#key?(field) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/trail_guide/adapters/participants/redis.rb', line 71

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

#keysObject



67
68
69
# File 'lib/trail_guide/adapters/participants/redis.rb', line 67

def keys
  TrailGuide.redis.hkeys(storage_key)
end

#to_hObject



75
76
77
# File 'lib/trail_guide/adapters/participants/redis.rb', line 75

def to_h
  TrailGuide.redis.hgetall(storage_key)
end