Class: Ohm::Set

Inherits:
Collection show all
Defined in:
lib/ohm/collection.rb

Overview

Represents a Redis set.

Examples:

Use a set attribute.

class Company < Ohm::Model
  attribute :name
  set :employees
end

company = Company.create :name => "Redis Co."
company.employees << "Albert"
company.employees << "Benoit"
company.employees.all       #=> ["Albert", "Benoit"]
company.employees.include?("Albert")  #=> true

Instance Method Summary (collapse)

Methods inherited from Collection

#==, #[], #clear, #concat, #each, #empty?, #first, #initialize, #replace, #sort, #to_ary

Constructor Details

This class inherits a constructor from Ohm::Collection

Instance Method Details

- (Object) <<(value)

Parameters:

  • (#to_s) value

    Adds value to the list.



161
162
163
# File 'lib/ohm/collection.rb', line 161

def << value
  db.sadd(key, value)
end

- (Object) all



173
174
175
# File 'lib/ohm/collection.rb', line 173

def all
  db.smembers(key)
end

- (Object) delete(value)



165
166
167
# File 'lib/ohm/collection.rb', line 165

def delete(value)
  db.srem(key, value)
end

- (Boolean) include?(value)

Returns:

  • (Boolean)


169
170
171
# File 'lib/ohm/collection.rb', line 169

def include?(value)
  db.sismember(key, value)
end

- (Object) inspect



182
183
184
# File 'lib/ohm/collection.rb', line 182

def inspect
  "#<Set: #{all.inspect}>"
end

- (Integer) size

Returns the number of elements in the set.

Returns:

  • (Integer)

    Returns the number of elements in the set.



178
179
180
# File 'lib/ohm/collection.rb', line 178

def size
  db.scard(key)
end