Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support passing a list of pairs to HMSET. #56

Merged
merged 2 commits into from

2 participants

@jeffday

invoking hmset("key", [:k1, "val1"], [:k2, "val2"]) is supported in redis-rb, so i've implemented that behavior here.

lib/redis/connection/memory.rb
((19 lines not shown))
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)
@caius Collaborator
caius added a note

FYI general convention in ruby is to skip the parens in if statement conditions. Could you possibly tidy this up to remove them please? :smile:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@caius
Collaborator

This is awesome - many many thanks for this PR. Spotted one little style thing, if you could please amend that I'll be more than happy to merge this.

Again, many thanks!

@caius caius merged commit 3919d96 into guilleiguaran:master
@caius
Collaborator

Merged. Thanks @jeffday!

@jeffday jeffday deleted the jeffday:hmset_list_of_pairs branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 19, 2013
  1. @jeffday
Commits on Mar 20, 2013
  1. @jeffday

    remove unnecessary parens

    jeffday authored
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 3 deletions.
  1. +23 −3 lib/redis/connection/memory.rb
  2. +11 −0 spec/hashes_spec.rb
View
26 lib/redis/connection/memory.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
11 spec/hashes_spec.rb
@@ -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
Something went wrong with that request. Please try again.