Module: Swagger::Commands::Lists

Included in:
Redis
Defined in:
lib/swagger/commands/lists.rb

Constant Summary collapse

KEY_TYPE =
'list'

Instance Method Summary collapse

Instance Method Details

#llen(list_name) ⇒ Object



6
7
8
# File 'lib/swagger/commands/lists.rb', line 6

def llen(list_name)
  ResqueValue.all(:conditions => {:key => list_name.to_s, :key_type=> KEY_TYPE }).size
end

#lpop(list_name) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/swagger/commands/lists.rb', line 31

def lpop(list_name)
  ResqueValue.transaction do
    last = ResqueValue.last(:conditions => {:key => list_name.to_s, :key_type => KEY_TYPE}, :lock => true)
    if last
      last.destroy
      return last.value
    end
  end
end

#lrange(list_name, start_range, end_range) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/swagger/commands/lists.rb', line 14

def lrange(list_name, start_range, end_range)
  options = { :conditions => {
                :key => list_name.to_s,
                :key_type=> KEY_TYPE}}
  unless end_range < 0
    limit = end_range - start_range + 1
    options.merge!(:limit => limit, :offset => start_range)
  end
  values = ResqueValue.all(options)
  values.map(&:value)
end

#lrem(list_name, count, value) ⇒ Object



26
27
28
29
# File 'lib/swagger/commands/lists.rb', line 26

def lrem(list_name, count, value)
  raise "Only supports count of 0 which means to remove all elements in list" if count != 0
  ResqueValue.delete_all(:key => list_name.to_s, :key_type=> KEY_TYPE, :value => value )
end

#lset(list_name, index, value) ⇒ Object



10
11
12
# File 'lib/swagger/commands/lists.rb', line 10

def lset(list_name, index, value)
  rpush(list_name, value)
end

#ltrim(list_name, start_range, end_range) ⇒ Object



45
46
47
48
49
# File 'lib/swagger/commands/lists.rb', line 45

def ltrim(list_name, start_range, end_range)
  limit = end_range - start_range + 1
  ids = ResqueValue.all(:select => "id", :conditions => {:key => list_name}, :offset => start_range, :limit => limit)
  ResqueValue.delete_all(["`key` = ? AND id NOT IN (?)", list_name, ids.collect{|i| i.id}])
end

#rpush(list_name, value) ⇒ Object



41
42
43
# File 'lib/swagger/commands/lists.rb', line 41

def rpush(list_name, value)
  ResqueValue.create!(:key => list_name.to_s, :key_type => KEY_TYPE, :value => value.to_s)
end