class Restruct::Array

Public Instance Methods

<<(*elements)
Alias for: push
[](*args) click to toggle source
# 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) click to toggle source
# File lib/restruct/array.rb, line 47
def []=(index, element)
  validate_index_type! index
  validate_index_bounds! index

  connection.lazy 'LSET', id, index, serialize(element)
end
at(index) click to toggle source
# File lib/restruct/array.rb, line 9
def at(index)
  deserialize connection.call('LINDEX', id, index)
end
clear() click to toggle source
# File lib/restruct/array.rb, line 114
def clear
  destroy
  self
end
concat(array) click to toggle source
# File lib/restruct/array.rb, line 67
def concat(array)
  push *array
end
count()
Alias for: size
delete(element) click to toggle source
# File lib/restruct/array.rb, line 87
def delete(element)
  removed_count = connection.lazy 'LREM', id, 0, serialize(element)
  removed_count && removed_count > 0 ? element : nil
end
delete_at(index) click to toggle source
# 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_if() { |e| ... } click to toggle source
# File lib/restruct/array.rb, line 103
def delete_if
  each { |e| delete e if yield e }
  self
end
each() { |at(index), index| ... } click to toggle source
# File lib/restruct/array.rb, line 134
def each
  index = 0
  while index < size
    yield at(index), index
    index += 1
  end
end
each_index() { |i| ... } click to toggle source
# File lib/restruct/array.rb, line 142
def each_index
  each { |_,i| yield i }
end
empty?() click to toggle source
# File lib/restruct/array.rb, line 125
def empty?
  size == 0
end
fetch(index, default=nil, &block) click to toggle source
# 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
first() click to toggle source
# File lib/restruct/array.rb, line 146
def first
  at 0
end
include?(element) click to toggle source
# File lib/restruct/array.rb, line 129
def include?(element)
  each { |e| return true if e == element }
  false
end
insert(index, *elements) click to toggle source
# 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_if() { |e| ... } click to toggle source
# File lib/restruct/array.rb, line 108
def keep_if
  each { |e| delete e unless yield e }
  self
end
Also aliased as: select!
last() click to toggle source
# File lib/restruct/array.rb, line 150
def last
  at -1
end
length()
Alias for: size
pop(count=1) click to toggle source
# File lib/restruct/array.rb, line 71
def pop(count=1)
  if count == 1
    deserialize connection.lazy('RPOP', id)
  else
    [count, size].min.times.map { pop }.reverse
  end
end
push(*elements) click to toggle source
# File lib/restruct/array.rb, line 54
def push(*elements)
  connection.lazy 'RPUSH', id, *elements.map { |e| serialize e }
  self
end
Also aliased as: <<
select!()
Alias for: keep_if
shift(count=1) click to toggle source
# File lib/restruct/array.rb, line 79
def shift(count=1)
  if count == 1
    deserialize connection.lazy('LPOP', id)
  else
    [count, size].min.times.map { shift }
  end
end
size() click to toggle source
# File lib/restruct/array.rb, line 119
def size
  connection.call 'LLEN', id
end
Also aliased as: count, length
to_a() click to toggle source
# File lib/restruct/array.rb, line 154
def to_a
  range 0, -1
end
Also aliased as: to_ary, to_primitive
to_ary()
Alias for: to_a
to_primitive()
Alias for: to_a
values_at(*args) click to toggle source
# 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

Private Instance Methods

deserialize(string) click to toggle source
# File lib/restruct/array.rb, line 183
def deserialize(string)
  string
end
out_of_bounds?(index) click to toggle source
# File lib/restruct/array.rb, line 175
def out_of_bounds?(index)
  !(-size..size).include?(index)
end
range(start, stop) click to toggle source
# File lib/restruct/array.rb, line 162
def range(start, stop)
  return nil if start > size
  connection.call('LRANGE', id, start, stop).map { |e| deserialize e }
end
serialize(string) click to toggle source
# File lib/restruct/array.rb, line 179
def serialize(string)
  string
end
validate_index_bounds!(index) click to toggle source
# File lib/restruct/array.rb, line 171
def validate_index_bounds!(index)
  raise IndexError, "index #{index} outside of array bounds: -#{size}...#{size}" if out_of_bounds? index
end
validate_index_type!(index) click to toggle source
# File lib/restruct/array.rb, line 167
def validate_index_type!(index)
  raise TypeError, "no implicit conversion from #{index.nil? ? 'nil' : index.class.name.downcase} to integer" unless index.is_a? Integer
end