Class: Familia::List

Inherits:
RedisType show all
Defined in:
lib/familia/redistype/types/list.rb

Instance Attribute Summary

Attributes inherited from RedisType

#dump_method, #keystring, #load_method, #opts, #parent

Attributes included from Features

#features_enabled

Instance Method Summary collapse

Methods inherited from RedisType

#class?, #db, #initialize, #parent?, #parent_class?, #parent_instance?, #redis, #rediskey, #uri

Methods included from Features

#feature

Methods included from RedisType::ClassMethods

#db, #has_relations?, #inherited, #register, #uri, #valid_keys_only

Methods included from RedisType::Serialization

#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value

Methods included from RedisType::Commands

#delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #realttl, #rename, #renamenx, #type

Methods included from Base

add_feature, #generate_id, #update_expiration, #uuid

Constructor Details

This class inherits a constructor from Familia::RedisType

Instance Method Details

#<<(val) ⇒ Object Also known as: add



26
27
28
# File 'lib/familia/redistype/types/list.rb', line 26

def <<(val)
  push val
end

#[](idx, count = nil) ⇒ Object Also known as: slice



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/familia/redistype/types/list.rb', line 48

def [](idx, count = nil)
  if idx.is_a? Range
    range idx.first, idx.last
  elsif count
    case count <=> 0
    when 1  then range(idx, idx + count - 1)
    when 0  then []
    when -1 then nil
    end
  else
    at idx
  end
end

#at(idx) ⇒ Object



126
127
128
# File 'lib/familia/redistype/types/list.rb', line 126

def at(idx)
  deserialize_value redis.lindex(rediskey, idx)
end

#collect(&blk) ⇒ Object



110
111
112
# File 'lib/familia/redistype/types/list.rb', line 110

def collect(&blk)
  range.collect(&blk)
end

#collectraw(&blk) ⇒ Object



118
119
120
# File 'lib/familia/redistype/types/list.rb', line 118

def collectraw(&blk)
  rangeraw.collect(&blk)
end

#each(&blk) ⇒ Object



94
95
96
# File 'lib/familia/redistype/types/list.rb', line 94

def each(&blk)
  range.each(&blk)
end

#each_with_index(&blk) ⇒ Object



98
99
100
# File 'lib/familia/redistype/types/list.rb', line 98

def each_with_index(&blk)
  range.each_with_index(&blk)
end

#eachraw(&blk) ⇒ Object



102
103
104
# File 'lib/familia/redistype/types/list.rb', line 102

def eachraw(&blk)
  rangeraw.each(&blk)
end

#eachraw_with_index(&blk) ⇒ Object



106
107
108
# File 'lib/familia/redistype/types/list.rb', line 106

def eachraw_with_index(&blk)
  rangeraw.each_with_index(&blk)
end

#element_countInteger Also known as: size

Returns the number of elements in the list

Returns:

  • (Integer)

    number of elements



8
9
10
# File 'lib/familia/redistype/types/list.rb', line 8

def element_count
  redis.llen rediskey
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/familia/redistype/types/list.rb', line 13

def empty?
  element_count.zero?
end

#firstObject



130
131
132
# File 'lib/familia/redistype/types/list.rb', line 130

def first
  at 0
end

#lastObject



134
135
136
# File 'lib/familia/redistype/types/list.rb', line 134

def last
  at(-1)
end

#members(count = -1)) ⇒ Object Also known as: all, to_a



81
82
83
84
85
# File 'lib/familia/redistype/types/list.rb', line 81

def members(count = -1)
  echo :members, caller(1..1).first if Familia.debug
  count -= 1 if count.positive?
  range 0, count
end

#membersraw(count = -1)) ⇒ Object



89
90
91
92
# File 'lib/familia/redistype/types/list.rb', line 89

def membersraw(count = -1)
  count -= 1 if count.positive?
  rangeraw 0, count
end

#popObject



40
41
42
# File 'lib/familia/redistype/types/list.rb', line 40

def pop
  deserialize_value redis.rpop(rediskey)
end

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



17
18
19
20
21
22
23
# File 'lib/familia/redistype/types/list.rb', line 17

def push *values
  echo :push, caller(1..1).first if Familia.debug
  values.flatten.compact.each { |v| redis.rpush rediskey, serialize_value(v) }
  redis.ltrim rediskey, -@opts[:maxlength], -1 if @opts[:maxlength]
  update_expiration
  self
end

#range(sidx = 0, eidx = -1)) ⇒ Object



72
73
74
75
# File 'lib/familia/redistype/types/list.rb', line 72

def range(sidx = 0, eidx = -1)
  elements = rangeraw sidx, eidx
  deserialize_values(*elements)
end

#rangeraw(sidx = 0, eidx = -1)) ⇒ Object



77
78
79
# File 'lib/familia/redistype/types/list.rb', line 77

def rangeraw(sidx = 0, eidx = -1)
  redis.lrange(rediskey, sidx, eidx)
end

#remove_element(value, count = 0) ⇒ Integer Also known as: remove

Removes elements equal to value from the list

Parameters:

  • value

    The value to remove

  • count (Integer) (defaults to: 0)

    Number of elements to remove (0 means all)

Returns:

  • (Integer)

    The number of removed elements



67
68
69
# File 'lib/familia/redistype/types/list.rb', line 67

def remove_element(value, count = 0)
  redis.lrem rediskey, count, serialize_value(value)
end

#select(&blk) ⇒ Object



114
115
116
# File 'lib/familia/redistype/types/list.rb', line 114

def select(&blk)
  range.select(&blk)
end

#selectraw(&blk) ⇒ Object



122
123
124
# File 'lib/familia/redistype/types/list.rb', line 122

def selectraw(&blk)
  rangeraw.select(&blk)
end

#shiftObject



44
45
46
# File 'lib/familia/redistype/types/list.rb', line 44

def shift
  deserialize_value redis.lpop(rediskey)
end

#unshift(*values) ⇒ Object Also known as: prepend



31
32
33
34
35
36
37
# File 'lib/familia/redistype/types/list.rb', line 31

def unshift *values
  values.flatten.compact.each { |v| redis.lpush rediskey, serialize_value(v) }
  # TODO: test maxlength
  redis.ltrim rediskey, 0, @opts[:maxlength] - 1 if @opts[:maxlength]
  update_expiration
  self
end