Class: RedList

Inherits:
Object
  • Object
show all
Defined in:
lib/redness/red_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ RedList

Returns a new instance of RedList.



52
53
54
# File 'lib/redness/red_list.rb', line 52

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



2
3
4
# File 'lib/redness/red_list.rb', line 2

def key
  @key
end

Class Method Details

.add(key, value) ⇒ Object



20
21
22
23
24
# File 'lib/redness/red_list.rb', line 20

def self.add(key, value)
  redis.execute_with_uncertainty(false) do
    redis.lpush(key, value)
  end
end

.count(key, value) ⇒ Object



32
33
34
# File 'lib/redness/red_list.rb', line 32

def self.count(key, value)
  get(key).count(value)
end

.get(key) ⇒ Object



8
9
10
11
12
# File 'lib/redness/red_list.rb', line 8

def self.get(key)
  redis.execute_with_uncertainty([]) do
    redis.lrange(key, 0, -1).map(&:to_i)
  end
end

.get_strings(key) ⇒ Object



14
15
16
17
18
# File 'lib/redness/red_list.rb', line 14

def self.get_strings(key)
  redis.execute_with_uncertainty([]) do
    redis.lrange(key, 0, -1)
  end
end

.pop(key) ⇒ Object



46
47
48
49
50
# File 'lib/redness/red_list.rb', line 46

def self.pop(key)
  redis.execute_with_uncertainty(false) do
    redis.rpop(key)
  end
end

.redisObject



4
5
6
# File 'lib/redness/red_list.rb', line 4

def self.redis
  @redis ||= Red.new
end

.remove(key, value, occurrences = 0) ⇒ Object



26
27
28
29
30
# File 'lib/redness/red_list.rb', line 26

def self.remove(key, value, occurrences = 0)
  redis.execute_with_uncertainty(false) do
    redis.lrem(key, occurrences, value)
  end
end

.total_size(key) ⇒ Object



36
37
38
# File 'lib/redness/red_list.rb', line 36

def self.total_size(key)
  get(key).count
end

.trim_to(key, amount) ⇒ Object



40
41
42
43
44
# File 'lib/redness/red_list.rb', line 40

def self.trim_to(key, amount)
  redis.execute_with_uncertainty(false) do
    redis.ltrim(key, 0, (amount - 1))
  end
end

Instance Method Details

#add(value) ⇒ Object



56
57
58
# File 'lib/redness/red_list.rb', line 56

def add(value)
  self.class.add(@key, value)
end

#remove(value) ⇒ Object



60
61
62
# File 'lib/redness/red_list.rb', line 60

def remove(value)
  self.class.remove(@key, value)
end

#valueObject



64
65
66
# File 'lib/redness/red_list.rb', line 64

def value
  self.class.get(@key)
end