Class: Redis::Value

Inherits:
Object
  • Object
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, #rename, #renamenx, #type

Constructor Details

#initialize(key, *args) ⇒ Value

Returns a new instance of Value.



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

def initialize(key, *args)
  @key = key
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @redis = args.first || $redis
  @redis.setnx(key, @options[:default]) if @options[:default]
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

#redisObject (readonly)

Returns the value of attribute redis.



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

def redis
  @redis
end

Instance Method Details

#==(x) ⇒ Object



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

def ==(x); value == x; end

#nil?Boolean

Returns:

  • (Boolean)


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

def nil?;  value.nil?; end

#to_sObject Also known as: to_str



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

def to_s;  value.to_s; end

#valueObject Also known as: get



24
25
26
# File 'lib/redis/value.rb', line 24

def value
  from_redis redis.get(key)
end

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



19
20
21
# File 'lib/redis/value.rb', line 19

def value=(val)
  redis.set key, to_redis(val)
end