lib/redis-model-extension.rb in redis-model-extension-0.2.6 vs lib/redis-model-extension.rb in redis-model-extension-0.2.7
- old
+ new
@@ -11,11 +11,11 @@
require 'string_to_bool'
require 'database'
module RedisModel
- attr_accessor :args, :error, :old_key, :conf
+ attr_accessor :args, :error, :old_args, :conf
def self.included(base)
base.send :extend, ClassMethods
base.send :include, InstanceMethods
end
@@ -48,11 +48,11 @@
@conf
end
#Generates redis key for storing object
def generate_key(args = {})
- out = "#{self.name.to_s.underscore.to_sym}:key:"
+ out = "#{self.name.to_s.underscore.to_sym}:key"
@conf[:redis_key].each do |key|
if args.has_key?(key)
out += ":#{args[key]}"
else
out += ":*"
@@ -113,14 +113,18 @@
out = []
klass = self.name.constantize
#is key specified directly? -> no needs of looking for other keys! -> faster
if klass.valid_key?(args)
- out << klass.new(args.merge(Database.redis.hgetall(klass.generate_key(args))).merge({:old_key => klass.generate_key(args)})) if klass.exists?(args)
+ if klass.exists?(args)
+ data_args = 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|
- out << klass.new(args.merge(Database.redis.hgetall(key)).merge({:old_key => key}))
+ data_args = Database.redis.hgetall(key)
+ out << klass.new(args.merge(data_args).merge({:old_args => data_args}))
end
end
out
end
@@ -143,12 +147,13 @@
#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.new(args.merge(Database.redis.hgetall(klass.generate_key(args))).merge({:old_key => klass.generate_key(args)})) if klass.exists?(args)
+ if klass.valid_key?(args) && klass.exists?(args)
+ data_args = Database.redis.hgetall(klass.generate_key(args))
+ klass.new(args.merge(data_args).merge({:old_args => data_args}))
else
nil
end
end
@@ -157,11 +162,12 @@
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)
- klass.new(args.merge(Database.redis.hgetall(key)).merge({:old_key => key}))
+ data_args = Database.redis.hgetall(key)
+ klass.new(args.merge(data_args).merge({:old_args => data_args}))
else
nil
end
else
nil
@@ -172,11 +178,11 @@
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_key => key}))
+ klass.new(args.merge(Database.redis.hgetall(key)).merge({:old_args => key}))
else
nil
end
else
nil
@@ -187,14 +193,13 @@
module InstanceMethods
def initialize(args={})
args.symbolize_keys!
- #if old_key is specified, don't usi it in args hash
- if args[:old_key] && args[:old_key].size > 0
- self.old_key = args[:old_key]
- args.delete(:old_key)
+ #if old_args is specified, don't usi it in args hash
+ if args[:old_args] && args[:old_args].size > 0
+ self.old_args = args.delete(:old_args).symbolize_keys
end
self.args = clear_args(args)
return self
end
@@ -252,13 +257,16 @@
end
end
#remove all aliases
def destroy_aliases!
- self.class.conf[:redis_aliases].each do |alias_name, fields|
- if self.class.valid_alias_key?(alias_name, self.args) && self.class.alias_exists?(alias_name, self.args)
- Database.redis.del(self.class.generate_alias_key(alias_name, self.args))
+ #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))
+ end
end
end
end
#Method for creating aliases
@@ -282,17 +290,17 @@
#save method
def save
if valid?
#generate key (possibly new)
generated_key = redis_key
- Database.redis.rename(self.old_key, generated_key) if self.old_key && generated_key != self.old_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]})
#destroy aliases
destroy_aliases!
create_aliases
#after save make new_key -> old_key
- self.old_key = generated_key
+ self.old_args = self.args
return self
else
raise ArgumentError, @error.join(", ")
end
end
\ No newline at end of file