Class: Redis::Collection
- Inherits:
-
Object
- Object
- Redis::Collection
- Includes:
- Enumerable, Helpers::Serialize
- Defined in:
- lib/redis/collection.rb
Instance Attribute Summary collapse
-
#counter_hash ⇒ Object
readonly
Returns the value of attribute counter_hash.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #all ⇒ Object
- #counter_key ⇒ Object
- #counters ⇒ Object
- #delete(object) ⇒ Object
- #delete_all ⇒ Object
- #delete_objects_not_found(object_map) ⇒ Object
- #each(&block) ⇒ Object
- #foreign_key ⇒ Object
- #increment_counter(object) ⇒ Object
-
#initialize(key, *args) ⇒ Collection
constructor
A new instance of Collection.
- #joins ⇒ Object
- #key ⇒ Object
- #model ⇒ Object
- #model_class_name ⇒ Object
- #move(object, index) ⇒ Object
- #options ⇒ Object
- #ordered_object_map ⇒ Object
- #redis ⇒ Object
- #unordered_object_map ⇒ Object
- #value(object) ⇒ Object
- #value_key(object) ⇒ Object
- #value_keys ⇒ Object
- #values ⇒ Object (also: #ids)
- #values=(values) ⇒ Object (also: #ids=)
Constructor Details
#initialize(key, *args) ⇒ Collection
Returns a new instance of Collection.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/redis/collection.rb', line 12 def initialize(key, *args) @list = Redis::List.new(key, *args) counter_hash_key = [:counter_key] || [key, 'counter'].join(":") @counter_hash = Redis::HashKey.new(counter_hash_key, *args) unless [:foreign_key].blank? foreign_key = [: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 |
Instance Attribute Details
#counter_hash ⇒ Object (readonly)
Returns the value of attribute counter_hash.
10 11 12 |
# File 'lib/redis/collection.rb', line 10 def counter_hash @counter_hash end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
10 11 12 |
# File 'lib/redis/collection.rb', line 10 def list @list end |
Instance Method Details
#<<(object) ⇒ Object
33 34 35 36 37 |
# File 'lib/redis/collection.rb', line 33 def <<(object) value = value(object) redis.lpush(key, to_redis(value)) self end |
#all ⇒ Object
71 72 73 74 75 |
# File 'lib/redis/collection.rb', line 71 def all value_keys.map do |value_key| ordered_object_map[value_key] end end |
#counter_key ⇒ Object
47 48 49 |
# File 'lib/redis/collection.rb', line 47 def counter_key counter_hash.key end |
#counters ⇒ Object
179 180 181 |
# File 'lib/redis/collection.rb', line 179 def counters counter_hash.all end |
#delete(object) ⇒ Object
165 166 167 168 |
# File 'lib/redis/collection.rb', line 165 def delete(object) value = value(object) redis.lrem(key, 0, to_redis(value)) end |
#delete_all ⇒ Object
170 171 172 |
# File 'lib/redis/collection.rb', line 170 def delete_all redis.del(key) end |
#delete_objects_not_found(object_map) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# 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) ⇒ Object
67 68 69 |
# File 'lib/redis/collection.rb', line 67 def each(&block) all.each(&block) end |
#foreign_key ⇒ Object
59 60 61 |
# File 'lib/redis/collection.rb', line 59 def foreign_key [:foreign_key] || :id end |
#increment_counter(object) ⇒ Object
174 175 176 177 |
# File 'lib/redis/collection.rb', line 174 def increment_counter(object) value = value(object) counter_hash.incrby(to_redis(value), 1) end |
#joins ⇒ Object
118 119 120 |
# File 'lib/redis/collection.rb', line 118 def joins [:joins] end |
#key ⇒ Object
43 44 45 |
# File 'lib/redis/collection.rb', line 43 def key list.key end |
#model ⇒ Object
122 123 124 |
# File 'lib/redis/collection.rb', line 122 def model model_class_name.constantize end |
#model_class_name ⇒ Object
126 127 128 |
# File 'lib/redis/collection.rb', line 126 def model_class_name ([:class_name] || key.split(":").last).to_s.classify end |
#move(object, index) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# 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 ⇒ Object
63 64 65 |
# File 'lib/redis/collection.rb', line 63 def list. end |
#ordered_object_map ⇒ Object
81 82 83 84 85 86 87 88 89 |
# 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 ⇒ Object
39 40 41 |
# File 'lib/redis/collection.rb', line 39 def redis list.redis end |
#unordered_object_map ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# 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) ⇒ Object
51 52 53 |
# File 'lib/redis/collection.rb', line 51 def value(object) object.send(foreign_key) end |
#value_key(object) ⇒ Object
55 56 57 |
# File 'lib/redis/collection.rb', line 55 def value_key(object) value(object).to_s.to_sym end |
#value_keys ⇒ Object
77 78 79 |
# File 'lib/redis/collection.rb', line 77 def value_keys values.map(&:to_sym) end |
#values ⇒ Object Also known as: ids
130 131 132 |
# File 'lib/redis/collection.rb', line 130 def values list.values end |
#values=(values) ⇒ Object Also known as: ids=
135 136 137 138 |
# File 'lib/redis/collection.rb', line 135 def values=(values) delete_all redis.rpush(key, to_redis(values)) end |