Class: Hanami::Controller::Configuration

Inherits:
Object
  • Object
show all
Defined in:
gems/gems/hanami-controller-1.3.2/lib/hanami/controller/configuration.rb

Overview

Configuration for the framework, controllers and actions.

Hanami::Controller has its own global configuration that can be manipulated via Hanami::Controller.configure.

Every time that Hanami::Controller and Hanami::Action are included, that global configuration is being copied to the recipient. The copy will inherit all the settings from the original, but all the subsequent changes aren’t reflected from the parent to the children, and viceversa.

This architecture allows to have a global configuration that capture the most common cases for an application, and let controllers and single actions to specify exceptions.

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHanami::Controller::Configuration

Initialize a configuration instance

Since:

  • 0.2.0

def initialize
  reset!
end

Instance Attribute Details

#action_module(value) ⇒ Object #action_moduleModule

Specify which is the default action module to be included when we use the Hanami::Controller.action method.

This setting is useful when we use multiple instances of the framework in the same process, so we want to ensure that the actions will include MyApp::Action, rather than AnotherApp::Action.

If not set, the default value is Hanami::Action

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/controller'

Hanami::Controller.configuration.action_module # => Hanami::Action

Setting the value

require 'hanami/controller'

module MyAction
end

Hanami::Controller.configure do
  action_module MyAction
end

module Dashboard
  # It includes MyAction, instead of Hanami::Action
  class Index
    include MyAction

    def call(params)
      # ...
    end
  end
end

Duplicated framework

require 'hanami/controller'

module MyApp
  Controller = Hanami::Controller.duplicate(self)

  module Controllers::Dashboard
    include MyApp::Controller

    # It includes MyApp::Action, instead of Hanami::Action
    class Index
      include MyApp::Action

      def call(params)
        # ...
      end
    end
  end
end

Overloads:

  • #action_module(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (Module)

      the module to be included in all the actions

  • #action_moduleModule

    Gets the value

    Returns:

    • (Module)

See Also:

Since:

  • 0.2.0

def action_module(value = nil)
  if value.nil?
    @action_module
  else
    @action_module = value
  end
end

#cookies(options = nil) ⇒ Object

Set default cookies options for all responses

By default this value is an empty hash.

Examples:

Getting the value

require 'hanami/controller'

Hanami::Controller.configuration.cookies # => {}

Setting the value

require 'hanami/controller'

Hanami::Controller.configure do
  cookies({
    domain: 'hanamirb.org',
    path: '/controller',
    secure: true,
    httponly: true
  })
end

Since:

  • 0.4.0

def cookies(options = nil)
  if options
    @cookies.merge!(
      options.reject { |_, v| v.nil? }
    )
  else
    @cookies
  end
end

#default_charset(charset = nil) ⇒ Object

Set a charset as default fallback for all the requests without a strict requirement for the charset.

By default this value is nil.

Examples:

Getting the value

require 'hanami/controller'

Hanami::Controller.configuration.default_charset # => nil

Setting the value

require 'hanami/controller'

Hanami::Controller.configure do
  default_charset 'koi8-r'
end

See Also:

Since:

  • 0.3.0

def default_charset(charset = nil)
  if charset
    @default_charset = charset
  else
    @default_charset
  end
end

#default_headers(headers = nil) ⇒ Object

Set default headers for all responses

By default this value is an empty hash.

Examples:

Getting the value

require 'hanami/controller'

Hanami::Controller.configuration.default_headers # => {}

Setting the value

require 'hanami/controller'

Hanami::Controller.configure do
  default_headers({
    'X-Frame-Options' => 'DENY'
  })
end

Since:

  • 0.4.0

def default_headers(headers = nil)
  if headers
    @default_headers.merge!(
      headers.reject {|_,v| v.nil? }
    )
  else
    @default_headers
  end
end

#default_request_format(format) ⇒ Object #default_request_formatSymbol?

Set a format as default fallback for all the requests without a strict requirement for the mime type.

The given format must be coercible to a symbol, and be a valid mime type alias. If it isn’t, at the runtime the framework will raise a Hanami::Controller::UnknownFormatError.

By default this value is nil.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/controller'

Hanami::Controller.configuration.default_request_format # => nil

Setting the value

require 'hanami/controller'

Hanami::Controller.configure do
  default_request_format :html
end

Overloads:

  • #default_request_format(format) ⇒ Object

    Sets the given value

    Parameters:

    • format (#to_sym)

      the symbol format

    Raises:

    • (TypeError)

      if it cannot be coerced to a symbol

  • #default_request_formatSymbol?

    Gets the value

    Returns:

    • (Symbol, nil)

See Also:

Since:

  • 0.5.0

def default_request_format(format = nil)
  if format
    @default_request_format = Utils::Kernel.Symbol(format)
  else
    @default_request_format
  end
end

#default_response_format(format) ⇒ Object #default_response_formatSymbol?

Set a format to be used for all responses regardless of the request type.

The given format must be coercible to a symbol, and be a valid mime type alias. If it isn’t, at the runtime the framework will raise a Hanami::Controller::UnknownFormatError.

By default this value is nil.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/controller'

Hanami::Controller.configuration.default_response_format # => nil

Setting the value

require 'hanami/controller'

Hanami::Controller.configure do
  default_response_format :json
end

Overloads:

  • #default_response_format(format) ⇒ Object

    Sets the given value

    Parameters:

    • format (#to_sym)

      the symbol format

    Raises:

    • (TypeError)

      if it cannot be coerced to a symbol

  • #default_response_formatSymbol?

    Gets the value

    Returns:

    • (Symbol, nil)

See Also:

Since:

  • 0.5.0

def default_response_format(format = nil)
  if format
    @default_response_format = Utils::Kernel.Symbol(format)
  else
    @default_response_format
  end
end

#formatsObject (protected)

Since:

  • 0.2.0

def formats
  @formats
end

#handled_exceptionsObject (protected)

Since:

  • 0.2.0

def handled_exceptions
  @handled_exceptions
end

#modules=(value) ⇒ Object (protected)

Since:

  • 0.2.0

def modules=(value)
  @modules = value
end

#public_directory(value = nil) ⇒ Object

Since:

  • 0.2.0

def public_directory(value = nil)
  if value.nil?
    @public_directory
  else
    @public_directory = root_directory.join(value).to_s
  end
end

Instance Method Details

#format(hash) ⇒ Object

Register a format

Examples:

require 'hanami/controller'

Hanami::Controller.configure do
  format custom: 'application/custom'
end

module Articles
  class Index
    include Hanami::Action

    def call(params)
      # ...
    end
  end

  class Show
    include Hanami::Action

    def call(params)
      # ...
      self.format = :custom
    end
  end
end

action = Articles::Index.new

action.call({ 'HTTP_ACCEPT' => 'text/html' })
  # => Content-Type "text/html"
action.format # => :html

action.call({ 'HTTP_ACCEPT' => 'application/custom' })
  # => Content-Type "application/custom"
action.format # => :custom

action = Articles::Show.new

action.call({ 'HTTP_ACCEPT' => 'text/html' })
  # => Content-Type "application/custom"
action.format # => :custom

Parameters:

  • hash (Hash)

    the symbol format must be the key and the mime type string must be the value of the hash

See Also:

Since:

  • 0.2.0

def format(hash)
  symbol, mime_type = *Utils::Kernel.Array(hash)

  @formats[Utils::Kernel.String(mime_type)] = Utils::Kernel.Symbol(symbol)
  @mime_types = nil
end

#handle_exception(exception) ⇒ Object

Specify how to handle an exception with an HTTP status

Raised exceptions will return the configured HTTP status, only if handled_exceptions is set on true.

Examples:

require 'hanami/controller'

Hanami::Controller.configure do
  handle_exception ArgumentError => 400
end

Parameters:

  • exception (Hash)

    the exception class must be the key and the HTTP status the value

See Also:

Since:

  • 0.2.0

def handle_exception(exception)
  @handled_exceptions.merge!(exception)
  _sort_handled_exceptions!
end

#handle_exceptions(value) ⇒ Object #handle_exceptionsTrueClass, FalseClass

Handle exceptions with an HTTP status or let them uncaught

If this value is set to true, the configured exceptions will return the specified HTTP status, the rest of them with 500.

If this value is set to false, the exceptions won’t be caught.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/controller'

Hanami::Controller.configuration.handle_exceptions # => true

Setting the value

require 'hanami/controller'

Hanami::Controller.configure do
  handle_exceptions false
end

Overloads:

  • #handle_exceptions(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (TrueClass, FalseClass)

      true or false, default to true

  • #handle_exceptionsTrueClass, FalseClass

    Gets the value

    Returns:

    • (TrueClass, FalseClass)

See Also:

Since:

  • 0.2.0

def handle_exceptions(value = nil)
  if value.nil?
    @handle_exceptions
  else
    @handle_exceptions = value
  end
end

#handle_exceptions=(value) ⇒ Object

Returns void

Returns:

  • void

See Also:

Since:

  • 0.2.0

def handle_exceptions=(value)
  @handle_exceptions = value
end

#prepare(&blk) ⇒ void

This method returns an undefined value.

Configure the logic to be executed when Hanami::Action is included This is useful to DRY code by having a single place where to configure shared behaviors like authentication, sessions, cookies etc.

This method can be called multiple times.

Examples:

Configure shared logic.

require 'hanami/controller'

Hanami::Controller.configure do
  prepare do
    include Hanami::Action::Session
    include MyAuthentication
    use SomeMiddleWare

    before { authenticate! }
  end
end

module Dashboard
  class Index
    # When Hanami::Action is included, it will:
    #   * Include `Hanami::Action::Session` and `MyAuthentication`
    #   * Configure to use `SomeMiddleWare`
    #   * Configure a `before` callback that triggers `#authenticate!`
    include Hanami::Action

    def call(params)
      # ...
    end
  end
end

Parameters:

  • blk (Proc)

    the code block

Raises:

  • (ArgumentError)

    if called without passing a block

See Also:

Since:

  • 0.3.0

def prepare(&blk)
  if block_given?
    @modules.push(blk)
  else
    raise ArgumentError.new('Please provide a block')
  end
end