Class: Redpear::Store::List

Inherits:
Enumerable show all
Defined in:
lib/redpear/store/list.rb

Constant Summary

Constants inherited from Base

Base::IS_NIL, Base::IS_ONE, Base::IS_TRUE, Base::IS_ZERO, Base::PICK_FIRST, Base::TO_INT, Base::TO_SET

Instance Attribute Summary

Attributes inherited from Base

#conn, #key

Instance Method Summary collapse

Methods inherited from Enumerable

#count, #size

Methods inherited from Base

#clear, #exists?, #expire, #expire_at, #expire_in, #initialize, #inspect, #purge!, temporary, #ttl, #type, #value, #watch

Constructor Details

This class inherits a constructor from Redpear::Store::Base

Instance Method Details

#==(other) ⇒ Boolean

Comparator

Parameters:

Returns:

  • (Boolean)

    true if items match



114
115
116
# File 'lib/redpear/store/list.rb', line 114

def ==(other)
  other.respond_to?(:to_a) && other.to_a == to_a
end

#allArray Also known as: to_a

Returns all items.

Returns:

  • (Array)

    all items



10
11
12
# File 'lib/redpear/store/list.rb', line 10

def all
  slice(0..-1)
end

#delete(item, count = 0) ⇒ Object

Remove ‘item` from list

Parameters:

  • item (String)
  • count (Integer) (defaults to: 0)

    The number of items to remove. When =0 - remove all occurences When >0 - remove the first ‘count` items When <0 - remove the last `count` items



125
126
127
# File 'lib/redpear/store/list.rb', line 125

def delete(item, count = 0)
  conn.lrem key, count, item
end

#each {|item| ... } ⇒ Object

Yields:

  • over each item in the list

Yield Parameters:

  • item (String)


5
6
7
# File 'lib/redpear/store/list.rb', line 5

def each(&block)
  all.each(&block)
end

#insert_after(pivot, item) ⇒ Object

Insert ‘item` after `pivot`

Parameters:

  • pivot (String)
  • item (String)


139
140
141
# File 'lib/redpear/store/list.rb', line 139

def insert_after(pivot, item)
  conn.linsert key, :after, pivot, item
end

#insert_before(pivot, item) ⇒ Object

Insert ‘item` before `pivot`

Parameters:

  • pivot (String)
  • item (String)


132
133
134
# File 'lib/redpear/store/list.rb', line 132

def insert_before(pivot, item)
  conn.linsert key, :before, pivot, item
end

#lengthInteger

Returns the number of items in the set.

Returns:

  • (Integer)

    the number of items in the set



71
72
73
# File 'lib/redpear/store/list.rb', line 71

def length
  conn.llen key
end

#popString

Removes the last item

Returns:

  • (String)

    the removed item



87
88
89
# File 'lib/redpear/store/list.rb', line 87

def pop
  conn.rpop key
end

#pop_unshift(target) ⇒ Object

Removes the last item and prepends it to ‘target`

Parameters:



107
108
109
# File 'lib/redpear/store/list.rb', line 107

def pop_unshift(target)
  conn.rpoplpush key, target.to_s
end

#push(item) ⇒ Object Also known as: <<

Appends a single item. Chainable example:

list << 'a' << 'b'

Parameters:

  • item (String)

    A item to add



79
80
81
82
# File 'lib/redpear/store/list.rb', line 79

def push(item)
  conn.rpush key, item
  self
end

#range(start, finish) ⇒ Array

Returns items.

Parameters:

  • start (Integer)
  • finish (Integer)

Returns:

  • (Array)

    items



60
61
62
# File 'lib/redpear/store/list.rb', line 60

def range(start, finish)
  conn.lrange(key, start, finish) || []
end

#shiftString

Removes the first item

Returns:

  • (String)

    the removed item



101
102
103
# File 'lib/redpear/store/list.rb', line 101

def shift
  conn.lpop key
end

#slice(index) ⇒ String #slice(start, length) ⇒ Array #slice(range) ⇒ Array Also known as: []

Returns a slice of the list

Overloads:

  • #slice(index) ⇒ String

    Returns the item at ‘index`

    Parameters:

    • index (Integer)

    Returns:

    • (String)

      item

  • #slice(start, length) ⇒ Array

    Returns from ‘length` items from `start`

    Parameters:

    • start (Integer)
    • length (Integer)

    Returns:

    • (Array)

      items

  • #slice(range) ⇒ Array

    Returns items from range

    Parameters:

    • range (Range)

    Returns:

    • (Array)

      items



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/redpear/store/list.rb', line 29

def slice(start, length = nil)
  case start
  when Integer
    if length
      range(start, start + length - 1)
    else
      conn.lindex(key, start) rescue nil
    end
  when Range, Array
    range *range_pair(start)
  else
    []
  end
end

#slice(start, length) ⇒ Object

Destructive slice. Returns specified range and removes other items.



47
48
49
50
51
52
53
54
55
# File 'lib/redpear/store/list.rb', line 47

def slice!(start, length = nil)
  result = case start
  when Range, Array
    trim *range_pair(start)
  else
    trim(start, start + length - 1)
  end
  Redis::Future === result ? result : to_a
end

#trim(start, finish) ⇒ Object

Parameters:

  • start (Integer)
  • finish (Integer)


66
67
68
# File 'lib/redpear/store/list.rb', line 66

def trim(start, finish)
  conn.ltrim(key, start, finish)
end

#unshift(item) ⇒ Object

Prepends a single item.

Parameters:

  • item (String)

    A item to add



94
95
96
97
# File 'lib/redpear/store/list.rb', line 94

def unshift(item)
  conn.lpush key, item
  self
end