Class: Redis::BaseObject

Inherits:
Object
  • Object
show all
Includes:
Helpers::CoreCommands
Defined in:
lib/redis/base_object.rb

Overview

Defines base functionality for all redis-objects.

Direct Known Subclasses

Counter, EnumerableObject, Lock, Value

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::CoreCommands

#delete, #exists, #exists?, #expire, #expireat, #marshal, #move, #persist, #rename, #renamenx, #serializer, #ttl, #type, #unmarshal

Constructor Details

#initialize(key, *args) ⇒ BaseObject

Returns a new instance of BaseObject.


10
11
12
13
14
# File 'lib/redis/base_object.rb', line 10

def initialize(key, *args)
  @key     = key.is_a?(Array) ? key.flatten.join(':') : key
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @myredis = Objects::ConnectionPoolProxy.proxy_if_needed(args.first)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.


8
9
10
# File 'lib/redis/base_object.rb', line 8

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.


8
9
10
# File 'lib/redis/base_object.rb', line 8

def options
  @options
end

Instance Method Details

#allow_expiration(&block) ⇒ Object


33
34
35
36
37
# File 'lib/redis/base_object.rb', line 33

def allow_expiration(&block)
  result = block.call
  set_expiration
  result
end

#as_jsonObject


45
46
47
# File 'lib/redis/base_object.rb', line 45

def as_json(*)
  to_hash
end

#redisObject

Dynamically query the handle to enable resetting midstream


17
18
19
# File 'lib/redis/base_object.rb', line 17

def redis
  @myredis || ::Redis::Objects.redis
end

#set_expirationObject


23
24
25
26
27
28
29
30
31
# File 'lib/redis/base_object.rb', line 23

def set_expiration
  if !@options[:expiration].nil?
    redis.expire(@key, @options[:expiration]) if redis.ttl(@key) < 0
  elsif !@options[:expireat].nil?
    expireat = @options[:expireat]
    at = expireat.respond_to?(:call) ? expireat.call.to_i : expireat.to_i
    redis.expireat(@key, at) if redis.ttl(@key) < 0
  end
end

#to_hashObject


49
50
51
# File 'lib/redis/base_object.rb', line 49

def to_hash
  { "key" => @key, "options" => @options, "value" => value }
end

#to_json(*args) ⇒ Object


39
40
41
42
43
# File 'lib/redis/base_object.rb', line 39

def to_json(*args)
  to_hash.to_json(*args)
rescue NoMethodError => e
  raise e.class, "The current runtime does not provide a `to_json` implementation. Require 'json' or another JSON library and try again."
end