Module: MockRedis::SortMethod

Includes:
Assertions
Included in:
Database
Defined in:
lib/mock_redis/sort_method.rb

Instance Method Summary collapse

Instance Method Details

#sort(key, options = {}) ⇒ Object


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

def sort(key, options = {})
  return [] if key.nil?

  enumerable = data[key]

  return [] if enumerable.nil?

  by           = options[:by]
  limit        = options[:limit] || []
  store        = options[:store]
  get_patterns = Array(options[:get])
  order        = options[:order] || 'ASC'
  direction    = order.split.first

  projected = project(enumerable, by, get_patterns)
  sorted    = sort_by(projected, direction)
  sliced    = slice(sorted, limit)

  store ? rpush(store, sliced) : sliced
end