Class: Hanami::Controller::Configuration
- Inherits:
-
Object
- Object
- Hanami::Controller::Configuration
- 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.
Instance Attribute Summary collapse
-
#action_module(value = nil) ⇒ Object
Specify which is the default action module to be included when we use the
Hanami::Controller.action
method. -
#cookies(options = nil) ⇒ Object
Set default cookies options for all responses.
-
#default_charset(charset = nil) ⇒ Object
Set a charset as default fallback for all the requests without a strict requirement for the charset.
-
#default_headers(headers = nil) ⇒ Object
Set default headers for all responses.
-
#default_request_format(format = nil) ⇒ Object
Set a format as default fallback for all the requests without a strict requirement for the mime type.
-
#default_response_format(format = nil) ⇒ Object
Set a format to be used for all responses regardless of the request type.
- #formats ⇒ Object protected
- #handled_exceptions ⇒ Object protected
- #modules ⇒ Object writeonly protected
- #public_directory(value = nil) ⇒ Object
Instance Method Summary collapse
-
#format(hash) ⇒ Object
Register a format.
-
#handle_exception(exception) ⇒ Object
Specify how to handle an exception with an HTTP status.
-
#handle_exceptions(value = nil) ⇒ Object
Handle exceptions with an HTTP status or let them uncaught.
-
#handle_exceptions(value) ⇒ Object
Void.
-
#initialize ⇒ Hanami::Controller::Configuration
constructor
Initialize a configuration instance.
-
#prepare(&blk) ⇒ void
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.
Constructor Details
#initialize ⇒ Hanami::Controller::Configuration
Initialize a configuration instance
Source: | on GitHub
def initialize reset! end |
Instance Attribute Details
#action_module(value) ⇒ Object #action_module ⇒ Module
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.
Source: | on GitHub
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.
Source: | on GitHub
def ( = nil) if @cookies.merge!( .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.
Source: | on GitHub
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.
Source: | on GitHub
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_format ⇒ Symbol?
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.
#default_response_format(format) ⇒ Object #default_response_format ⇒ Symbol?
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.
#handled_exceptions ⇒ Object (protected)
Source: | on GitHub
def handled_exceptions @handled_exceptions end |
#public_directory(value = nil) ⇒ Object
Source: | on GitHub
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
#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
.
Source: | on GitHub
def handle_exception(exception) @handled_exceptions.merge!(exception) _sort_handled_exceptions! end |
#handle_exceptions(value) ⇒ Object #handle_exceptions ⇒ TrueClass, 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.
Source: | on GitHub
def handle_exceptions(value = nil) if value.nil? @handle_exceptions else @handle_exceptions = value end end |
#handle_exceptions=(value) ⇒ Object
Returns void
Source: | on GitHub
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.
Source: | on GitHub
def prepare(&blk) if block_given? @modules.push(blk) else raise ArgumentError.new('Please provide a block') end end |