lib/redis/connection/memory.rb in fakeredis-0.6.0 vs lib/redis/connection/memory.rb in fakeredis-0.7.0

- old
+ new

@@ -7,10 +7,11 @@ require "fakeredis/sorted_set_argument_handler" require "fakeredis/sorted_set_store" require "fakeredis/transaction_commands" require "fakeredis/zset" require "fakeredis/bitop_command" +require "fakeredis/version" class Redis module Connection class Memory include Redis::Connection::CommandHelper @@ -54,10 +55,11 @@ end def database_id @database_id ||= 0 end + attr_writer :database_id def database_instance_key [options[:host], options[:port]].hash end @@ -87,10 +89,20 @@ end def disconnect end + def client(command, _options = {}) + case command + when :setname then true + when :getname then nil + when :client then true + else + raise Redis::CommandError, "ERR unknown command '#{command}'" + end + end + def timeout=(usecs) end def read replies.shift @@ -384,13 +396,13 @@ value = value.to_s index = data[key].index(pivot.to_s) return -1 if index.nil? - case where - when :before then data[key].insert(index, value) - when :after then data[key].insert(index + 1, value) + case where.to_s + when /\Abefore\z/i then data[key].insert(index, value) + when /\Aafter\z/i then data[key].insert(index + 1, value) else raise_syntax_error end end def lset(key, index, value) @@ -557,15 +569,18 @@ result = self.srem(source, value) self.sadd(destination, value) if result result end - def spop(key) + def spop(key, count = nil) data_type_check(key, ::Set) - elem = srandmember(key) - srem(key, elem) - elem + results = (count || 1).times.map do + elem = srandmember(key) + srem(key, elem) + elem + end.compact + count.nil? ? results.first : results end def scard(key) data_type_check(key, ::Set) return 0 unless data[key] @@ -777,10 +792,11 @@ else fields.each_slice(2) do |field| data[key][field[0].to_s] = field[1].to_s end end + "OK" end def hmget(key, *fields) raise_argument_error('hmget') if fields.empty? @@ -965,11 +981,11 @@ if idx = args.index("COUNT") count = args[idx + 1] end start_cursor = start_cursor.to_i - data_type_check(start_cursor, Fixnum) + data_type_check(start_cursor, Integer) cursor = start_cursor returned_keys = [] final_page = start_cursor + count >= keys(match).length @@ -1075,11 +1091,11 @@ return [] unless data[key] results = sort_keys(data[key]) # Select just the keys unless we want scores results = results.map(&:first) unless with_scores - results[start..stop].flatten.map(&:to_s) + (results[start..stop] || []).flatten.map(&:to_s) end def zrangebylex(key, start, stop, *opts) data_type_check(key, ZSet) return [] unless data[key] @@ -1284,11 +1300,11 @@ if idx = args.index("COUNT") count = args[idx + 1] end start_cursor = start_cursor.to_i - data_type_check(start_cursor, Fixnum) + data_type_check(start_cursor, Integer) cursor = start_cursor next_keys = [] sorted_keys = sort_keys(data[key]) @@ -1415,6 +1431,7 @@ end end end end +# FIXME this line should be deleted as explicit enabling is better Redis::Connection.drivers << Redis::Connection::Memory