Class: Conflow::Redis::SetField

Inherits:
Field
  • Object
show all
Includes:
Enumerable
Defined in:
lib/conflow/redis/set_field.rb

Overview

Represents Redis list. It’s methods mirror most used Set methods.

Instance Attribute Summary

Attributes inherited from Field

#key

Instance Method Summary collapse

Methods inherited from Field

#initialize

Constructor Details

This class inherits a constructor from Conflow::Redis::Field

Instance Method Details

#add(*values) ⇒ String

Adds one or more elements to the set.

Examples:

Adding multiple fields

field.add(:last, :first, :other, :first)

Parameters:

  • values (String)

    list of values to be added

Returns:

  • (String)

    Redis response



15
16
17
# File 'lib/conflow/redis/set_field.rb', line 15

def add(*values)
  command :sadd, [key, values]
end

#overwrite(enumerable) ⇒ String

Removes old values from the set and overrides them with new.

Parameters:

  • enumerable (Enumerable)

    new values of the set

Returns:

  • (String)

    Redis response



27
28
29
30
31
32
33
34
# File 'lib/conflow/redis/set_field.rb', line 27

def overwrite(enumerable)
  redis.with do |conn|
    conn.pipelined do
      conn.del(key)
      conn.sadd(key, enumerable)
    end
  end
end

#sizeInteger

Returns Number of elements in the set.

Returns:

  • (Integer)

    Number of elements in the set



20
21
22
# File 'lib/conflow/redis/set_field.rb', line 20

def size
  command :scard, [key]
end

#to_aArray Also known as: to_ary

Returns array with set values

Returns:

  • (Array)

    values from the set



38
39
40
# File 'lib/conflow/redis/set_field.rb', line 38

def to_a
  command :smembers, [key]
end