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

expiration_filter, #redis, #set_expiration

Constructor Details

#initialize(key, *args) ⇒ Value


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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object


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

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


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

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

#as_json(*args) ⇒ Object


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

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

#inspectObject


31
32
33
# File 'lib/redis/value.rb', line 31

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

#nil?Boolean


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

def nil?; value.nil? end

#to_json(*args) ⇒ Object


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

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

#valueObject Also known as: get


26
27
28
# File 'lib/redis/value.rb', line 26

def value
  unmarshal redis.get(key)
end

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


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

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