Class: Familia::UnsortedSet

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

Overview

Familia::UnsortedSet

Instance Attribute Summary collapse

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::Commands

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

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

#<<(v) ⇒ Object



27
28
29
# File 'lib/familia/data_type/types/unsorted_set.rb', line 27

def <<(v)
  add v
end

#add(*values) ⇒ Object Also known as: add_element



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

def add *values
  values.flatten.compact.each { |v| dbclient.sadd? dbkey, serialize_value(v) }
  update_expiration
  self
end

#collectObject



51
52
53
# File 'lib/familia/data_type/types/unsorted_set.rb', line 51

def collect(&)
  members.collect(&)
end

#collectrawObject



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

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

#eachObject



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

def each(&)
  members.each(&)
end

#each_with_indexObject



47
48
49
# File 'lib/familia/data_type/types/unsorted_set.rb', line 47

def each_with_index(&)
  members.each_with_index(&)
end

#eachrawObject



59
60
61
# File 'lib/familia/data_type/types/unsorted_set.rb', line 59

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

#eachraw_with_indexObject



63
64
65
# File 'lib/familia/data_type/types/unsorted_set.rb', line 63

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

#element_countInteger Also known as: size, length, count

Returns the number of elements in the unsorted set

Returns:

  • (Integer)

    number of elements



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

def element_count
  dbclient.scard dbkey
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  element_count.zero?
end

#intersection(*setkeys) ⇒ Object



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

def intersection *setkeys
  # TODO
end

#member?(val) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


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

def member?(val)
  dbclient.sismember dbkey, serialize_value(val)
end

#membersObject Also known as: all, to_a



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

def members
  echo :members, Familia.pretty_stack(limit: 1) if Familia.debug
  elements = membersraw
  deserialize_values(*elements)
end

#membersrawObject



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

def membersraw
  dbclient.smembers(dbkey)
end

#move(dstkey, val) ⇒ Object



96
97
98
# File 'lib/familia/data_type/types/unsorted_set.rb', line 96

def move(dstkey, val)
  dbclient.smove dbkey, dstkey, val
end

#popObject



92
93
94
# File 'lib/familia/data_type/types/unsorted_set.rb', line 92

def pop
  dbclient.spop dbkey
end

#remove_element(value) ⇒ Integer Also known as: remove

Removes a member from the set

Parameters:

  • value

    The value to remove from the set

Returns:

  • (Integer)

    The number of members that were removed (0 or 1)



83
84
85
# File 'lib/familia/data_type/types/unsorted_set.rb', line 83

def remove_element(value)
  dbclient.srem dbkey, serialize_value(value)
end

#sample(count = 1) ⇒ Array

Get one or more random members from the set

Parameters:

  • count (Integer) (defaults to: 1)

    Number of random members to return (default: 1)

Returns:

  • (Array)

    Array of deserialized random members



103
104
105
# File 'lib/familia/data_type/types/unsorted_set.rb', line 103

def sample(count = 1)
  deserialize_values(*sampleraw(count))
end

#sampleraw(count = 1) ⇒ Array Also known as: random

Get one or more random members from the set without deserialization

Parameters:

  • count (Integer) (defaults to: 1)

    Number of random members to return (default: 1)

Returns:

  • (Array)

    Array of raw random members



111
112
113
# File 'lib/familia/data_type/types/unsorted_set.rb', line 111

def sampleraw(count = 1)
  dbclient.srandmember(dbkey, count) || []
end

#selectObject



55
56
57
# File 'lib/familia/data_type/types/unsorted_set.rb', line 55

def select(&)
  members.select(&)
end

#selectrawObject



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

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