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

- old
+ new

@@ -1,93 +1,83 @@ module Kredis::Attributes extend ActiveSupport::Concern class_methods do def kredis_proxy(name, key: nil, config: :shared) - ivar_symbol = :"@#{name}_kredis_proxy" + kredis_connection_with __method__, name, key, config: config + end - 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 + def kredis_string(name, key: nil, config: :shared) + kredis_connection_with __method__, name, key, config: config end - def kredis_list(name, key: nil, config: :shared) - ivar_symbol = :"@#{name}_kredis_list" + def kredis_integer(name, key: nil, config: :shared) + kredis_connection_with __method__, name, key, config: config + end - 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_evaluated(key) || kredis_key_for_attribute(name), config: config)) - end - end + def kredis_datetime(name, key: nil, config: :shared) + kredis_connection_with __method__, name, key, config: config end - def kredis_unique_list(name, limit: nil, key: nil, config: :shared) - ivar_symbol = :"@#{name}_kredis_unique_list" + def kredis_flag(name, key: nil, config: :shared) + kredis_connection_with __method__, name, key, config: config - 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_evaluated(key) || kredis_key_for_attribute(name), limit: limit, config: config)) - end + define_method("#{name}?") do + send(name).marked? end end - def kredis_flag(name, key: nil, config: :shared) - ivar_symbol = :"@#{name}_kredis_flag" + def kredis_enum(name, key: nil, values:, default:, config: :shared) + kredis_connection_with __method__, name, key, values: values, default: default, config: config + end - 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_evaluated(key) || kredis_key_for_attribute(name), config: config)) - end - end + def kredis_json(name, key: nil, config: :shared) + kredis_connection_with __method__, name, key, config: config + end - define_method("#{name}?") do - instance_variable_defined?(ivar_symbol) && instance_variable_get(ivar_symbol).marked? - end + def kredis_list(name, key: nil, typed: :string, config: :shared) + kredis_connection_with __method__, name, key, typed: typed, config: config end - def kredis_string(name, key: nil, config: :shared) - ivar_symbol = :"@#{name}_kredis_string" + def kredis_unique_list(name, limit: nil, key: nil, typed: :string, config: :shared) + kredis_connection_with __method__, name, key, limit: limit, typed: typed, config: config + end - 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 + def kredis_set(name, key: nil, typed: :string, config: :shared) + kredis_connection_with __method__, name, key, typed: typed, config: config end - def kredis_integer(name, key: nil, config: :shared) - ivar_symbol = :"@#{name}_kredis_integer" + def kredis_slot(name, key: nil, config: :shared) + kredis_connection_with __method__, name, key, config: config + end - 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)) + def kredis_slots(name, available:, key: nil, config: :shared) + kredis_connection_with __method__, name, key, available: available, config: config + end + + private + def kredis_connection_with(method, name, key, **options) + ivar_symbol = :"@#{name}_#{method}" + type = method.to_s.sub("kredis_", "") + + define_method(name) do + if instance_variable_defined?(ivar_symbol) + instance_variable_get(ivar_symbol) + else + instance_variable_set(ivar_symbol, Kredis.send(type, kredis_key_evaluated(key) || kredis_key_for_attribute(name), **options)) + end end end - end end private 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) + def kredis_key_for_attribute(name) "#{self.class.name.tableize.gsub("/", ":")}:#{id}:#{name}" end end