Module: Seabright::Collections

Included in:
RedisObject
Defined in:
lib/redis_object/collection.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
	base.extend(ClassMethods)
	base.intercept_sets_for_collecting!
end

Instance Method Details

#<<(obj) ⇒ Object



29
30
31
# File 'lib/redis_object/collection.rb', line 29

def <<(obj)
	reference obj
end

#backreferences(cls = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/redis_object/collection.rb', line 45

def backreferences(cls = nil)
	out = store.smembers(ref_key).map do |backreference_hkey|
		obj = RedisObject.find_by_key(backreference_hkey)
		if cls && !obj.is_a?(cls)
			nil
		else
			obj
		end
	end
	out.compact
end

#collect_type_by_key(col, *keys) ⇒ Object



110
111
112
113
114
115
# File 'lib/redis_object/collection.rb', line 110

def collect_type_by_key(col,*keys)
	collect = get_collection(col)
	keys.each do |k|
		collect << k
	end
end

#collection_nameObject



15
16
17
# File 'lib/redis_object/collection.rb', line 15

def collection_name
	self.class.collection_name
end

#collection_namesObject



106
107
108
# File 'lib/redis_object/collection.rb', line 106

def collection_names
	@collection_names ||= store.smembers(hkey_col)
end

#collectionsObject



102
103
104
# File 'lib/redis_object/collection.rb', line 102

def collections
	@collections ||= {}
end

#delete_child(obj) ⇒ Object



9
10
11
12
13
# File 'lib/redis_object/collection.rb', line 9

def delete_child(obj)
	if col = get_collection(obj.collection_name)
		col.delete obj
	end
end

#dereference_from(obj) ⇒ Object



57
58
59
# File 'lib/redis_object/collection.rb', line 57

def dereference_from(obj)
	obj.get_collection(collection_name).delete(hkey)
end

#dereference_from_backreferencesObject



61
62
63
64
65
# File 'lib/redis_object/collection.rb', line 61

def dereference_from_backreferences
	backreferences.each do |backreference|
		dereference_from(backreference)
	end
end

#get_collection(name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/redis_object/collection.rb', line 85

def get_collection(name)
	if has_collection?(name)
		collections[name.to_s] ||= Collection.load(name,self)
	else
		store.sadd hkey_col, name
		collection_names << name.to_s
		collections[name.to_s] ||= Collection.load(name,self)
		define_access(name.to_s.pluralize) do
			get_collection(name)
		end
		define_access(name.to_s.singularize) do
			get_collection(name).first
		end
	end
	collections[name.to_s]
end

#has_collection?(name) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/redis_object/collection.rb', line 81

def has_collection?(name)
	collection_names.include?(name.to_s)
end

#hkey_col(ident = nil) ⇒ Object



5
6
7
# File 'lib/redis_object/collection.rb', line 5

def hkey_col(ident = nil)
	"#{hkey}:collections"
end

#load(o_id) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/redis_object/collection.rb', line 67

def load(o_id)
	super(o_id)
	store.smembers(hkey_col).each do |name|
		collections[name] = Seabright::Collection.load(name,self)
		define_access(name) do
			get_collection(name)
		end
		define_access(name.to_s.singularize) do
			get_collection(name).latest
		end
	end
	true
end

#push(obj) ⇒ Object



33
34
35
# File 'lib/redis_object/collection.rb', line 33

def push(obj)
	reference obj
end

#ref_key(ident = nil) ⇒ Object



19
20
21
# File 'lib/redis_object/collection.rb', line 19

def ref_key(ident = nil)
	"#{hkey}:backreferences"
end

#reference(obj) ⇒ Object



23
24
25
26
27
# File 'lib/redis_object/collection.rb', line 23

def reference(obj)
	raise "Not an object." unless obj.is_a?(RedisObject)
	get_collection(obj.collection_name) << obj.hkey
	obj.referenced_by self
end

#referenced_by(obj) ⇒ Object



41
42
43
# File 'lib/redis_object/collection.rb', line 41

def referenced_by(obj)
	store.sadd(ref_key,obj.hkey)
end

#remove_collection!(name) ⇒ Object



37
38
39
# File 'lib/redis_object/collection.rb', line 37

def remove_collection!(name)
	store.srem hkey_col, name
end