Class: Hanami::Controller::Configuration
- Inherits:
-
Object
- Object
- Hanami::Controller::Configuration
- Defined in:
- 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.
Constant Summary collapse
- DEFAULT_ERROR_CODE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default HTTP code for server side errors
500
- DEFAULT_PUBLIC_DIRECTORY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default public directory
It serves as base root for file downloads
'public'.freeze
- DEFAULT_FORMATS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Default Mime type to format mapping
{ 'application/octet-stream' => :all, '*/*' => :all, 'text/html' => :html }.freeze
Instance Attribute Summary collapse
-
#action_module(value = nil) ⇒ Object
readonly
Specify which is the default action module to be included when we use the ‘Hanami::Controller.action` method.
-
#cookies(options = nil) ⇒ Object
readonly
Set default cookies options for all responses.
-
#default_charset(charset = nil) ⇒ Object
readonly
Set a charset as default fallback for all the requests without a strict requirement for the charset.
-
#default_headers(headers = nil) ⇒ Object
readonly
Set default headers for all responses.
-
#default_request_format(format = nil) ⇒ Object
readonly
Set a format as default fallback for all the requests without a strict requirement for the mime type.
-
#default_response_format(format = nil) ⇒ Object
readonly
Set a format to be used for all responses regardless of the request type.
-
#handle_exceptions(value = nil) ⇒ Object
Handle exceptions with an HTTP status or let them uncaught.
-
#modules ⇒ Array<Proc>
readonly
private
Return included modules.
- #public_directory(value = nil) ⇒ Object readonly
- #root_directory ⇒ Object readonly private
Class Method Summary collapse
-
.for(base) ⇒ Hanami::Controller::Configuration
private
Return a copy of the configuration of the framework instance associated with the given class.
Instance Method Summary collapse
-
#copy!(base) ⇒ Object
private
Copy the configuration for the given action.
-
#duplicate ⇒ Hanami::Controller::Configuration
private
Duplicate by copying the settings in a new instance.
-
#exception_handler(exception) ⇒ Object
private
Return a callable handler for the given exception.
-
#exception_handler_for(exception) ⇒ Object
private
Finds configured handler for given exception, or nil if not found.
-
#format(hash) ⇒ Object
Register a format.
-
#format_for(mime_type) ⇒ Symbol?
private
Returns a format for the given mime type.
-
#handle_exception(exception) ⇒ Object
Specify how to handle an exception with an HTTP status.
-
#handled_exception?(exception) ⇒ Boolean
private
Check if the given exception is handled.
-
#initialize ⇒ Hanami::Controller::Configuration
constructor
Initialize a configuration instance.
-
#load! ⇒ Object
private
Load the framework.
-
#mime_type_for(format) ⇒ String?
private
Returns a mime type for the given format.
-
#mime_types ⇒ Object
private
Return the configured format’s MIME types.
-
#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.
-
#reset! ⇒ Object
private
Reset all the values to the defaults.
-
#restrict_mime_types!(mime_types) ⇒ Object
private
Restrict the MIME types set only to the given set.
Constructor Details
#initialize ⇒ Hanami::Controller::Configuration
Initialize a configuration instance
110 111 112 |
# File 'lib/hanami/controller/configuration.rb', line 110 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.
306 307 308 309 310 311 312 |
# File 'lib/hanami/controller/configuration.rb', line 306 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.
618 619 620 621 622 623 624 625 626 |
# File 'lib/hanami/controller/configuration.rb', line 618 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.
559 560 561 562 563 564 565 |
# File 'lib/hanami/controller/configuration.rb', line 559 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.
586 587 588 589 590 591 592 593 594 |
# File 'lib/hanami/controller/configuration.rb', line 586 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.
487 488 489 490 491 492 493 |
# File 'lib/hanami/controller/configuration.rb', line 487 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_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.
531 532 533 534 535 536 537 |
# File 'lib/hanami/controller/configuration.rb', line 531 def default_response_format(format = nil) if format @default_response_format = Utils::Kernel.Symbol(format) else @default_response_format end 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.
161 162 163 164 165 166 167 |
# File 'lib/hanami/controller/configuration.rb', line 161 def handle_exceptions(value = nil) if value.nil? @handle_exceptions else @handle_exceptions = value end end |
#modules ⇒ Array<Proc>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return included modules
696 697 698 |
# File 'lib/hanami/controller/configuration.rb', line 696 def modules @modules end |
#public_directory(value = nil) ⇒ Object
658 659 660 661 662 663 664 |
# File 'lib/hanami/controller/configuration.rb', line 658 def public_directory(value = nil) if value.nil? @public_directory else @public_directory = root_directory.join(value).to_s end end |
#root_directory ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
656 657 658 |
# File 'lib/hanami/controller/configuration.rb', line 656 def root_directory @root_directory end |
Class Method Details
.for(base) ⇒ Hanami::Controller::Configuration
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a copy of the configuration of the framework instance associated with the given class.
When multiple instances of Hanami::Controller are used in the same application, we want to make sure that a controller or an action will receive the expected configuration.
98 99 100 101 102 |
# File 'lib/hanami/controller/configuration.rb', line 98 def self.for(base) namespace = Utils::String.new(base).namespace framework = Utils::Class.load_from_pattern!("(#{namespace}|Hanami)::Controller") framework.configuration.duplicate end |
Instance Method Details
#copy!(base) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Copy the configuration for the given action
728 729 730 731 732 |
# File 'lib/hanami/controller/configuration.rb', line 728 def copy!(base) modules.each do |mod| base.class_eval(&mod) end end |
#duplicate ⇒ Hanami::Controller::Configuration
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Duplicate by copying the settings in a new instance.
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
# File 'lib/hanami/controller/configuration.rb', line 672 def duplicate Configuration.new.tap do |c| c.handle_exceptions = handle_exceptions c.handled_exceptions = handled_exceptions.dup c.action_module = action_module c.modules = modules.dup c.formats = formats.dup c.default_request_format = default_request_format c.default_response_format = default_response_format c.default_charset = default_charset c.default_headers = default_headers.dup c.public_directory = public_directory c. = .dup end end |
#exception_handler(exception) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a callable handler for the given exception
202 203 204 |
# File 'lib/hanami/controller/configuration.rb', line 202 def exception_handler(exception) exception_handler_for(exception) || DEFAULT_ERROR_CODE end |
#exception_handler_for(exception) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Finds configured handler for given exception, or nil if not found.
227 228 229 230 231 232 233 |
# File 'lib/hanami/controller/configuration.rb', line 227 def exception_handler_for(exception) @handled_exceptions.each do |exception_class, handler| return handler if exception.kind_of?(exception_class) end nil end |
#format(hash) ⇒ Object
Register a format
417 418 419 420 421 422 |
# File 'lib/hanami/controller/configuration.rb', line 417 def format(hash) symbol, mime_type = *Utils::Kernel.Array(hash) @formats[Utils::Kernel.String(mime_type)] = Utils::Kernel.Symbol(symbol) @mime_types = nil end |
#format_for(mime_type) ⇒ Symbol?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a format for the given mime type
638 639 640 |
# File 'lib/hanami/controller/configuration.rb', line 638 def format_for(mime_type) @formats[mime_type] 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`.
189 190 191 192 |
# File 'lib/hanami/controller/configuration.rb', line 189 def handle_exception(exception) @handled_exceptions.merge!(exception) _sort_handled_exceptions! end |
#handled_exception?(exception) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the given exception is handled.
214 215 216 217 |
# File 'lib/hanami/controller/configuration.rb', line 214 def handled_exception?(exception) handled_exceptions && !exception_handler_for(exception).nil? end |
#load! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Load the framework
738 739 740 |
# File 'lib/hanami/controller/configuration.rb', line 738 def load! freeze end |
#mime_type_for(format) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a mime type for the given format
650 651 652 |
# File 'lib/hanami/controller/configuration.rb', line 650 def mime_type_for(format) @formats.key(format) end |
#mime_types ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the configured format’s MIME types
431 432 433 434 435 436 |
# File 'lib/hanami/controller/configuration.rb', line 431 def mime_types @mime_types ||= begin ((@formats.keys - DEFAULT_FORMATS.keys) + Hanami::Action::Mime::MIME_TYPES.values).freeze end 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.
357 358 359 360 361 362 363 |
# File 'lib/hanami/controller/configuration.rb', line 357 def prepare(&blk) if block_given? @modules.push(blk) else raise ArgumentError.new('Please provide a block') end end |
#reset! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reset all the values to the defaults
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/hanami/controller/configuration.rb', line 702 def reset! @handle_exceptions = true @handled_exceptions = {} @modules = [] @formats = DEFAULT_FORMATS.dup @mime_types = nil @default_request_format = nil @default_response_format = nil @default_charset = nil @default_headers = {} @cookies = {} @root_directory = ::Pathname.new(Dir.pwd).realpath @public_directory = root_directory.join(DEFAULT_PUBLIC_DIRECTORY).to_s @action_module = ::Hanami::Action end |
#restrict_mime_types!(mime_types) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Restrict the MIME types set only to the given set
446 447 448 |
# File 'lib/hanami/controller/configuration.rb', line 446 def restrict_mime_types!(mime_types) @mime_types = self.mime_types & mime_types end |