Class: Restruct::Array

Inherits:
Structure show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/restruct/array.rb

Direct Known Subclasses

MarshalArray

Instance Attribute Summary

Attributes inherited from Structure

#id, #redis

Instance Method Summary collapse

Methods inherited from Structure

#==, #destroy, #dump, #initialize, #restore

Constructor Details

This class inherits a constructor from Restruct::Structure

Instance Method Details

#[](*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/restruct/array.rb', line 31

def [](*args)
  if args.count == 1
    if args[0].is_a? Integer
      at args[0].to_i
    elsif args[0].is_a? Range
      range args[0].first, args[0].last
    else
      validate_index_type! args.first
    end
  elsif args.count == 2
    range args[0], args[0] + args[1] - 1
  else
    raise ArgumentError, "wrong number of arguments (#{args.count} for 1..2)"
  end
end

#[]=(index, element) ⇒ Object



47
48
49
50
51
52
# File 'lib/restruct/array.rb', line 47

def []=(index, element)
  validate_index_type! index
  validate_index_bounds! index

  redis.call 'LSET', id, index, serialize(element)
end

#at(index) ⇒ Object



9
10
11
# File 'lib/restruct/array.rb', line 9

def at(index)
  deserialize redis.call('LINDEX', id, index)
end

#clearObject



114
115
116
117
# File 'lib/restruct/array.rb', line 114

def clear
  destroy
  self
end

#concat(array) ⇒ Object



67
68
69
# File 'lib/restruct/array.rb', line 67

def concat(array)
  push *array
end

#delete(element) ⇒ Object



87
88
89
90
# File 'lib/restruct/array.rb', line 87

def delete(element)
  removed_count = redis.call 'LREM', id, 0, serialize(element)
  removed_count > 0 ? element : nil
end

#delete_at(index) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/restruct/array.rb', line 92

def delete_at(index)
  validate_index_type! index
  return nil if out_of_bounds? index
  
  element = at index
  tail_size = index >= 0 ? size - index : size - (size + index)
  tail = Array(pop(tail_size))
  push *tail[1..-1]
  element
end

#delete_ifObject



103
104
105
106
# File 'lib/restruct/array.rb', line 103

def delete_if
  each { |e| delete e if yield e }
  self
end

#eachObject



134
135
136
137
138
139
140
# File 'lib/restruct/array.rb', line 134

def each
  index = 0
  while index < size
    yield at(index), index
    index += 1
  end
end

#each_indexObject



142
143
144
# File 'lib/restruct/array.rb', line 142

def each_index
  each { |_,i| yield i }
end

#empty?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/restruct/array.rb', line 125

def empty?
  size == 0
end

#fetch(index, default = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/restruct/array.rb', line 20

def fetch(index, default=nil, &block)
  validate_index_type! index

  if index < size
    at index
  else
    validate_index_bounds! index if default.nil? && block.nil?
    default || block.call(index)
  end
end

#firstObject



146
147
148
# File 'lib/restruct/array.rb', line 146

def first
  at 0
end

#include?(element) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/restruct/array.rb', line 129

def include?(element)
  each { |e| return true if e == element }
  false
end

#insert(index, *elements) ⇒ Object



60
61
62
63
64
65
# File 'lib/restruct/array.rb', line 60

def insert(index, *elements)
  validate_index_type! index
  tail_size = index >= 0 ? size - index : size - (size + index) - 1
  tail = Array(pop(tail_size))
  push *(elements + tail)
end

#keep_ifObject Also known as: select!



108
109
110
111
# File 'lib/restruct/array.rb', line 108

def keep_if
  each { |e| delete e unless yield e }
  self
end

#lastObject



150
151
152
# File 'lib/restruct/array.rb', line 150

def last
  at -1
end

#pop(count = 1) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/restruct/array.rb', line 71

def pop(count=1)
  if count == 1
    deserialize redis.call('RPOP', id)
  else
    [count, size].min.times.map { pop }.reverse
  end
end

#push(*elements) ⇒ Object Also known as: <<



54
55
56
57
# File 'lib/restruct/array.rb', line 54

def push(*elements)
  redis.call 'RPUSH', id, *elements.map { |e| serialize e }
  self
end

#shift(count = 1) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/restruct/array.rb', line 79

def shift(count=1)
  if count == 1
    deserialize redis.call('LPOP', id)
  else
    [count, size].min.times.map { shift }
  end
end

#sizeObject Also known as: count, length



119
120
121
# File 'lib/restruct/array.rb', line 119

def size
  redis.call 'LLEN', id
end

#to_aObject Also known as: to_ary, to_primitive



154
155
156
# File 'lib/restruct/array.rb', line 154

def to_a
  range 0, -1
end

#values_at(*args) ⇒ Object



13
14
15
16
17
18
# File 'lib/restruct/array.rb', line 13

def values_at(*args)
  args.each_with_object([]) do |arg, array|
    elements = self[arg]
    array.push *(elements ? Array(elements) : [nil])
  end
end