Module: RedisLike::Helpers::Lists

Included in:
Lists
Defined in:
lib/redislike/helpers/lists.rb

Instance Method Summary collapse

Instance Method Details

#block_while_empty(list, interval = 0.010) ⇒ Object



39
40
41
# File 'lib/redislike/helpers/lists.rb', line 39

def block_while_empty(list, interval = 0.010)
  sleep interval until llen(list) > 0
end

#dequeue(list, from:) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/redislike/helpers/lists.rb', line 12

def dequeue(list, from:)
  operation = from == :head ? :shift : :pop
  current = fetch list, []
  item = current.method(operation).call
  store list, current
  item
end

#enqueue(list, item, to:, allow_create: true) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/redislike/helpers/lists.rb', line 4

def enqueue(list, item, to:, allow_create: true)
  operation = to == :head ? :unshift : :push
  items = fetch list, []
  items.method(operation).call(item)
  store list, items if allow_create || exists(list)
  llen list
end

#remove_all(list, item) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/redislike/helpers/lists.rb', line 31

def remove_all(list, item)
  items = fetch list, []
  remove_count list, items.count(item), item, from: :head
  # remaining = items.reject { |i| i == item }
  # store list, remaining
  # items.length - remaining.length
end

#remove_count(list, count, item, from:) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/redislike/helpers/lists.rb', line 20

def remove_count(list, count, item, from:)
  operation = from == :tail ? :rindex : :index
  items = fetch list, []
  removed = count.times.map do
    found = items.method(operation).call(item)
    items.delete_at found if found
  end
  store list, items
  removed.compact.length
end