Sha256: 24fa9c65b158b23aced8bb1061cfe0de15081e7fd0afdea83910f85d3e91e2c3

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module Catche
  module Model

    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)[:expire]
    end

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

  end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
catche-0.2.4 lib/catche/model.rb