Class: Familia::ListKey

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

Instance Attribute Summary collapse

Attributes included from Settings

#current_key_version, #default_expiration, #delim, #encryption_keys, #encryption_personalization, #logical_database, #prefix, #schema_path, #schema_validator, #schemas, #suffix, #transaction_mode

Instance Method Summary collapse

Methods included from Features::Autoloader

autoload_files, included, normalize_to_config_name

Methods included from DataType::Serialization

#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value

Methods included from DataType::DatabaseCommands

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

Methods included from DataType::Connection

#dbclient, #dbkey, #direct_access, #uri

Methods included from Connection::Behavior

#connect, #create_dbclient, #multi, #normalize_uri, #pipeline, #pipelined, #transaction, #uri=, #url, #url=

Methods included from Settings

#configure, #default_suffix, #pipelined_mode, #pipelined_mode=

Methods included from Base

add_feature, #as_json, #expired?, #expires?, find_feature, #generate_id, #to_json, #to_s, #ttl, #update_expiration, #uuid

Constructor Details

This class inherits a constructor from Familia::DataType

Instance Attribute Details

#features_enabledObject (readonly) Originally defined in module Features

Returns the value of attribute features_enabled.

#logical_database(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

#parentObject Originally defined in module DataType::ClassMethods

Returns the value of attribute parent.

#prefixObject Originally defined in module DataType::ClassMethods

Returns the value of attribute prefix.

#suffixObject Originally defined in module DataType::ClassMethods

Returns the value of attribute suffix.

#uri(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

Returns the value of attribute uri.

Instance Method Details

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



29
30
31
# File 'lib/familia/data_type/types/listkey.rb', line 29

def <<(val)
  push(val)
end

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



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/familia/data_type/types/listkey.rb', line 52

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



134
135
136
# File 'lib/familia/data_type/types/listkey.rb', line 134

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

#collectObject



118
119
120
# File 'lib/familia/data_type/types/listkey.rb', line 118

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

#collectrawObject



126
127
128
# File 'lib/familia/data_type/types/listkey.rb', line 126

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

#eachObject



102
103
104
# File 'lib/familia/data_type/types/listkey.rb', line 102

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

#each_with_indexObject



106
107
108
# File 'lib/familia/data_type/types/listkey.rb', line 106

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

#eachrawObject



110
111
112
# File 'lib/familia/data_type/types/listkey.rb', line 110

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

#eachraw_with_indexObject



114
115
116
# File 'lib/familia/data_type/types/listkey.rb', line 114

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

#element_countInteger Also known as: size, length, count

Returns the number of elements in the list

Returns:

  • (Integer)

    number of elements



9
10
11
# File 'lib/familia/data_type/types/listkey.rb', line 9

def element_count
  dbclient.llen dbkey
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/familia/data_type/types/listkey.rb', line 16

def empty?
  element_count.zero?
end

#firstObject



138
139
140
# File 'lib/familia/data_type/types/listkey.rb', line 138

def first
  at 0
end

#lastObject



142
143
144
# File 'lib/familia/data_type/types/listkey.rb', line 142

def last
  at(-1)
end

#member?(value) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/familia/data_type/types/listkey.rb', line 67

def member?(value)
  !dbclient.lpos(dbkey, serialize_value(value)).nil?
end

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



89
90
91
92
93
# File 'lib/familia/data_type/types/listkey.rb', line 89

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

#membersraw(count = -1)) ⇒ Object



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

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

#popObject



44
45
46
# File 'lib/familia/data_type/types/listkey.rb', line 44

def pop
  deserialize_value dbclient.rpop(dbkey)
end

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



20
21
22
23
24
25
26
# File 'lib/familia/data_type/types/listkey.rb', line 20

def push *values
  echo :push, Familia.pretty_stack(limit: 1) 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



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

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

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



85
86
87
# File 'lib/familia/data_type/types/listkey.rb', line 85

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



75
76
77
# File 'lib/familia/data_type/types/listkey.rb', line 75

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

#selectObject



122
123
124
# File 'lib/familia/data_type/types/listkey.rb', line 122

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

#selectrawObject



130
131
132
# File 'lib/familia/data_type/types/listkey.rb', line 130

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

#shiftObject



48
49
50
# File 'lib/familia/data_type/types/listkey.rb', line 48

def shift
  deserialize_value dbclient.lpop(dbkey)
end

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



35
36
37
38
39
40
41
# File 'lib/familia/data_type/types/listkey.rb', line 35

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