Sha256: 479403b2adaf38fada74cdcb2fb875e212fce59bcf4e1f219779f382dd7241ba

Contents?: true

Size: 1.97 KB

Versions: 13

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Hanami
  class Action
    module Cache
      # Module with Cache-Control logic
      #
      # @since 0.3.0
      # @api private
      module CacheControl
        # @since 0.3.0
        # @api private
        def self.included(base)
          base.class_eval do
            extend ClassMethods
            @cache_control_directives = nil
          end
        end

        # @since 0.3.0
        # @api private
        module ClassMethods
          # @since 0.3.0
          # @api private
          def cache_control(*values)
            @cache_control_directives ||= Directives.new(*values)
          end

          # @since 0.3.0
          # @api private
          def cache_control_directives
            @cache_control_directives || Object.new.tap do |null_object|
              def null_object.headers
                {}
              end
            end
          end
        end

        # Finalize the response including default cache headers into the response
        #
        # @since 0.3.0
        # @api private
        #
        # @see Hanami::Action#finish
        def finish(_, res, _)
          unless res.headers.include?(Action::CACHE_CONTROL)
            res.headers.merge!(self.class.cache_control_directives.headers)
          end

          super
        end

        # Class which stores CacheControl values
        #
        # @since 0.3.0
        # @api private
        class Directives
          # @since 2.0.0
          # @api private
          SEPARATOR = ", "
          private_constant :SEPARATOR

          # @since 0.3.0
          # @api private
          def initialize(*values)
            @directives = Hanami::Action::Cache::Directives.new(*values)
          end

          # @since 0.3.0
          # @api private
          def headers
            if @directives.any?
              {Action::CACHE_CONTROL => @directives.join(SEPARATOR)}
            else
              {}
            end
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hanami-controller-2.2.0 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.2.0.rc1 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.2.0.beta2 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.2.0.beta1 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.1.0 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.1.0.rc3 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.1.0.rc2 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.1.0.rc1 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.1.0.beta2 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.1.0.beta1 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.2 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.1 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.0 lib/hanami/action/cache/cache_control.rb