Class: Redis::Value

Inherits:
BaseObject show all
Defined in:
lib/redis/value.rb

Overview

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

Instance Attribute Summary

Attributes inherited from BaseObject

#key, #options

Instance Method Summary collapse

Methods inherited from BaseObject

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

Methods included from Helpers::CoreCommands

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

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


61
62
63
# File 'lib/redis/value.rb', line 61

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

Instance Method Details

#==(other) ⇒ Object


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

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

#compress(value) ⇒ Object


50
51
52
# File 'lib/redis/value.rb', line 50

def compress(value)
  Zlib::Deflate.deflate(value)
end

#decompress(value) ⇒ Object


46
47
48
# File 'lib/redis/value.rb', line 46

def decompress(value)
  Zlib::Inflate.inflate(value)
end

#inspectObject


54
55
56
# File 'lib/redis/value.rb', line 54

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

#marshal(value, *args) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/redis/value.rb', line 30

def marshal(value, *args)
  if !value.nil? && options[:compress]
    compress(super)
  else
    super
  end
end

#nil?Boolean

Returns:

  • (Boolean)

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

def nil?; value.nil? end

#unmarshal(value, *args) ⇒ Object


38
39
40
41
42
43
44
# File 'lib/redis/value.rb', line 38

def unmarshal(value, *args)
  if !value.nil? && options[:compress]
    super(decompress(value), *args)
  else
    super
  end
end

#valueObject Also known as: get


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

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


9
10
11
12
13
14
15
16
17
# File 'lib/redis/value.rb', line 9

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