Class: Redis::Objective

Inherits:
Object
  • Object
show all
Defined in:
lib/redis/objective.rb

Constant Summary collapse

VERSION =
File.read( File.join(File.dirname(__FILE__),'..','..','VERSION') ).strip

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Objective

Returns a new instance of Objective.



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

def initialize(client)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



56
57
58
# File 'lib/redis/objective.rb', line 56

def method_missing(name, *args, &block)
  @client.send(name, *args, &block)
end

Instance Method Details

#get(key, *args) ⇒ Object Also known as: []



16
17
18
19
20
# File 'lib/redis/objective.rb', line 16

def get(key, *args)
  value = @client.get(key, *args)
  return if value.nil?
  YAML.load(value)
end

#mapped_mget(*args) ⇒ Object Also known as: mget



38
39
40
# File 'lib/redis/objective.rb', line 38

def mapped_mget(*args)
  @client.mapped_mget(*args).inject({}){|h,k_v| h[k_v[0]] = YAML.load(k_v[1]); h}
end

#mset(*args) ⇒ Object Also known as: mapped_mset



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/redis/objective.rb', line 43

def mset(*args)
  # flatten given hash/nested array
  if args.size == 1
    args = args.first.inject([]){|a,k_v| a << k_v[0]; a << k_v[1]; a}
  end

  # serialize all values [k1, v1, k2, v2, ...]
  args.size.times{|i| args[i] = args[i].to_yaml if i % 2 == 1 }

  @client.mset(*args)
end

#set(key, value, *args) ⇒ Object Also known as: []=, set_with_expire



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/redis/objective.rb', line 23

def set(key, value, *args)
  if value.nil?
    @client.del(key)
  else
    if args.empty?
      @client.set(key, value.to_yaml)
    else
      @client.set_with_expire(key, value.to_yaml, *args)
    end
  end
  value
end