lib/redis/hash_key.rb in redis-objects-0.5.2 vs lib/redis/hash_key.rb in redis-objects-0.5.3
- old
+ new
@@ -68,11 +68,11 @@
end
alias_method :vals, :values
# Retrieve the entire hash. Redis: HGETALL
def all
- h = redis.hgetall(key)
+ h = redis.hgetall(key) || {}
h.each { |k,v| h[k] = from_redis(v, options[:marshal_keys][k]) }
h
end
alias_method :clone, :all
@@ -112,9 +112,18 @@
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]])]
})
+ 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 ArgumentErorr, "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]))
+ end
end
# Get keys in bulk, takes an array of fields as arguments. Redis: HMGET
def bulk_get(*fields)
hsh = {}