Class: Familia::ListKey
- Defined in:
- lib/familia/data_type/types/listkey.rb
Instance Attribute Summary collapse
-
#features_enabled ⇒ Object
included
from Features
readonly
Returns the value of attribute features_enabled.
- #logical_database(val = nil) ⇒ Object included from DataType::ClassMethods
-
#parent ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute parent.
-
#prefix ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute prefix.
-
#suffix ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute suffix.
-
#uri(val = nil) ⇒ Object
included
from DataType::ClassMethods
Returns the value of attribute uri.
Attributes included from Settings
#current_key_version, #default_expiration, #delim, #encryption_keys, #encryption_personalization, #logical_database, #prefix, #schema_path, #schema_validator, #schemas, #strict_write_order, #suffix, #transaction_mode
Instance Method Summary collapse
- #<<(val) ⇒ Object (also: #add_element, #add)
- #[](idx, count = nil) ⇒ Object (also: #slice)
- #at(idx) ⇒ Object
- #collect ⇒ Object
- #collectraw ⇒ Object
- #each ⇒ Object
- #each_with_index ⇒ Object
- #eachraw ⇒ Object
- #eachraw_with_index ⇒ Object
-
#element_count ⇒ Integer
(also: #size, #length, #count)
Returns the number of elements in the list.
- #empty? ⇒ Boolean
- #first ⇒ Object
- #last ⇒ Object
- #member?(value) ⇒ Boolean
- #members(count = -1)) ⇒ Object (also: #all, #to_a)
- #membersraw(count = -1)) ⇒ Object
- #pop ⇒ Object
- #push(*values) ⇒ Object (also: #append)
- #range(sidx = 0, eidx = -1)) ⇒ Object
- #rangeraw(sidx = 0, eidx = -1)) ⇒ Object
-
#remove_element(value, count = 0) ⇒ Integer
(also: #remove)
Removes elements equal to value from the list.
- #select ⇒ Object
- #selectraw ⇒ Object
- #shift ⇒ Object
- #unshift(*values) ⇒ Object (also: #prepend)
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_enabled ⇒ Object (readonly) Originally defined in module Features
Returns the value of attribute features_enabled.
#logical_database(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods
#parent ⇒ Object Originally defined in module DataType::ClassMethods
Returns the value of attribute parent.
#prefix ⇒ Object Originally defined in module DataType::ClassMethods
Returns the value of attribute prefix.
#suffix ⇒ Object 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
33 34 35 |
# File 'lib/familia/data_type/types/listkey.rb', line 33 def <<(val) push(val) end |
#[](idx, count = nil) ⇒ Object Also known as: slice
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/familia/data_type/types/listkey.rb', line 64 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
148 149 150 |
# File 'lib/familia/data_type/types/listkey.rb', line 148 def at(idx) deserialize_value dbclient.lindex(dbkey, idx) end |
#collect ⇒ Object
132 133 134 |
# File 'lib/familia/data_type/types/listkey.rb', line 132 def collect(&) range.collect(&) end |
#collectraw ⇒ Object
140 141 142 |
# File 'lib/familia/data_type/types/listkey.rb', line 140 def collectraw(&) rangeraw.collect(&) end |
#each ⇒ Object
116 117 118 |
# File 'lib/familia/data_type/types/listkey.rb', line 116 def each(&) range.each(&) end |
#each_with_index ⇒ Object
120 121 122 |
# File 'lib/familia/data_type/types/listkey.rb', line 120 def each_with_index(&) range.each_with_index(&) end |
#eachraw ⇒ Object
124 125 126 |
# File 'lib/familia/data_type/types/listkey.rb', line 124 def eachraw(&) rangeraw.each(&) end |
#eachraw_with_index ⇒ Object
128 129 130 |
# File 'lib/familia/data_type/types/listkey.rb', line 128 def eachraw_with_index(&) rangeraw.each_with_index(&) end |
#element_count ⇒ Integer Also known as: size, length, count
Returns the number of elements in the list
9 10 11 |
# File 'lib/familia/data_type/types/listkey.rb', line 9 def element_count dbclient.llen dbkey end |
#empty? ⇒ Boolean
16 17 18 |
# File 'lib/familia/data_type/types/listkey.rb', line 16 def empty? element_count.zero? end |
#first ⇒ Object
152 153 154 |
# File 'lib/familia/data_type/types/listkey.rb', line 152 def first at 0 end |
#last ⇒ Object
156 157 158 |
# File 'lib/familia/data_type/types/listkey.rb', line 156 def last at(-1) end |
#member?(value) ⇒ Boolean
79 80 81 |
# File 'lib/familia/data_type/types/listkey.rb', line 79 def member?(value) !dbclient.lpos(dbkey, serialize_value(value)).nil? end |
#members(count = -1)) ⇒ Object Also known as: all, to_a
103 104 105 106 107 |
# File 'lib/familia/data_type/types/listkey.rb', line 103 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
111 112 113 114 |
# File 'lib/familia/data_type/types/listkey.rb', line 111 def membersraw(count = -1) count -= 1 if count.positive? rangeraw 0, count end |
#pop ⇒ Object
52 53 54 55 56 |
# File 'lib/familia/data_type/types/listkey.rb', line 52 def pop ret = deserialize_value dbclient.rpop(dbkey) update_expiration ret end |
#push(*values) ⇒ Object Also known as: append
This method executes a Redis RPUSH immediately, unlike scalar field setters which are deferred until save. If the parent object has unsaved scalar field changes, consider calling save first to avoid split-brain state.
23 24 25 26 27 28 29 30 |
# File 'lib/familia/data_type/types/listkey.rb', line 23 def push *values warn_if_dirty! 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
94 95 96 97 |
# File 'lib/familia/data_type/types/listkey.rb', line 94 def range(sidx = 0, eidx = -1) elements = rangeraw sidx, eidx deserialize_values(*elements) end |
#rangeraw(sidx = 0, eidx = -1)) ⇒ Object
99 100 101 |
# File 'lib/familia/data_type/types/listkey.rb', line 99 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
87 88 89 90 91 |
# File 'lib/familia/data_type/types/listkey.rb', line 87 def remove_element(value, count = 0) ret = dbclient.lrem dbkey, count, serialize_value(value) update_expiration ret end |
#select ⇒ Object
136 137 138 |
# File 'lib/familia/data_type/types/listkey.rb', line 136 def select(&) range.select(&) end |
#selectraw ⇒ Object
144 145 146 |
# File 'lib/familia/data_type/types/listkey.rb', line 144 def selectraw(&) rangeraw.select(&) end |
#shift ⇒ Object
58 59 60 61 62 |
# File 'lib/familia/data_type/types/listkey.rb', line 58 def shift ret = deserialize_value dbclient.lpop(dbkey) update_expiration ret end |
#unshift(*values) ⇒ Object Also known as: prepend
This method executes a Redis LPUSH immediately, unlike scalar field setters which are deferred until save. If the parent object has unsaved scalar field changes, consider calling save first to avoid split-brain state.
42 43 44 45 46 47 48 49 |
# File 'lib/familia/data_type/types/listkey.rb', line 42 def unshift *values warn_if_dirty! 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 |