Class: Ohm::List

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

Overview

Represents a Redis list.

Examples:

Use a list attribute.

class Event < Ohm::Model
  attribute :name
  list :participants
end

event = Event.create :name => "Redis Meeting"
event.participants << "Albert"
event.participants << "Benoit"
event.participants.all
# => ["Albert", "Benoit"]

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) Also known as: push

Parameters:

  • (#to_s) value

    Pushes value to the tail of the list.



104
105
106
# File 'lib/ohm/collection.rb', line 104

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

- (Array) all

Elements of the list.

Returns:

  • (Array)

    Elements of the list.



126
127
128
# File 'lib/ohm/collection.rb', line 126

def all
  db.lrange(key, 0, -1)
end

- (Boolean) include?(value)

Returns:

  • (Boolean)


135
136
137
# File 'lib/ohm/collection.rb', line 135

def include?(value)
  all.include?(value)
end

- (Object) inspect



139
140
141
# File 'lib/ohm/collection.rb', line 139

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

- (String) pop

Return and remove the last element of the list.

Returns:

  • (String)

    Return and remove the last element of the list.



111
112
113
# File 'lib/ohm/collection.rb', line 111

def pop
  db.rpop(key)
end

- (String) shift

Return and remove the first element of the list.

Returns:

  • (String)

    Return and remove the first element of the list.



116
117
118
# File 'lib/ohm/collection.rb', line 116

def shift
  db.lpop(key)
end

- (Integer) size

Returns the number of elements in the list.

Returns:

  • (Integer)

    Returns the number of elements in the list.



131
132
133
# File 'lib/ohm/collection.rb', line 131

def size
  db.llen(key)
end

- (Object) unshift(value)

Parameters:

  • (#to_s) value

    Pushes value to the head of the list.



121
122
123
# File 'lib/ohm/collection.rb', line 121

def unshift(value)
  db.lpush(key, value)
end