Module: FakeRedis::SortMethod

Defined in:
lib/fakeredis/sort_method.rb

Instance Method Summary collapse

Instance Method Details

#sort(key, *redis_options_array) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fakeredis/sort_method.rb', line 4

def sort(key, *redis_options_array)
  return [] unless key
  return [] if type(key) == 'none'

  unless %w(list set zset).include? type(key)
    raise Redis::CommandError.new("WRONGTYPE Operation against a key holding the wrong kind of value")
  end

  # redis_options is an array of format [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
  # Lets nibble it back into a hash
  options = extract_options_from(redis_options_array)

  # And now to actually do the work of this method

  projected = project(data[key], options[:by], options[:get])
  sorted    = sort_by(projected, options[:order])
  sliced    = slice(sorted, options[:limit])
  # We have to flatten it down as redis-rb adds back the array to the return value
  result = sliced.flatten(1)

  options[:store] ? rpush(options[:store], sliced) : result
end