Class: Ohm::Model::List
- Inherits:
-
Collection
- Object
- Collection
- Ohm::Model::List
- 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
Instance Method Summary (collapse)
-
- (Object) <<(model)
(also: #push)
Thin wrapper around RPUSH.
-
- (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`.
-
- (Array<Ohm::Model>) all
Returns an array representation of this list, with elements of the array being an instance of Collection#model.
-
- (Object) each(&block)
An implementation which relies on LRANGE and yields an instance of Collection#model.
-
- (Ohm::Model?) first
Convience method for doing list[0], similar to Ruby’s Array#first method.
-
- (true, false) include?(model)
Ruby-like interface wrapper around LRANGE.
- - (Object) inspect
-
- (Ohm::Model?) pop
Returns the model at the tail of this list, while simultaneously removing it from the list.
-
- (Ohm::Model?) shift
Returns the model at the head of this list, while simultaneously removing it from the list.
-
- (Fixnum) size
Thin Ruby interface wrapper for LLEN.
-
- (Object) unshift(model)
Prepends an Ohm::Model instance at the beginning of this list.
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.
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.
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.
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.
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.
861 862 863 |
# File 'lib/ohm.rb', line 861 def first self[0] end |
- (true, false) include?(model)
Ruby-like interface wrapper around LRANGE.
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.
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.
885 886 887 |
# File 'lib/ohm.rb', line 885 def shift model[key.lpop] end |
- (Fixnum) size
Thin Ruby interface wrapper for LLEN.
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.
895 896 897 |
# File 'lib/ohm.rb', line 895 def unshift(model) key.lpush(model.id) end |