Permalink
Browse files

Support passing a list of pairs to HMSET.

  • Loading branch information...
1 parent c3b7d4f commit 745a8a977490050f65f430cf1d427011532bec87 Jeff Day committed Mar 19, 2013
Showing with 34 additions and 3 deletions.
  1. +23 −3 lib/redis/connection/memory.rb
  2. +11 −0 spec/hashes_spec.rb
@@ -546,11 +546,31 @@ def hsetnx(key, field, value)
def hmset(key, *fields)
# mapped_hmset gives us [[:k1, "v1", :k2, "v2"]] for `fields`. Fix that.
fields = fields[0] if mapped_param?(fields)
- raise Redis::CommandError, "ERR wrong number of arguments for HMSET" if fields.empty? || fields.size.odd?
+ if fields.empty?
+ raise Redis::CommandError, "ERR wrong number of arguments for HMSET"
+ end
+
+ is_list_of_arrays = fields.all?{|field| field.instance_of?(Array)}
+
+ if fields.size.odd? and !is_list_of_arrays
+ raise Redis::CommandError, "ERR wrong number of arguments for HMSET"
+ end
+
+ if is_list_of_arrays and !fields.all?{|field| field.length == 2}
+ raise Redis::CommandError, "ERR wrong number of arguments for HMSET"
+ end
+
data_type_check(key, Hash)
data[key] ||= {}
- fields.each_slice(2) do |field|
- data[key][field[0].to_s] = field[1].to_s
+
+ if (is_list_of_arrays)
+ fields.each do |pair|
+ data[key][pair[0].to_s] = pair[1].to_s
+ end
+ else
+ fields.each_slice(2) do |field|
+ data[key][field[0].to_s] = field[1].to_s
+ end
end
end
View
@@ -153,5 +153,16 @@ module FakeRedis
@client.hvals("key1").should =~ ["val1", "val2"]
end
+ it "should accept a list of array pairs as arguments and not throw an invalid argument number error" do
+ @client.hmset("key1", [:k1, "val1"], [:k2, "val2"], [:k3, "val3"])
+ @client.hget("key1", :k1).should be == "val1"
+ @client.hget("key1", :k2).should be == "val2"
+ @client.hget("key1", :k3).should be == "val3"
+ end
+
+ it "should reject a list of arrays that contain an invalid number of arguments" do
+ expect { @client.hmset("key1", [:k1, "val1"], [:k2, "val2", "bogus val"]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for HMSET")
+ end
+
end
end

0 comments on commit 745a8a9

Please sign in to comment.