Sha256: af9444fc4171bff55afeced3cc459aa2c191868028cf616ff5dff866bad77a19

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# -*- coding: utf-8 -*-
module Redisable
  module Key
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      # return model_name:id:field_name
      def redis_key(name, options={})
        klass_name ||= self.name
        define_method(name) do |id_=nil|
          id_ ||= if options[:id]
                    options[:id].call(self)
                  elsif defined?(self.id)
                    self.id
                  end
          Key.join_key(klass_name, id_, name, options[:blank_field])
        end

        define_singleton_method(name) do |id_=nil|
          id_ ||= if options[:id]
                    options[:id].call(self)
                  elsif defined?(self.id)
                    self.id
                  end
          Key.join_key(klass_name, id_, name, options[:blank_field])
        end
      end
    end

    def self.join_key(klass_name, id, name, is_blank_field)
      k = "#{klass_name}"
      k << ":#{id}" if id
      k << ":#{name}" unless is_blank_field
      k
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redisable-0.2.0 lib/redisable/key.rb
redisable-0.1.1 lib/redisable/key.rb