Class: Redisabel::KeyArray

Inherits:
KeyValue show all
Defined in:
lib/redisabel/key_array.rb

Instance Attribute Summary

Attributes inherited from KeyValue

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KeyValue

#==, #autosave=, #autosave?, database_key_name, #destroy, #empty?, #eql?, #initialize, #inspect, #load, #to_s, #value, #value=

Methods included from Transformations

detect_type, transform, transform_hash, transform_list, transform_set, transform_string, transform_zset

Methods included from Finders

#filter, #find

Constructor Details

This class inherits a constructor from Redisabel::KeyValue

Class Method Details

.data_typeObject



5
6
7
# File 'lib/redisabel/key_array.rb', line 5

def self.data_type
  return Array
end

Instance Method Details

#[](i) ⇒ Object



9
10
11
# File 'lib/redisabel/key_array.rb', line 9

def [](i)
  return @data[i]
end

#delete(value) ⇒ Object



13
14
15
16
17
# File 'lib/redisabel/key_array.rb', line 13

def delete(value)
  if @data.delete(value) && self.autosave? && !self.id.to_s.empty?
    Database.db.lrem(self.database_key, 0, value)
  end
end

#delete_at(i) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/redisabel/key_array.rb', line 19

def delete_at(i)
  if @data.delete_at(i) && self.autosave? && !self.id.to_s.empty?
    tmp_value = Time.now.to_s
    self.insert(i, tmp_value)
    Database.db.lrem(self.database_key, 0, tmp_value)
  end
end

#insert(i, value) ⇒ Object Also known as: []=



27
28
29
30
31
32
33
34
# File 'lib/redisabel/key_array.rb', line 27

def insert(i, value)
  params = i.nil? ? [value] : [i, value]
  amethod, lmethod = i.nil? ? [:push, :rpush] : [:insert, :lset]
  @data.send(amethod, *params)
  if self.autosave? && !self.id.to_s.empty?
    Database.db.send(lmethod, self.database_key, *params)
  end
end

#push(*values) ⇒ Object



37
38
39
40
# File 'lib/redisabel/key_array.rb', line 37

def push(*values)
  values = values.shift if values.first.is_a?(Array)
  values.each{ |v| self.insert(nil, v) }
end

#saveObject



42
43
44
45
46
47
48
# File 'lib/redisabel/key_array.rb', line 42

def save
  return super do |key|
    Database.db.del(key)
    results = @data.map{ |v| Database.db.rpush(key, v.to_s) }
    next results.all?
  end
end

#to_aObject



54
55
56
# File 'lib/redisabel/key_array.rb', line 54

def to_a
  return @data.dup
end

#to_aryObject



50
51
52
# File 'lib/redisabel/key_array.rb', line 50

def to_ary
  return @data
end