Class: Seabright::Collection

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

Constant Summary collapse

NilPattern =
'nilpattern:'

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.



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

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

Class Method Details

.class_const_for(name) ⇒ Object



586
587
588
# File 'lib/redis_object/collection.rb', line 586

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

.load(name, owner) ⇒ Object



580
581
582
583
584
# File 'lib/redis_object/collection.rb', line 580

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



556
557
558
559
560
# File 'lib/redis_object/collection.rb', line 556

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

#[](k) ⇒ Object



272
273
274
# File 'lib/redis_object/collection.rb', line 272

def [](k)
	find k
end

#class_constObject



566
567
568
# File 'lib/redis_object/collection.rb', line 566

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

#cleanup!Object



518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/redis_object/collection.rb', line 518

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

#clear!Object



552
553
554
# File 'lib/redis_object/collection.rb', line 552

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

#convert_regex_to_lua(reg) ⇒ Object



471
472
473
# File 'lib/redis_object/collection.rb', line 471

def convert_regex_to_lua(reg)
	"#{reg.casefold? ? "i" : ""}pattern:#{reg.source.gsub("\\","")}"
end

#delete(obj) ⇒ Object



546
547
548
549
550
# File 'lib/redis_object/collection.rb', line 546

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

#eachObject



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/redis_object/collection.rb', line 501

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



261
262
263
264
265
266
267
268
269
270
# File 'lib/redis_object/collection.rb', line 261

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

#firstObject



493
494
495
# File 'lib/redis_object/collection.rb', line 493

def first
	class_const.find_by_key(super)
end

#index_key(idx) ⇒ Object



253
254
255
# File 'lib/redis_object/collection.rb', line 253

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

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/redis_object/collection.rb', line 219

def indexed(idx,num=-1,reverse=false)
	keys = keys_by_index(idx,num,reverse)
	out = ListEnumerator.new(keys) 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

#inject_key(key, list) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/redis_object/collection.rb', line 458

def inject_key(key,list)
	out = []
	list.each do |i|
		if i == list.first
			out << i
		else
			out << key
			out << i
		end
	end
	out
end

#item_key(k) ⇒ Object



257
258
259
# File 'lib/redis_object/collection.rb', line 257

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

#keyObject



574
575
576
# File 'lib/redis_object/collection.rb', line 574

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

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



244
245
246
247
248
249
250
251
# File 'lib/redis_object/collection.rb', line 244

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

#lastObject



497
498
499
# File 'lib/redis_object/collection.rb', line 497

def last
	class_const.find_by_key(super)
end

#latestObject



215
216
217
# File 'lib/redis_object/collection.rb', line 215

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

#map(&block) ⇒ Object



531
532
533
# File 'lib/redis_object/collection.rb', line 531

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

#match(pkt, use_or = false) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/redis_object/collection.rb', line 429

def match(pkt, use_or=false)
	if use_or
		mtchr = :ColOrMatcher
	else
		mtchr = pkt.keys.count > 1 ? :ColMultiMatcher : :ColMatcher
	end
	pkt = pkt.flatten.reduce([]) do |i,v|
		x = case v
		when Regexp
			convert_regex_to_lua(v)
		when Array
			raise ArgumentError.new("An array can only be used with the find_or method") unless use_or
			inject_key(i.last, v)
		when NilClass
			NilPattern
		else
			v.to_s
		end
		i << x
		i
	end
	kys = run_script(mtchr,self,pkt.flatten)
	ListEnumerator.new(kys) do |y|
		kys.each do |k|
			y << class_const.find_by_key(k)
		end
	end
end

#objectsObject



489
490
491
# File 'lib/redis_object/collection.rb', line 489

def objects
	each.to_a
end

#push(obj) ⇒ Object



562
563
564
# File 'lib/redis_object/collection.rb', line 562

def push(obj)
	self << obj
end

#real_at(key) ⇒ Object

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



485
486
487
# File 'lib/redis_object/collection.rb', line 485

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

#remove!Object



211
212
213
# File 'lib/redis_object/collection.rb', line 211

def remove!
	@owner.remove_collection! @name
end

#select(&block) ⇒ Object



535
536
537
538
539
540
541
542
543
544
# File 'lib/redis_object/collection.rb', line 535

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

#storeObject



570
571
572
# File 'lib/redis_object/collection.rb', line 570

def store
	class_const.store
end

#temp_keyObject



237
238
239
# File 'lib/redis_object/collection.rb', line 237

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