Class: Ohm::Model::List

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

Overview

Provides a Ruby-esque interface to a Redis LIST. The LIST is assumed to be composed of ids which maps to Collection#model.

Instance Attribute Summary

Attributes inherited from Collection

key, model

Instance Method Summary (collapse)

Methods inherited from Collection

#add, #clear, #empty?, #initialize, #replace, #sort, #sort_by, #to_a

Constructor Details

This class inherits a constructor from Ohm::Model::Collection

Instance Method Details

- (Object) <<(model) Also known as: push

Thin wrapper around RPUSH.

Examples:

class Post < Ohm::Model
  list :comments, Comment
end

class Comment < Ohm::Model
end

p = Post.create
p.comments << Comment.create

Parameters:

  • model (#id)

    Typically an Ohm::Model instance.

See Also:



808
809
810
# File 'lib/ohm.rb', line 808

def <<(model)
  key.rpush(model.id)
end

- (Object) [](index, limit = nil)

Returns the element at index, or returns a subarray starting at `start` and continuing for `length` elements, or returns a subarray specified by `range`. Negative indices count backward from the end of the array (-1 is the last element). Returns nil if the index (or starting index) are out of range.

Examples:

class Post < Ohm::Model
  list :comments, Comment
end

class Comment < Ohm::Model
end

post = Post.create

10.times { post.comments << Comment.create }

post.comments[0] == Comment[1]
# => true

post.comments[0, 4] == (1..5).map { |i| Comment[i] }
# => true

post.comments[0, 4] == post.comments[0..4]
# => true

post.comments.all == post.comments[0, -1]
# => true

See Also:



845
846
847
848
849
850
851
852
853
854
# File 'lib/ohm.rb', line 845

def [](index, limit = nil)
  case [index, limit]
  when Pattern[Fixnum, Fixnum] then
    key.lrange(index, limit).collect { |id| model.to_proc[id] }
  when Pattern[Range, nil] then
    key.lrange(index.first, index.last).collect { |id| model.to_proc[id] }
  when Pattern[Fixnum, nil] then
    model[key.lindex(index)]
  end
end

- (Array<Ohm::Model>) all

Returns an array representation of this list, with elements of the array being an instance of Collection#model.

Returns:



903
904
905
# File 'lib/ohm.rb', line 903

def all
  key.lrange(0, -1).map(&model)
end

- (Object) each(&block)

An implementation which relies on LRANGE and yields an instance of Collection#model.

Examples:

class Post < Ohm::Model
  list :comments, Comment
end

class Comment < Ohm::Model
end

post = Post.create
post.comments.add(Comment.create)
post.comments.add(Comment.create)

post.comments.each do |comment|
  # do something with the comment
end

# reading the source reveals that this is achieved by doing:
post.comments.key.lrange(0, -1).each do |id|
  comment = Comment[id]
  # do something with the comment
end

See Also:



786
787
788
# File 'lib/ohm.rb', line 786

def each(&block)
  key.lrange(0, -1).each { |id| block.call(model.to_proc[id]) }
end

- (Ohm::Model?) first

Convience method for doing list[0], similar to Ruby’s Array#first method.

Returns:



861
862
863
# File 'lib/ohm.rb', line 861

def first
  self[0]
end

- (true, false) include?(model)

Ruby-like interface wrapper around LRANGE.

Parameters:

  • model (#id)

    Typically an Ohm::Model instance.

Returns:

  • (true, false)

    Whether or not the Ohm::Model instance is an element of this list.

See Also:



926
927
928
# File 'lib/ohm.rb', line 926

def include?(model)
  key.lrange(0, -1).include?(model.id)
end

- (Object) inspect



930
931
932
# File 'lib/ohm.rb', line 930

def inspect
  "#<List (#{model}): #{key.lrange(0, -1).inspect}>"
end

- (Ohm::Model?) pop

Returns the model at the tail of this list, while simultaneously removing it from the list.

Returns:

See Also:



873
874
875
# File 'lib/ohm.rb', line 873

def pop
  model[key.rpop]
end

- (Ohm::Model?) shift

Returns the model at the head of this list, while simultaneously removing it from the list.

Returns:

See Also:



885
886
887
# File 'lib/ohm.rb', line 885

def shift
  model[key.lpop]
end

- (Fixnum) size

Thin Ruby interface wrapper for LLEN.

Returns:

  • (Fixnum)

    The total number of elements for this list.

See Also:



913
914
915
# File 'lib/ohm.rb', line 913

def size
  key.llen
end

- (Object) unshift(model)

Prepends an Ohm::Model instance at the beginning of this list.

Parameters:

  • model (#id)

    Typically an Ohm::Model instance.

See Also:



895
896
897
# File 'lib/ohm.rb', line 895

def unshift(model)
  key.lpush(model.id)
end