Module: Kiki::ClassMethods

Defined in:
lib/kiki.rb

Instance Method Summary collapse

Instance Method Details

#cache_key(namespace, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kiki.rb', line 14

def cache_key(namespace, options = {})
  class_eval(<<-EVAL, __FILE__, __LINE__)
    class << self
      @@#{namespace}_key = Key.new "#{namespace}",
                                   "#{options[:delimiter] || ":"}",
                                   "#{options[:domain] || self.name.downcase.pluralize}",
                                   :"#{options[:identifier] || :id}"

      def #{namespace}_key(*args)
        return args.empty? ? @@#{namespace}_key : @@#{namespace}_key.for(*args)
      end
    end

    def #{namespace}_key(*args)
      return self.class.#{namespace}_key(*[self, *args])
    end
  EVAL

  return self.send("#{namespace}_key")
end