Sha256: eebd97f13903e5ac6335827416a9a8e66574f4bcc28f10dfc4e80e26c4c39961

Contents?: true

Size: 1.89 KB

Versions: 4

Compression:

Stored size: 1.89 KB

Contents

module Catche
  module Model
    module Base
      extend ActiveSupport::Concern

      included do
        class_attribute :catche_class,
                        :catche_collection_tag,
                        :catche_tag_identifier,
                        :catche_associations

        # Expiration callbacks

        after_update  :expire_resource_and_collection!
        after_destroy :expire_resource_and_collection!

        after_create  :expire_collection!
      end

      module ClassMethods

        # Configures catche
        #
        #   catche :through => :project, :catche_class => Task
        def catche(options={})
          options = {
            :class          => self,
            :tag_identifier => :id,
            :collection_tag => nil,
            :associations   => [options[:through]].flatten.compact
          }.merge(options)

          options.each do |key, value|
            self.send("catche_#{key}=", value) if self.respond_to?("catche_#{key}")
          end

          self.catche_collection_tag ||= self.catche_class.name.downcase.pluralize
        end

        def catche_reset!
          catche {}
        end

        def catche_tag
          self.catche_collection_tag || self.name.downcase.pluralize
        end

        def catche_tag=(value)
          self.catche_collection_tag = value
        end

        def catche_tag_identifier
          super || :id
        end

      end

      def catche_tag
        Tag.join self.class.catche_tag, self.send(:id)
      end

      def expire_resource_and_collection!
        expire_collection!
        expire_resource!
      end

      def expire_collection!
        Catche::Tag.expire! *Catche::Tag::Collect.collection(self, self.class)[:expire]
      end

      def expire_resource!
        Catche::Tag.expire! *Catche::Tag::Collect.resource(self)[:expire]
      end

    end
  end
end

ActiveRecord::Base.send :include, Catche::Model::Base

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
catche-0.2.3 lib/catche/model/base.rb
catche-0.2.2 lib/catche/model/base.rb
catche-0.2.1 lib/catche/model/base.rb
catche-0.2 lib/catche/model/base.rb