lib/redis-model-extension.rb in redis-model-extension-0.3.3 vs lib/redis-model-extension.rb in redis-model-extension-0.3.4
- old
+ new
@@ -92,16 +92,16 @@
full_key
end
#Check if key by arguments exists in db
def exists?(args = {})
- Database.redis.exists(self.name.constantize.generate_key(args))
+ RedisModelExtension::Database.redis.exists(self.name.constantize.generate_key(args))
end
#Check if key by alias name and arguments exists in db
def alias_exists?(alias_name, args = {})
- Database.redis.exists(self.name.constantize.generate_alias_key(alias_name, args))
+ RedisModelExtension::Database.redis.exists(self.name.constantize.generate_alias_key(alias_name, args))
end
#Wrapper around find to get all instances
def all
self.find({})
@@ -114,16 +114,16 @@
klass = self.name.constantize
#is key specified directly? -> no needs of looking for other keys! -> faster
if klass.valid_key?(args)
if klass.exists?(args)
- data_args = Database.redis.hgetall(klass.generate_key(args))
+ data_args = RedisModelExtension::Database.redis.hgetall(klass.generate_key(args))
out << klass.new(args.merge(data_args).merge({:old_args => data_args}))
end
else
- Database.redis.keys(klass.generate_key(args)).each do |key|
- data_args = Database.redis.hgetall(key)
+ RedisModelExtension::Database.redis.keys(klass.generate_key(args)).each do |key|
+ data_args = RedisModelExtension::Database.redis.hgetall(key)
out << klass.new(args.merge(data_args).merge({:old_args => data_args}))
end
end
out
end
@@ -136,11 +136,11 @@
#is key specified directly? -> no needs of looking for other keys! -> faster
if klass.valid_alias_key?(alias_name, args)
out << klass.get_by_alias(alias_name, args) if klass.alias_exists?(alias_name, args)
else
- Database.redis.keys(klass.generate_alias_key(alias_name, args)).each do |key|
+ RedisModelExtension::Database.redis.keys(klass.generate_alias_key(alias_name, args)).each do |key|
out << klass.get_by_alias_key(key)
end
end
out
end
@@ -148,22 +148,22 @@
#fastest method to get object from redis by getting it by arguments
def get(args = {})
args.symbolize_keys!
klass = self.name.constantize
if klass.valid_key?(args) && klass.exists?(args)
- data_args = Database.redis.hgetall(klass.generate_key(args))
+ data_args = RedisModelExtension::Database.redis.hgetall(klass.generate_key(args))
klass.new(args.merge(data_args).merge({:old_args => data_args}))
else
nil
end
end
#if you know redis key and would like to get object
def get_by_redis_key(redis_key)
- if redis_key.is_a?(String) && Database.redis.exists(redis_key)
+ if redis_key.is_a?(String) && RedisModelExtension::Database.redis.exists(redis_key)
unless redis_key.include?("*")
- data_args = Database.redis.hgetall(redis_key)
+ data_args = RedisModelExtension::Database.redis.hgetall(redis_key)
klass = self.name.constantize
klass.new(data_args.merge({:old_args => data_args}))
else
raise ArgumentError, "RedisKey for method get_by_redis_key can not contains '*'"
end
@@ -175,13 +175,13 @@
#fastest method to get object from redis by getting it by alias and arguments
def get_by_alias(alias_name, args = {})
args.symbolize_keys!
klass = self.name.constantize
if klass.valid_alias_key?(alias_name, args) && klass.alias_exists?(alias_name, args)
- key = Database.redis.get(klass.generate_alias_key(alias_name, args))
- if Database.redis.exists(key)
- data_args = Database.redis.hgetall(key)
+ key = RedisModelExtension::Database.redis.get(klass.generate_alias_key(alias_name, args))
+ if RedisModelExtension::Database.redis.exists(key)
+ data_args = RedisModelExtension::Database.redis.hgetall(key)
klass.new(args.merge(data_args).merge({:old_args => data_args}))
else
nil
end
else
@@ -190,14 +190,14 @@
end
#fastest method to get object from redis by getting it by alias and arguments
def get_by_alias_key(alias_key)
klass = self.name.constantize
- if Database.redis.exists(alias_key)
- key = Database.redis.get(alias_key)
- if Database.redis.exists(key)
- klass.new(args.merge(Database.redis.hgetall(key)).merge({:old_args => key}))
+ if RedisModelExtension::Database.redis.exists(alias_key)
+ key = RedisModelExtension::Database.redis.get(alias_key)
+ if RedisModelExtension::Database.redis.exists(key)
+ klass.new(args.merge(RedisModelExtension::Database.redis.hgetall(key)).merge({:old_args => key}))
else
nil
end
else
nil
@@ -258,39 +258,39 @@
end
end
#if this record exists in database
def exists?
- Database.redis.exists(self.class.generate_key(self.args))
+ RedisModelExtension::Database.redis.exists(self.class.generate_key(self.args))
end
#remove record form database
def destroy!
if exists?
#destroy main object
- Database.redis.del(redis_key)
+ RedisModelExtension::Database.redis.del(redis_key)
destroy_aliases!
end
end
#remove all aliases
def destroy_aliases!
#do it only if it is existing object!
if self.old_args
self.class.conf[:redis_aliases].each do |alias_name, fields|
if self.class.valid_alias_key?(alias_name, self.old_args) && self.class.alias_exists?(alias_name, self.old_args)
- Database.redis.del(self.class.generate_alias_key(alias_name, self.old_args))
+ RedisModelExtension::Database.redis.del(self.class.generate_alias_key(alias_name, self.old_args))
end
end
end
end
#Method for creating aliases
def create_aliases
main_key = redis_key
self.class.conf[:redis_aliases].each do |alias_name, fields|
- Database.redis.set(self.class.generate_alias_key(alias_name, self.args), main_key) if self.class.valid_alias_key?(alias_name, self.args)
+ RedisModelExtension::Database.redis.set(self.class.generate_alias_key(alias_name, self.args), main_key) if self.class.valid_alias_key?(alias_name, self.args)
end
end
#get redis key for instance
def redis_key
@@ -300,16 +300,26 @@
#get redis key for instance alias
def redis_alias_key(alias_name)
self.class.generate_alias_key(alias_name, self.args)
end
+ #update multiple attrubutes at once
+ def update args
+ args.each do |key, value|
+ method = "#{key}=".to_sym
+ if self.respond_to? method
+ self.send(method, value)
+ end
+ end
+ end
+
#save method
def save
if valid?
#generate key (possibly new)
generated_key = redis_key
- Database.redis.rename(self.class.generate_key(self.old_args), generated_key) if self.old_args && generated_key != self.class.generate_key(self.old_args) && Database.redis.exists(self.class.generate_key(self.old_args))
- Database.redis.hmset(generated_key, *self.args.reject{|k,v| v.nil?}.inject([]){ |arr,kv| arr + [kv[0], kv[1].to_s]})
+ RedisModelExtension::Database.redis.rename(self.class.generate_key(self.old_args), generated_key) if self.old_args && generated_key != self.class.generate_key(self.old_args) && RedisModelExtension::Database.redis.exists(self.class.generate_key(self.old_args))
+ RedisModelExtension::Database.redis.hmset(generated_key, *self.args.reject{|k,v| v.nil?}.inject([]){ |arr,kv| arr + [kv[0], kv[1].to_s]})
#destroy aliases
destroy_aliases!
create_aliases
#after save make new_key -> old_key
\ No newline at end of file