Module: Seabright::Collections::ClassMethods

Defined in:
lib/redis_object/collection.rb

Instance Method Summary collapse

Instance Method Details

#<<(obj) ⇒ Object



152
153
154
# File 'lib/redis_object/collection.rb', line 152

def <<(obj)
	reference obj
end

#collection_name ⇒ Object



142
143
144
# File 'lib/redis_object/collection.rb', line 142

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

#collections ⇒ Object



183
184
185
# File 'lib/redis_object/collection.rb', line 183

def collections
	@collections ||= {}
end

#delete_child(obj) ⇒ Object



136
137
138
139
140
# File 'lib/redis_object/collection.rb', line 136

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

#get(k) ⇒ Object



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

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

#get_collection(name) ⇒ Object



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

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

#has_collection?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hkey_col(ident = nil) ⇒ Object



132
133
134
# File 'lib/redis_object/collection.rb', line 132

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

#push(obj) ⇒ Object



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

def push(obj)
	reference obj
end

#reference(obj) ⇒ Object



146
147
148
149
150
# File 'lib/redis_object/collection.rb', line 146

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

#remove_collection!(name) ⇒ Object



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

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