lib/redis/hash_key.rb in redis-objects-0.8.0 vs lib/redis/hash_key.rb in redis-objects-0.9.0
- old
+ new
@@ -7,43 +7,29 @@
class HashKey < BaseObject
require 'enumerator'
include Enumerable
require 'redis/helpers/core_commands'
include Redis::Helpers::CoreCommands
- require 'redis/helpers/serialize'
- include Redis::Helpers::Serialize
attr_reader :key, :options
def initialize(key, *args)
super
@options[:marshal_keys] ||= {}
end
- # Needed since Redis::Hash masks bare Hash in redis.rb
- def self.[](*args)
- ::Hash[*args]
- end
-
- # Sets a field to value
- def []=(field, value)
- store(field, to_redis(value))
- end
-
- # Gets the value of a field
- def [](field)
- fetch(field)
- end
-
# Redis: HSET
def store(field, value)
- redis.hset(key, field, to_redis(value, options[:marshal_keys][field]))
+ redis.hset(key, field, marshal(value, options[:marshal_keys][field]))
end
+ alias_method :[]=, :store
# Redis: HGET
- def fetch(field)
- from_redis redis.hget(key, field), options[:marshal_keys][field]
+ def hget(field)
+ unmarshal redis.hget(key, field), options[:marshal_keys][field]
end
+ alias_method :get, :hget
+ alias_method :[], :hget
# Verify that a field exists. Redis: HEXISTS
def has_key?(field)
redis.hexists(key, field)
end
@@ -54,25 +40,35 @@
# Delete field. Redis: HDEL
def delete(field)
redis.hdel(key, field)
end
+ # Fetch a key in a way similar to Ruby's Hash#fetch
+ def fetch(field, *args, &block)
+ value = hget(field)
+ default = args[0]
+
+ return value if value || (!default && !block_given?)
+
+ block_given? ? block.call(field) : default
+ end
+
# Return all the keys of the hash. Redis: HKEYS
def keys
redis.hkeys(key)
end
# Return all the values of the hash. Redis: HVALS
def values
- from_redis redis.hvals(key)
+ redis.hvals(key).map{|v| unmarshal(v) }
end
alias_method :vals, :values
# Retrieve the entire hash. Redis: HGETALL
def all
h = redis.hgetall(key) || {}
- h.each { |k,v| h[k] = from_redis(v, options[:marshal_keys][k]) }
+ h.each{|k,v| h[k] = unmarshal(v, options[:marshal_keys][k]) }
h
end
alias_method :clone, :all
# Enumerate through all fields. Redis: HGETALL
@@ -109,49 +105,71 @@
# Set keys in bulk, takes a hash of field/values {'field1' => 'val1'}. Redis: HMSET
def bulk_set(*args)
raise ArgumentError, "Argument to bulk_set must be hash of key/value pairs" unless args.last.is_a?(::Hash)
redis.hmset(key, *args.last.inject([]){ |arr,kv|
- arr + [kv[0], to_redis(kv[1], options[:marshal_keys][kv[0]])]
+ arr + [kv[0], marshal(kv[1], options[:marshal_keys][kv[0]])]
})
end
alias_method :update, :bulk_set
# Set keys in bulk if they do not exist. Takes a hash of field/values {'field1' => 'val1'}. Redis: HSETNX
def fill(pairs={})
raise ArgumentError, "Arugment to fill must be a hash of key/value pairs" unless pairs.is_a?(::Hash)
pairs.each do |field, value|
- redis.hsetnx(key, field, to_redis(value, options[:marshal_keys][field]))
+ redis.hsetnx(key, field, marshal(value, options[:marshal_keys][field]))
end
end
# Get keys in bulk, takes an array of fields as arguments. Redis: HMGET
def bulk_get(*fields)
hsh = {}
res = redis.hmget(key, *fields.flatten)
fields.each do |k|
- hsh[k] = from_redis(res.shift, options[:marshal_keys][k])
+ hsh[k] = unmarshal(res.shift, options[:marshal_keys][k])
end
hsh
end
# Get values in bulk, takes an array of keys as arguments.
# Values are returned in a collection in the same order than their keys in *keys Redis: HMGET
def bulk_values(*keys)
res = redis.hmget(key, *keys.flatten)
- keys.inject([]){|collection, k| collection << from_redis(res.shift)}
+ keys.inject([]){|collection, k| collection << unmarshal(res.shift, options[:marshal_keys][k])}
end
# Increment value by integer at field. Redis: HINCRBY
- def incrby(field, val = 1)
- ret = redis.hincrby(key, field, val)
+ def incrby(field, by=1)
+ ret = redis.hincrby(key, field, by)
unless ret.is_a? Array
ret.to_i
else
nil
end
end
alias_method :incr, :incrby
+ # Decrement value by integer at field. Redis: HINCRBY
+ def decrby(field, by=1)
+ incrby(field, -by)
+ end
+ alias_method :decr, :decrby
+
+ # Increment value by float at field. Redis: HINCRBYFLOAT
+ def incrbyfloat(field, by=1.0)
+ ret = redis.hincrbyfloat(key, field, by)
+ unless ret.is_a? Array
+ ret.to_i
+ else
+ nil
+ end
+ end
+
+ # Decrement value by float at field. Redis: HINCRBYFLOAT
+ def decrbyfloat(field, by=1.0)
+ incrbyfloat(field, -by)
+ end
+
+ expiration_filter :[]=, :store, :bulk_set, :fill, :incrby
end
end