Class: Seabright::Collection

Inherits:
Array
  • Object
show all
Includes:
CachedScripts
Defined in:
lib/redis_object/collection.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CachedScripts

included, #run_script

Constructor Details

#initialize(name, owner) ⇒ Collection

Returns a new instance of Collection.



199
200
201
202
# File 'lib/redis_object/collection.rb', line 199

def initialize(name,owner)
	@name = name.to_s
	@owner = owner
end

Class Method Details

.class_const_for(name) ⇒ Object



380
381
382
# File 'lib/redis_object/collection.rb', line 380

def class_const_for(name)
	Object.const_get(name.to_s.classify.to_sym) rescue RedisObject
end

.load(name, owner) ⇒ Object



374
375
376
377
378
# File 'lib/redis_object/collection.rb', line 374

def load(name,owner)
	out = new(name,owner)
	out.replace class_const_for(name).store.zrange(out.key,0,-1)
	out
end

Instance Method Details

#<<(obj) ⇒ Object



350
351
352
353
354
# File 'lib/redis_object/collection.rb', line 350

def <<(obj)
	k = obj.class == String ? obj : obj.hkey
	store.zadd(key,store.zcount(key,"-inf", "+inf"),k)
	super(k)
end

#[](k) ⇒ Object



265
266
267
# File 'lib/redis_object/collection.rb', line 265

def [](k)
	find k
end

#class_const ⇒ Object



360
361
362
# File 'lib/redis_object/collection.rb', line 360

def class_const
	self.class.class_const_for(@name)
end

#cleanup! ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/redis_object/collection.rb', line 312

def cleanup!
	each_index do |key|
		unless a = class_const.find_by_key(at(key))
			puts "Deleting #{key} because not #{a.inspect}" if DEBUG
			delete at(key)
		end
	end
	if size < 1
		puts "Deleting collection #{@name} because empty" if DEBUG
		remove!
	end
end

#clear! ⇒ Object



346
347
348
# File 'lib/redis_object/collection.rb', line 346

def clear!
	store.zrem(key,self.join(" "))
end

#delete(obj) ⇒ Object



340
341
342
343
344
# File 'lib/redis_object/collection.rb', line 340

def delete(obj)
	k = obj.class == String ? obj : obj.hkey
	store.zrem(key,k)
	super(k)
end

#each ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/redis_object/collection.rb', line 295

def each
	out = Enumerator.new do |y|
		each_index do |key|
			if a = class_const.find_by_key(at(key))
				y << a
			end
		end
	end
	if block_given?
		out.each do |a|
			yield a
		end
	else
		out
	end
end

#find(k) ⇒ Object



254
255
256
257
258
259
260
261
262
263
# File 'lib/redis_object/collection.rb', line 254

def find(k)
	if k.is_a? String
		return real_at(item_key(k))
	elsif k.is_a? Hash
		return match(k)
	elsif k.is_a? Integer
		return real_at(at(k))
	end
	return nil
end

#first ⇒ Object



287
288
289
# File 'lib/redis_object/collection.rb', line 287

def first
	class_const.find_by_key(super)
end

#index_key(idx) ⇒ Object



246
247
248
# File 'lib/redis_object/collection.rb', line 246

def index_key(idx)
	class_const.index_key(idx)
end

#indexed(idx, num = -1,, reverse = false) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/redis_object/collection.rb', line 212

def indexed(idx,num=-1,reverse=false)
	keys = keys_by_index(idx,num,reverse)
	out = Enumerator.new do |y|
		keys.each do |member|
			if a = class_const.find_by_key(member)
				y << a
			end
		end
	end
	if block_given?
		out.each do |itm|
			yield itm
		end
	else
		out
	end
end

#item_key(k) ⇒ Object



250
251
252
# File 'lib/redis_object/collection.rb', line 250

def item_key(k)
	"#{class_const}:#{k}_h"
end

#key ⇒ Object



368
369
370
# File 'lib/redis_object/collection.rb', line 368

def key
	"#{@owner ? "#{@owner.key}:" : ""}COLLECTION:#{@name}"
end

#keys_by_index(idx, num = -1,, reverse = false) ⇒ Object



237
238
239
240
241
242
243
244
# File 'lib/redis_object/collection.rb', line 237

def keys_by_index(idx,num=-1,reverse=false)
	keys = run_script(reverse ? :RevScript : :FwdScript, [temp_key, index_key(idx), key, num])
	Enumerator.new do |y|
		keys.each do |member|
			y << member
		end
	end
end

#last ⇒ Object



291
292
293
# File 'lib/redis_object/collection.rb', line 291

def last
	class_const.find_by_key(super)
end

#latest ⇒ Object



208
209
210
# File 'lib/redis_object/collection.rb', line 208

def latest
	indexed(:created_at,5,true).first || first
end

#map(&block) ⇒ Object



325
326
327
# File 'lib/redis_object/collection.rb', line 325

def map(&block)
	each.map(&block)
end

#match(pkt) ⇒ Object



269
270
271
272
273
274
275
276
277
# File 'lib/redis_object/collection.rb', line 269

def match(pkt)
	Enumerator.new do |y|
		each do |i|
			if pkt.map {|hk,va| i.get(hk)==va }.all?
				y << i
			end
		end
	end
end

#objects ⇒ Object



283
284
285
# File 'lib/redis_object/collection.rb', line 283

def objects
	each.to_a
end

#push(obj) ⇒ Object



356
357
358
# File 'lib/redis_object/collection.rb', line 356

def push(obj)
	self << obj
end

#real_at(key) ⇒ Object



279
280
281
# File 'lib/redis_object/collection.rb', line 279

def real_at(key)
	class_const.find_by_key(key)
end

#remove! ⇒ Object



204
205
206
# File 'lib/redis_object/collection.rb', line 204

def remove!
	@owner.remove_collection! @name
end

#select(&block) ⇒ Object



329
330
331
332
333
334
335
336
337
338
# File 'lib/redis_object/collection.rb', line 329

def select(&block)
	return nil unless block_given?
	Enumerator.new do |y|
		each_index do |key|
			if (a = class_const.find_by_key(at(key))) && block.call(a)
				y << a
			end
		end
	end
end

#store ⇒ Object



364
365
366
# File 'lib/redis_object/collection.rb', line 364

def store
	class_const.store
end

#temp_key ⇒ Object



230
231
232
# File 'lib/redis_object/collection.rb', line 230

def temp_key
	"#{key}::zintersect_temp::#{RedisObject.new_id(4)}"
end