Sha256: 75c6caddbaa50d1266dd018ff997bc861dd70c7209f4116af9dca1e080d7f508

Contents?: true

Size: 903 Bytes

Versions: 3

Compression:

Stored size: 903 Bytes

Contents

module Cachers
  class Base

    attr_reader :record

    def initialize(record)
      @record = record
    end

    def cache
      # Sublcass must implement this
    end

    def uncache
      # Subclass must implement this
    end

    def recache
      if record.previous_changes.any?
        previous_record = record.dup
        record.previous_changes.each do |attribute, values|
          previous_record.send "#{attribute}=", values.first
        end
        previous_record.id = record.id
        self.class.new(previous_record).uncache
      else
        uncache
      end
      cache
    end

    def self.inherited(subclass)
      if model = (subclass.name.sub(/Cacher$/, '').constantize rescue nil)
        model.include Concern
        model.class_eval do
          define_method :cacher do
            @cacher ||= subclass.new(self)
          end
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cachers-4.0.0.0 lib/cachers/base.rb
cachers-0.1.1 lib/cachers/base.rb
cachers-0.0.1 lib/cachers/base.rb