Sha256: 6fbc9391291126d56ee5192ba60d7518b2ed41abe5ace1fa680d857556fc5e93

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

require 'hanami/action/cache/directives'

module Hanami
  class Action
    module Cache
      # Module with Cache-Control logic
      #
      # @since 0.3.0
      # @api private
      module CacheControl
        # The HTTP header for Cache-Control
        #
        # @since 0.3.0
        # @api private
        HEADER = 'Cache-Control'.freeze

        # @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
                ::Hash.new
              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, _)
          res.headers.merge!(self.class.cache_control_directives.headers) unless res.headers.include? HEADER
          super
        end

        # Class which stores CacheControl values
        #
        # @since 0.3.0
        # @api private
        class Directives
          # @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?
              { HEADER => @directives.join(', ') }
            else
              {}
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hanami-controller-2.0.0.alpha8 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.0.alpha6 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.0.alpha5 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.0.alpha4 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.0.alpha3 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.0.alpha2 lib/hanami/action/cache/cache_control.rb
hanami-controller-2.0.0.alpha1 lib/hanami/action/cache/cache_control.rb