Sha256: 5a77620949d8fa88345da39f653d33e89b902520984dfaf13b7fd03573987945

Contents?: true

Size: 812 Bytes

Versions: 12

Compression:

Stored size: 812 Bytes

Contents

module ActiveModel
  class Serializer
    module Caching
      def to_json(*args)
        if caching_enabled?
          key = expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json'])
          cache.fetch key do
            super
          end
        else
          super
        end
      end

      def serialize(*args)
        if caching_enabled?
          key = expand_cache_key([self.class.to_s.underscore, cache_key, 'serialize'])
          cache.fetch key do
            serialize_object
          end
        else
          serialize_object
        end
      end

      private

      def caching_enabled?
        perform_caching && cache && respond_to?(:cache_key)
      end

      def expand_cache_key(*args)
        ActiveSupport::Cache.expand_cache_key(args)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
gpi-active_model_serializers-0.8.5 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.5.alpha1 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.4 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.4.alpha5 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.4.alpha4 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.4.alpha3 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.4.alpha2 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.4.alpha1 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.3.rc1 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.3.alpha2 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.3.alpha1 lib/active_model/serializer/caching.rb
gpi-active_model_serializers-0.8.2 lib/active_model/serializer/caching.rb