Module: Seabright::Collections::ClassMethods

Defined in:
lib/redis_object/collection.rb

Instance Method Summary collapse

Instance Method Details

#<<(obj) ⇒ Object



170
171
172
# File 'lib/redis_object/collection.rb', line 170

def <<(obj)
	reference obj
end

#collection_nameObject



160
161
162
# File 'lib/redis_object/collection.rb', line 160

def collection_name
	self.name.split('::').last.pluralize.underscore.to_sym
end

#collectionsObject



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

def collections
	@collections ||= {}
end

#delete_child(obj) ⇒ Object



154
155
156
157
158
# File 'lib/redis_object/collection.rb', line 154

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

#get(k) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/redis_object/collection.rb', line 182

def get(k)
	if has_collection?(k)
		return get_collection(k)
	elsif has_collection?(pk = k.to_s.pluralize)
		return get_collection(pk).first
	end
	nil
end

#get_collection(name) ⇒ Object



195
196
197
198
# File 'lib/redis_object/collection.rb', line 195

def get_collection(name)
	collections[name.to_s] ||= Collection.load(name,self)
	collections[name.to_s]
end

#has_collection?(name) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/redis_object/collection.rb', line 191

def has_collection?(name)
	store.sismember(hkey_col,name.to_s)
end

#hkey_col(ident = nil) ⇒ Object



150
151
152
# File 'lib/redis_object/collection.rb', line 150

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

#intercept_sets_for_collecting!Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/redis_object/collection.rb', line 119

def intercept_sets_for_collecting!
	return if @intercepted_sets_for_collecting
	self.class_eval do
		
		filter_gets do |obj, k, v|
			if obj.has_collection?(k)
				obj.get_collection(k)
			elsif obj.has_collection?(pk = k.to_s.pluralize)
				obj.get_collection(pk).first
			else
				v
			end
		end
		
		filter_sets do |obj, k, v|
			if obj.has_collection?(k)
				obj.get_collection(k.to_s).replace(v)
				return [nil,nil]
			else
				[k,v]
			end
		end
		
		filter_msets do |obj, dat|
			dat.select {|k,v| !obj.collections[k.to_s] }
		end
		
	end
	@intercepted_sets_for_collecting = true
end

#push(obj) ⇒ Object



174
175
176
# File 'lib/redis_object/collection.rb', line 174

def push(obj)
	reference obj
end

#reference(obj) ⇒ Object



164
165
166
167
168
# File 'lib/redis_object/collection.rb', line 164

def reference(obj)
	name = obj.collection_name
	store.sadd hkey_col, name
	get_collection(name) << obj.hkey
end

#remove_collection!(name) ⇒ Object



178
179
180
# File 'lib/redis_object/collection.rb', line 178

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