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, #move, #persist, #rename, #renamenx, #sort, #ttl, #type

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



66
67
68
# File 'lib/redis/value.rb', line 66

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



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

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

#compress(value) ⇒ Object



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

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

#decompress(value) ⇒ Object



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

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

#inspectObject



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

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

#marshal(value, *args) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/redis/value.rb', line 35

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

#nil?Boolean

Returns:

  • (Boolean)


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

def nil?; value.nil? end

#unmarshal(value, *args) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/redis/value.rb', line 43

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

#valueObject Also known as: get



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

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



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

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