redis_assist 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5ca743b1a93bc0582651d353aa37b5b9fb4b1ce
4
- data.tar.gz: c0a20e395c0c9ade86935a5299684ef64f0e47da
3
+ metadata.gz: 7df8d359a12b776a26ea86bf2b593825b5146994
4
+ data.tar.gz: 010c01a56bb842ca476a528b6b4ea254fa41e7ad
5
5
  SHA512:
6
- metadata.gz: 59dcf841c9d547297ecc461de66e63f2a0e8d465abd9b6cc78593e7d3fef36d44aeb4f71deeb7e73b142ec2d4a43e9bba7c1e80d1cf1a6627080e4827b7dae92
7
- data.tar.gz: fe0f0fc265f5ff06219c16b224e752709424b19efa7267193eb0c6bde005b69c6f3e14fbdd0e19c48be6182472acdaa4f37b8696308ec5d057c2877480e7d275
6
+ metadata.gz: 982ea611873442583290d984117e8015c7668b9e884d0f36a2ef915af9af4bedb6bf2abc0b90c52ce039d75e5e021286c388d8866f12c57ff6d8032db3ab2e12
7
+ data.tar.gz: d55d9df08d606043c4df664b42bfe4ff582cbbeea18e77746236859c1da57bc4dd42b5151cf24493a8616e15d4eafa0e19fc75c695936c08f55b8d5543b18d72
@@ -33,7 +33,7 @@ module RedisAssist
33
33
  # Deprecated finds
34
34
  def find_by_id(id, opts={})
35
35
  raw_attributes = load_attributes(id)
36
- return nil unless raw_attributes[id]
36
+ return nil unless raw_attributes[id][:exists].value
37
37
  obj = new(id: id, raw_attributes: raw_attributes[id])
38
38
  (obj.deleted? && !opts[:deleted].eql?(true)) ? nil : obj
39
39
  end
@@ -42,8 +42,10 @@ module RedisAssist
42
42
  attrs = load_attributes(*ids)
43
43
  raw_attributes = attrs
44
44
  ids.each_with_object([]) do |id, instances|
45
- instance = new(id: id, raw_attributes: raw_attributes[id]) unless raw_attributes[id].nil?
46
- instances << instance if instance && (!instance.deleted? || opts[:deleted].eql?(true))
45
+ if raw_attributes[id][:exists].value
46
+ instance = new(id: id, raw_attributes: raw_attributes[id])
47
+ instances << instance if instance && (!instance.deleted? || opts[:deleted].eql?(true))
48
+ end
47
49
  end
48
50
  end
49
51
 
@@ -152,24 +154,16 @@ module RedisAssist
152
154
 
153
155
  future_fields = pipe.hmget(key_for(id, :attributes), fields.keys)
154
156
 
155
- futures[id] = { lists: future_lists, hashes: future_hashes, fields: future_fields }
157
+ futures[id] = {
158
+ lists: future_lists,
159
+ hashes: future_hashes,
160
+ fields: future_fields,
161
+ exists: pipe.exists(key_for(id, :attributes))
162
+ }
156
163
  end
157
164
  end
158
165
 
159
- # Remove the empty futures
160
- future_attrs = ids.each_with_object({}) do |id, obj|
161
- obj[id] = future_attrs[id] unless future_attrs[id][:fields].value.select{|v| !v.nil? }.empty?
162
- end
163
-
164
- # Map futures to attributes
165
- future_attrs.each_with_object(attrs) do |kv, obj|
166
- lists = kv[1][:lists].each_with_object({}){|kv,obj| obj[kv[0]] = kv[1].value unless kv[1].value.nil? }
167
- hashes = kv[1][:hashes].each_with_object({}){|kv,obj| obj[kv[0]] = kv[1].value unless kv[1].value.nil?}
168
- fields = Hash[*self.fields.keys.zip(kv[1][:fields].value).flatten]
169
- obj[kv[0]] = { lists: lists, hashes: hashes, fields: fields }
170
- end
171
-
172
- attrs
166
+ future_attrs
173
167
  end
174
168
 
175
169
  def hash_to_redis(obj)
@@ -181,8 +175,13 @@ module RedisAssist
181
175
  def define_list(name)
182
176
  define_method(name) do
183
177
  opts = self.class.persisted_attrs[name]
184
- self.send("#{name}=", opts[:default]) if !lists[name] && opts[:default]
185
- lists[name]
178
+
179
+ if !lists[name] && opts[:default]
180
+ opts[:default]
181
+ else
182
+ send("#{name}=", lists[name].value) if lists[name].is_a?(Redis::Future)
183
+ lists[name]
184
+ end
186
185
  end
187
186
 
188
187
  define_method("#{name}=") do |val|
@@ -194,8 +193,12 @@ module RedisAssist
194
193
  def define_hash(name)
195
194
  define_method(name) do
196
195
  opts = self.class.persisted_attrs[name]
197
- self.send("#{name}=", opts[:default]) if !hashes[name] && opts[:default]
198
- hashes[name]
196
+
197
+ if !hashes[name] && opts[:default]
198
+ opts[:default]
199
+ else
200
+ self.send("#{name}=", hashes[name].value)
201
+ end
199
202
  end
200
203
 
201
204
  define_method("#{name}=") do |val|
@@ -206,6 +209,11 @@ module RedisAssist
206
209
 
207
210
  def define_attribute(name)
208
211
  define_method(name) do
212
+ if attributes.is_a?(Redis::Future)
213
+ value = attributes.value
214
+ self.attributes = value ? Hash[*self.class.fields.keys.zip(value).flatten] : {}
215
+ end
216
+
209
217
  self.class.transform(:from, name, attributes[name])
210
218
  end
211
219
 
@@ -223,12 +231,14 @@ module RedisAssist
223
231
  self.lists = {}
224
232
  self.hashes = {}
225
233
 
234
+ if attrs[:id]
235
+ end
236
+
226
237
  if attrs[:id]
227
238
  self.id = attrs[:id]
228
239
  load_attributes(attrs[:raw_attributes])
240
+ return self if self.id
229
241
  end
230
-
231
- return self if id
232
242
 
233
243
  self.new_record = true
234
244
 
@@ -259,18 +269,24 @@ module RedisAssist
259
269
  redis.multi do
260
270
  # build the arguments to pass to redis hmset
261
271
  # and insure the attributes are explicitely declared
262
- attribute_args = hash_to_redis(attributes)
263
-
264
- redis.hmset(key_for(:attributes), *attribute_args)
272
+
273
+ unless attributes.is_a?(Redis::Future)
274
+ attribute_args = hash_to_redis(attributes)
275
+ redis.hmset(key_for(:attributes), *attribute_args)
276
+ end
265
277
 
266
278
  lists.each do |name, val|
267
- redis.del(key_for(name))
268
- redis.rpush(key_for(name), val) if val && !val.empty?
279
+ if val && !val.is_a?(Redis::Future)
280
+ redis.del(key_for(name))
281
+ redis.rpush(key_for(name), val) unless val.empty?
282
+ end
269
283
  end
270
284
 
271
285
  hashes.each do |name, val|
272
- hash_as_args = hash_to_redis(val)
273
- redis.hmset(key_for(name), *hash_as_args)
286
+ unless val.is_a?(Redis::Future)
287
+ hash_as_args = hash_to_redis(val)
288
+ redis.hmset(key_for(name), *hash_as_args)
289
+ end
274
290
  end
275
291
  end
276
292
 
@@ -344,10 +360,9 @@ module RedisAssist
344
360
 
345
361
  def load_attributes(raw_attributes)
346
362
  return nil unless raw_attributes
347
- load_response = self.class.load_attributes(id)
348
- self.lists = load_response[id][:lists]
349
- self.hashes = load_response[id][:hashes]
350
- self.attributes = load_response[id][:fields]
363
+ self.lists = raw_attributes[:lists]
364
+ self.hashes = raw_attributes[:hashes]
365
+ self.attributes = raw_attributes[:fields]
351
366
  self.new_record = false
352
367
  end
353
368
 
@@ -1,3 +1,3 @@
1
1
  module RedisAssist
2
- VERSION = '0.4.1' unless defined?(::RedisAssist::VERSION)
2
+ VERSION = '0.4.2' unless defined?(::RedisAssist::VERSION)
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_assist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Love
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-24 00:00:00.000000000 Z
11
+ date: 2013-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis