Class: Familia::List

Inherits:
DataType show all
Defined in:
lib/familia/data_type/types/list.rb

Instance Attribute Summary

Attributes inherited from DataType

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

Attributes included from Features

#features_enabled

Instance Method Summary collapse

Methods inherited from DataType

#class?, #dbclient, #dbkey, #initialize, #logical_database, #parent?, #parent_class?, #parent_instance?, #uri

Methods included from Features

#feature

Methods included from DataType::ClassMethods

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

Methods included from DataType::Serialization

#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value

Methods included from DataType::Commands

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

Methods included from Base

add_feature, #generate_id, #to_s, #update_expiration, #uuid

Constructor Details

This class inherits a constructor from Familia::DataType

Instance Method Details

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



25
26
27
# File 'lib/familia/data_type/types/list.rb', line 25

def <<(val)
  push val
end

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



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

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



125
126
127
# File 'lib/familia/data_type/types/list.rb', line 125

def at(idx)
  deserialize_value dbclient.lindex(dbkey, idx)
end

#collectObject



109
110
111
# File 'lib/familia/data_type/types/list.rb', line 109

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

#collectrawObject



117
118
119
# File 'lib/familia/data_type/types/list.rb', line 117

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

#eachObject



93
94
95
# File 'lib/familia/data_type/types/list.rb', line 93

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

#each_with_indexObject



97
98
99
# File 'lib/familia/data_type/types/list.rb', line 97

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

#eachrawObject



101
102
103
# File 'lib/familia/data_type/types/list.rb', line 101

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

#eachraw_with_indexObject



105
106
107
# File 'lib/familia/data_type/types/list.rb', line 105

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

#element_countInteger Also known as: size

Returns the number of elements in the list

Returns:

  • (Integer)

    number of elements



7
8
9
# File 'lib/familia/data_type/types/list.rb', line 7

def element_count
  dbclient.llen dbkey
end

#empty?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/familia/data_type/types/list.rb', line 12

def empty?
  element_count.zero?
end

#firstObject



129
130
131
# File 'lib/familia/data_type/types/list.rb', line 129

def first
  at 0
end

#lastObject



133
134
135
# File 'lib/familia/data_type/types/list.rb', line 133

def last
  at(-1)
end

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



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

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



88
89
90
91
# File 'lib/familia/data_type/types/list.rb', line 88

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

#popObject



39
40
41
# File 'lib/familia/data_type/types/list.rb', line 39

def pop
  deserialize_value dbclient.rpop(dbkey)
end

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



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

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

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



71
72
73
74
# File 'lib/familia/data_type/types/list.rb', line 71

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

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



76
77
78
# File 'lib/familia/data_type/types/list.rb', line 76

def rangeraw(sidx = 0, eidx = -1)
  dbclient.lrange(dbkey, 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



66
67
68
# File 'lib/familia/data_type/types/list.rb', line 66

def remove_element(value, count = 0)
  dbclient.lrem dbkey, count, serialize_value(value)
end

#selectObject



113
114
115
# File 'lib/familia/data_type/types/list.rb', line 113

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

#selectrawObject



121
122
123
# File 'lib/familia/data_type/types/list.rb', line 121

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

#shiftObject



43
44
45
# File 'lib/familia/data_type/types/list.rb', line 43

def shift
  deserialize_value dbclient.lpop(dbkey)
end

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



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

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