Sha256: ada1c47fb29a8af49447872df157644c155c8b04f1c569374a4d83faad936452

Contents?: true

Size: 1.46 KB

Versions: 15

Compression:

Stored size: 1.46 KB

Contents

class Roda
  module RodaPlugins
    # The default_headers plugin accepts a hash of headers,
    # and overrides the default_headers method in the
    # response class to be a copy of the headers.
    #
    # Note that when using this module, you should not
    # attempt to mutate of the values set in the default
    # headers hash.
    #
    # Example:
    #
    #   plugin :default_headers, 'Content-Type'=>'text/csv'
    #
    # You can also modify the default headers later:
    #
    #   plugin :default_headers
    #   default_headers['Foo'] = 'bar'
    #   default_headers.merge!('Bar'=>'baz')
    module DefaultHeaders
      # Merge the given headers into the existing default headers, if any.
      def self.configure(app, headers={})
        app.instance_eval do
          @default_headers ||= {}
          @default_headers.merge!(headers)
        end
      end 

      module ClassMethods
        # The default response headers to use for the current class.
        attr_reader :default_headers

        # Copy the default headers into the subclass when inheriting.
        def inherited(subclass)
          super
          subclass.instance_variable_set(:@default_headers, default_headers.dup)
        end
      end

      module ResponseMethods
        # Get the default headers from the related roda class.
        def default_headers
          self.class.roda_class.default_headers.dup
        end
      end
    end

    register_plugin(:default_headers, DefaultHeaders)
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
roda-1.1.0 lib/roda/plugins/default_headers.rb
roda-cj-1.0.5 lib/roda/plugins/default_headers.rb
roda-cj-1.0.4 lib/roda/plugins/default_headers.rb
roda-cj-1.0.3 lib/roda/plugins/default_headers.rb
roda-cj-1.0.2 lib/roda/plugins/default_headers.rb
roda-cj-1.0.1 lib/roda/plugins/default_headers.rb
roda-cj-1.0.0 lib/roda/plugins/default_headers.rb
roda-1.0.0 lib/roda/plugins/default_headers.rb
roda-cj-0.9.6 lib/roda/plugins/default_headers.rb
roda-cj-0.9.5 lib/roda/plugins/default_headers.rb
roda-cj-0.9.4 lib/roda/plugins/default_headers.rb
roda-cj-0.9.3 lib/roda/plugins/default_headers.rb
roda-cj-0.9.2 lib/roda/plugins/default_headers.rb
roda-cj-0.9.1 lib/roda/plugins/default_headers.rb
roda-0.9.0 lib/roda/plugins/default_headers.rb