Class: Redis::Value

Inherits:
BaseObject show all
Includes:
Helpers::CoreCommands
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::CoreCommands

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

Methods inherited from BaseObject

#allow_expiration, #as_json, #initialize, #redis, #set_expiration, #to_hash, #to_json

Constructor Details

This class inherits a constructor from Redis::BaseObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object


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

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key


11
12
13
# File 'lib/redis/value.rb', line 11

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options


11
12
13
# File 'lib/redis/value.rb', line 11

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object


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

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

#inspectObject


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

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

#nil?Boolean

Returns:

  • (Boolean)

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

def nil?; value.nil? end

#valueObject Also known as: get


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

def value
  value = unmarshal(redis.get(key))
  if value.nil? && !@options[:default].nil?
    @options[:default]
  else
    value
  end
end

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


13
14
15
16
17
18
19
20
21
# File 'lib/redis/value.rb', line 13

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