lib/kredis/attributes.rb in kredis-0.1.1 vs lib/kredis/attributes.rb in kredis-0.1.2

- old
+ new

@@ -1,50 +1,93 @@ module Kredis::Attributes extend ActiveSupport::Concern class_methods do - def kredis_list(name, config: :shared) + def kredis_proxy(name, key: nil, config: :shared) + ivar_symbol = :"@#{name}_kredis_proxy" + + define_method(name) do + if instance_variable_defined?(ivar_symbol) + instance_variable_get(ivar_symbol) + else + instance_variable_set(ivar_symbol, Kredis.proxy(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config)) + end + end + end + + def kredis_list(name, key: nil, config: :shared) ivar_symbol = :"@#{name}_kredis_list" define_method(name) do if instance_variable_defined?(ivar_symbol) instance_variable_get(ivar_symbol) else - instance_variable_set(ivar_symbol, Kredis.list(kredis_key_for_attribute(name), config: config)) + instance_variable_set(ivar_symbol, Kredis.list(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config)) end end end - def kredis_unique_list(name, limit: nil, config: :shared) + def kredis_unique_list(name, limit: nil, key: nil, config: :shared) ivar_symbol = :"@#{name}_kredis_unique_list" define_method(name) do if instance_variable_defined?(ivar_symbol) instance_variable_get(ivar_symbol) else - instance_variable_set(ivar_symbol, Kredis.unique_list(kredis_key_for_attribute(name), limit: limit, config: config)) + instance_variable_set(ivar_symbol, Kredis.unique_list(kredis_key_evaluated(key) || kredis_key_for_attribute(name), limit: limit, config: config)) end end end - def kredis_flag(name, config: :shared) + def kredis_flag(name, key: nil, config: :shared) ivar_symbol = :"@#{name}_kredis_flag" define_method(name) do if instance_variable_defined?(ivar_symbol) instance_variable_get(ivar_symbol) else - instance_variable_set(ivar_symbol, Kredis.flag(kredis_key_for_attribute(name), config: config)) + instance_variable_set(ivar_symbol, Kredis.flag(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config)) end end define_method("#{name}?") do instance_variable_defined?(ivar_symbol) && instance_variable_get(ivar_symbol).marked? end end + + def kredis_string(name, key: nil, config: :shared) + ivar_symbol = :"@#{name}_kredis_string" + + define_method(name) do + if instance_variable_defined?(ivar_symbol) + instance_variable_get(ivar_symbol) + else + instance_variable_set(ivar_symbol, Kredis.string(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config)) + end + end + end + + def kredis_integer(name, key: nil, config: :shared) + ivar_symbol = :"@#{name}_kredis_integer" + + define_method(name) do + if instance_variable_defined?(ivar_symbol) + instance_variable_get(ivar_symbol) + else + instance_variable_set(ivar_symbol, Kredis.integer(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config)) + end + end + end end private - def kredis_key_for_attribute(name) + def kredis_key_evaluated(key) + case key + when String then key + when Proc then key.call(self) + end + end + + def kredis_key_for_attribute(name, key: nil) "#{self.class.name.tableize.gsub("/", ":")}:#{id}:#{name}" end end