Class: Redis::Value

Inherits:
BaseObject show all
Includes:
Helpers::CoreCommands, Helpers::Serialize
Defined in:
lib/redis/value.rb

Overview

Class representing a simple value. You can use standard Ruby operations on it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Serialize

#from_redis, #to_redis

Methods included from Helpers::CoreCommands

#delete, #exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #sort, #ttl, #type

Methods inherited from BaseObject

#redis

Constructor Details

#initialize(key, *args) ⇒ Value

Returns a new instance of Value.



14
15
16
17
# File 'lib/redis/value.rb', line 14

def initialize(key, *args)
  super(key, *args)
  redis.setnx(key, to_redis(@options[:default])) if @options[:default]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



42
43
44
# File 'lib/redis/value.rb', line 42

def method_missing(*args)
  self.value.send *args
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/redis/value.rb', line 13

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/redis/value.rb', line 13

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



37
# File 'lib/redis/value.rb', line 37

def ==(other); value == other end

#as_json(*args) ⇒ Object



39
# File 'lib/redis/value.rb', line 39

def as_json(*args); value.as_json *args end

#inspectObject



33
34
35
# File 'lib/redis/value.rb', line 33

def inspect
  "#<Redis::Value #{value.inspect}>"
end

#nil?Boolean

Returns:

  • (Boolean)


38
# File 'lib/redis/value.rb', line 38

def nil?; value.nil? end

#to_json(*args) ⇒ Object



40
# File 'lib/redis/value.rb', line 40

def to_json(*args); value.to_json *args end

#valueObject Also known as: get



28
29
30
# File 'lib/redis/value.rb', line 28

def value
  from_redis redis.get(key)
end

#value=(val) ⇒ Object Also known as: set



19
20
21
22
23
24
25
# File 'lib/redis/value.rb', line 19

def value=(val)
  if val.nil?
    delete
  else
    redis.set key, to_redis(val)
  end
end