Class: Redistry::List::CollectionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/redistry/list/collection_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, collection_name, options = {}) ⇒ CollectionProxy

Returns a new instance of CollectionProxy.



6
7
8
9
10
11
# File 'lib/redistry/list/collection_proxy.rb', line 6

def initialize(klass, collection_name, options = {})
  self.klass = klass
  self.collection_name = collection_name
  self.options = default_options.merge(options)
  setup_serializer!
end

Instance Attribute Details

#collection_nameObject

Returns the value of attribute collection_name.



4
5
6
# File 'lib/redistry/list/collection_proxy.rb', line 4

def collection_name
  @collection_name
end

#klassObject

Returns the value of attribute klass.



4
5
6
# File 'lib/redistry/list/collection_proxy.rb', line 4

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/redistry/list/collection_proxy.rb', line 4

def options
  @options
end

Instance Method Details

#add(*objs) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redistry/list/collection_proxy.rb', line 18

def add(*objs)
  serialized_objs = @serializer.serialize(klass, objs.flatten)
  serialized_objs.reverse.each do |val|
    Redistry.client.lpush(key, val)
  end

  if options[:size]
    Redistry.client.ltrim(key,0,options[:size]-1)
  end
  self
end

#allObject



13
14
15
16
# File 'lib/redistry/list/collection_proxy.rb', line 13

def all
  serialized_objs = Redistry.client.lrange(key,0,-1)
  serialized_objs.nil? || serialized_objs.empty? ? [] : @serializer.deserialize(klass, serialized_objs)
end

#clearObject



30
31
32
33
# File 'lib/redistry/list/collection_proxy.rb', line 30

def clear
  Redistry.client.del(key)
  self
end

#keyObject



35
36
37
# File 'lib/redistry/list/collection_proxy.rb', line 35

def key
  @key ||= "#{klass.name}-#{collection_name.to_s}"
end