Class: Conflow::Redis::ArrayField

Inherits:
Field
  • Object
show all
Includes:
Enumerable
Defined in:
lib/conflow/redis/array_field.rb

Overview

Represents Redis list. It’s methods mirror most used Array methods.

Instance Attribute Summary

Attributes inherited from Field

#key

Instance Method Summary collapse

Methods inherited from Field

#initialize

Constructor Details

This class inherits a constructor from Conflow::Redis::Field

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/conflow/redis/array_field.rb', line 68

def ==(other)
  case other
  when Array      then to_a == other
  when ArrayField then key == other.key || to_a == other.to_a
  else super
  end
end

#[](index) ⇒ Object



9
10
11
# File 'lib/conflow/redis/array_field.rb', line 9

def [](index)
  command :lindex, [key, index]
end

#[]=(index, value) ⇒ Object



13
14
15
# File 'lib/conflow/redis/array_field.rb', line 13

def []=(index, value)
  command :lset, [key, index, value]
end

#concat(ary) ⇒ Object



43
44
45
# File 'lib/conflow/redis/array_field.rb', line 43

def concat(ary)
  push(*ary)
end

#each(&block) ⇒ Object



64
65
66
# File 'lib/conflow/redis/array_field.rb', line 64

def each(&block)
  to_a.each(&block)
end

#insert(value, after: nil, before: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/conflow/redis/array_field.rb', line 17

def insert(value, after: nil, before: nil)
  if after
    command :linsert, [key, :after, after, value]
  elsif before
    command :linsert, [key, :before, before, value]
  else
    raise ArgumentError, "You need to pass one of [:after, :before] keywords"
  end
end

#overwrite(new_array) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/conflow/redis/array_field.rb', line 55

def overwrite(new_array)
  redis.with do |conn|
    conn.pipelined do
      conn.del(key)
      conn.rpush(key, new_array)
    end
  end
end

#popObject



35
36
37
# File 'lib/conflow/redis/array_field.rb', line 35

def pop
  command :rpop, [key]
end

#push(*values) ⇒ Object Also known as: <<



39
40
41
# File 'lib/conflow/redis/array_field.rb', line 39

def push(*values)
  command :rpush, [key, values]
end

#shiftObject



47
48
49
# File 'lib/conflow/redis/array_field.rb', line 47

def shift
  command :lpop, [key]
end

#sizeObject



27
28
29
# File 'lib/conflow/redis/array_field.rb', line 27

def size
  command :llen, [key]
end

#to_aObject Also known as: to_ary



31
32
33
# File 'lib/conflow/redis/array_field.rb', line 31

def to_a
  command :lrange, [key, 0, -1]
end

#to_sObject



76
77
78
# File 'lib/conflow/redis/array_field.rb', line 76

def to_s
  to_a.to_s
end

#unshift(value) ⇒ Object



51
52
53
# File 'lib/conflow/redis/array_field.rb', line 51

def unshift(value)
  command :lpush, [key, value]
end