Class: Redstruct::Types::List

Inherits:
Struct show all
Includes:
Utils::Scriptable
Defined in:
lib/redstruct/types/list.rb

Direct Known Subclasses

Hls::Queue

Instance Attribute Summary

Attributes inherited from Base

#key

Instance Method Summary collapse

Methods included from Utils::Scriptable

included

Methods inherited from Struct

#delete, #exists?, #expire, #expire_at, #inspectable_attributes, #persist, #type

Methods included from Utils::Inspectable

#inspect, #inspectable_attributes, #to_s

Methods inherited from Base

#initialize, #inspectable_attributes, #to_h, #with

Constructor Details

This class inherits a constructor from Redstruct::Types::Base

Instance Method Details

#[](index) ⇒ Object



14
15
16
# File 'lib/redstruct/types/list.rb', line 14

def [](index)
  return self.connection.lindex(@key, index.to_i)
end

#[]=(index, value) ⇒ Object



18
19
20
# File 'lib/redstruct/types/list.rb', line 18

def []=(index, value)
  return self.connection.lset(@key, index.to_i, value)
end

#append(*elements, max: 0) ⇒ Object



22
23
24
25
26
# File 'lib/redstruct/types/list.rb', line 22

def append(*elements, max: 0)
  max = max.to_i
  return self.connection.rpush(@key, elements) if max <= 0
  return push_and_trim_script(keys: @key, argv: [max - 1, 0] + elements)
end

#clearObject



6
7
8
# File 'lib/redstruct/types/list.rb', line 6

def clear
  delete
end

#empty?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/redstruct/types/list.rb', line 10

def empty?
  return !exists?
end

#pop(timeout: nil) ⇒ Object



34
35
36
37
38
# File 'lib/redstruct/types/list.rb', line 34

def pop(timeout: nil)
  options = {}
  options[:timeout] = timeout.to_i unless timeout.nil?
  return self.connection.blpop(@key, options)&.last
end

#prepend(*elements, max: nil) ⇒ Object



28
29
30
31
32
# File 'lib/redstruct/types/list.rb', line 28

def prepend(*elements, max: nil)
  max = max.to_i
  return self.connection.lpush(@key, elements) if max <= 0
  return push_and_trim_script(keys: @key, argv: [max - 1, 1] + elements)
end

#remove(value, count: 1) ⇒ Object



40
41
42
43
# File 'lib/redstruct/types/list.rb', line 40

def remove(value, count: 1)
  count = [1, count.to_i].max
  self.connection.lrem(@key, count, value)
end

#sizeObject



45
46
47
# File 'lib/redstruct/types/list.rb', line 45

def size
  return self.connection.llen(@key)
end

#slice(start = 0, length = -1)) ⇒ Object



49
50
51
# File 'lib/redstruct/types/list.rb', line 49

def slice(start = 0, length = -1)
  return self.connection.lrange(@key, start.to_i, length.to_i)
end

#to_aObject



53
54
55
# File 'lib/redstruct/types/list.rb', line 53

def to_a
  return slice(0, -1)
end