Sha256: 7884b1374b067f89c4206ba155ebe19c4e09e779fec0306541c938745c8b3f35

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Kredis::Attributes
  extend ActiveSupport::Concern

  class_methods do
    def kredis_list(name, 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))
        end
      end
    end

    def kredis_unique_list(name, limit: 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))
        end
      end
    end

    def kredis_flag(name, 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))
        end
      end

      define_method("#{name}?") do
        instance_variable_defined?(ivar_symbol) && instance_variable_get(ivar_symbol).marked?
      end
    end
  end

  private
    def kredis_key_for_attribute(name)
      "#{self.class.name.tableize.gsub("/", ":")}:#{id}:#{name}"
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kredis-0.1.1 lib/kredis/attributes.rb