class Redis::Collection
Attributes
counter_hash[R]
list[R]
Public Class Methods
new(key, *args)
click to toggle source
# File lib/redis/collection.rb, line 12 def initialize(key, *args) @list = Redis::List.new(key, *args) counter_hash_key = options[:counter_key] || [key, 'counter'].join(":") @counter_hash = Redis::HashKey.new(counter_hash_key, *args) unless options[:foreign_key].blank? foreign_key = options[:foreign_key].to_s class_eval do define_method foreign_key.pluralize do ids end define_method :"#{foreign_key.pluralize}=" do |new_ids| self.ids = new_ids end end end end
Public Instance Methods
<<(object)
click to toggle source
# File lib/redis/collection.rb, line 33 def <<(object) value = value(object) redis.lpush(key, to_redis(value)) self end
all()
click to toggle source
# File lib/redis/collection.rb, line 71 def all value_keys.map do |value_key| ordered_object_map[value_key] end end
counter_key()
click to toggle source
# File lib/redis/collection.rb, line 47 def counter_key counter_hash.key end
counters()
click to toggle source
# File lib/redis/collection.rb, line 179 def counters counter_hash.all end
delete(object)
click to toggle source
# File lib/redis/collection.rb, line 165 def delete(object) value = value(object) redis.lrem(key, 0, to_redis(value)) end
delete_all()
click to toggle source
# File lib/redis/collection.rb, line 170 def delete_all redis.del(key) end
delete_objects_not_found(object_map)
click to toggle source
# File lib/redis/collection.rb, line 108 def delete_objects_not_found(object_map) not_found = object_map.select do |object_value_key, object| object.nil? end.keys not_found.each do |value_not_found| redis.lrem(key, 0, to_redis(value_not_found)) end end
each(&block)
click to toggle source
# File lib/redis/collection.rb, line 67 def each(&block) all.each(&block) end
foreign_key()
click to toggle source
# File lib/redis/collection.rb, line 59 def foreign_key options[:foreign_key] || :id end
increment_counter(object)
click to toggle source
# File lib/redis/collection.rb, line 174 def increment_counter(object) value = value(object) counter_hash.incrby(to_redis(value), 1) end
joins()
click to toggle source
# File lib/redis/collection.rb, line 118 def joins options[:joins] end
key()
click to toggle source
# File lib/redis/collection.rb, line 43 def key list.key end
model()
click to toggle source
# File lib/redis/collection.rb, line 122 def model model_class_name.constantize end
model_class_name()
click to toggle source
# File lib/redis/collection.rb, line 126 def model_class_name (options[:class_name] || key.split(":").last).to_s.classify end
move(object, index)
click to toggle source
# File lib/redis/collection.rb, line 141 def move(object, index) # IMPORTANT: get value at index before removing from list value = value(object) before = redis.lindex(key, index) moved = if before.nil? || index == before false else redis.lrem(key, 0, value) # remove key if it already exists after = redis.lindex(key, index) where = if before == after # moving up :before else # moving down :after end redis.linsert(key, where, before, value) true end moved end
options()
click to toggle source
# File lib/redis/collection.rb, line 63 def options list.options end
ordered_object_map()
click to toggle source
# File lib/redis/collection.rb, line 81 def ordered_object_map unordered_map = unordered_object_map object_map = value_keys.inject({}) do |map, value_key| object = unordered_map[value_key] map.merge(value_key => object) end delete_objects_not_found(object_map) object_map end
redis()
click to toggle source
# File lib/redis/collection.rb, line 39 def redis list.redis end
unordered_object_map()
click to toggle source
# File lib/redis/collection.rb, line 91 def unordered_object_map if defined?(::Mongoid) && model.included_modules.include?(::Mongoid::Document) results = model.where(foreign_key.in => values) if joins results = results.includes(joins) end results else model .includes(joins) .joins(joins) .where("#{foreign_key} in (?)", values) end.inject({}) do |object_hash, object| object_hash.merge(value_key(object) => object) end end
value(object)
click to toggle source
# File lib/redis/collection.rb, line 51 def value(object) object.send(foreign_key) end
value_key(object)
click to toggle source
# File lib/redis/collection.rb, line 55 def value_key(object) value(object).to_s.to_sym end
value_keys()
click to toggle source
# File lib/redis/collection.rb, line 77 def value_keys values.map(&:to_sym) end
values()
click to toggle source
# File lib/redis/collection.rb, line 130 def values list.values end
Also aliased as: ids
values=(values)
click to toggle source
# File lib/redis/collection.rb, line 135 def values=(values) delete_all redis.rpush(key, to_redis(values)) end
Also aliased as: ids=