Class: Lotus::Controller::Configuration
- Inherits:
-
Object
- Object
- Lotus::Controller::Configuration
- Defined in:
- lib/lotus/controller/configuration.rb
Overview
Configuration for the framework, controllers and actions.
Lotus::Controller has its own global configuration that can be manipulated via ‘Lotus::Controller.configure`.
Every time that ‘Lotus::Controller` and `Lotus::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_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 ‘Lotus::Controller.action` method.
-
#default_format(format = nil) ⇒ Object
readonly
Set a format as default fallback for all the requests without a strict requirement for the mime type.
-
#handle_exceptions(value = nil) ⇒ Object
Decide if handle exceptions with an HTTP status or let them uncaught.
-
#modules(&blk) ⇒ Object
readonly
Specify the default modules to be included when ‘Lotus::Action` (or the `action_module`) is included.
Class Method Summary collapse
-
.for(base) ⇒ Lotus::Controller::Configuration
private
Return a copy of the configuration of the framework instance associated with the given class.
Instance Method Summary collapse
-
#duplicate ⇒ Lotus::Controller::Configuration
private
Duplicate by copying the settings in a new instance.
-
#exception_code(exception) ⇒ Object
private
Return the HTTP status for the given exception.
-
#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.
-
#initialize ⇒ Lotus::Controller::Configuration
constructor
Initialize a configuration instance.
-
#load!(base) ⇒ Object
private
Load the configuration for the given action.
-
#mime_type_for(format) ⇒ String?
Returns a mime type for the given format.
-
#reset! ⇒ Object
private
Reset all the values to the defaults.
Constructor Details
#initialize ⇒ Lotus::Controller::Configuration
Initialize a configuration instance
102 103 104 |
# File 'lib/lotus/controller/configuration.rb', line 102 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 ‘Lotus::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 ‘Lotus::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.
268 269 270 271 272 273 274 |
# File 'lib/lotus/controller/configuration.rb', line 268 def action_module(value = nil) if value.nil? @action_module else @action_module = value end end |
#default_format(format) ⇒ Object #default_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 ‘Lotus::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.
431 432 433 434 435 436 437 |
# File 'lib/lotus/controller/configuration.rb', line 431 def default_format(format = nil) if format @default_format = Utils::Kernel.Symbol(format) else @default_format end end |
#handle_exceptions(value) ⇒ Object #handle_exceptions ⇒ TrueClass, FalseClass
Decide if 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.
151 152 153 154 155 156 157 |
# File 'lib/lotus/controller/configuration.rb', line 151 def handle_exceptions(value = nil) if value.nil? @handle_exceptions else @handle_exceptions = value end end |
#modules(blk) ⇒ Object #modules ⇒ Array
Specify the default modules to be included when ‘Lotus::Action` (or the `action_module`) is included. This also works with `Lotus::Controller.action`.
If not set, this option will be ignored.
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.
329 330 331 332 333 334 335 |
# File 'lib/lotus/controller/configuration.rb', line 329 def modules(&blk) if block_given? @modules.push(blk) else @modules end end |
Class Method Details
.for(base) ⇒ Lotus::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 Lotus::Controller are used in the same application, we want to make sure that a controller or an action will receive the expected configuration.
90 91 92 93 94 |
# File 'lib/lotus/controller/configuration.rb', line 90 def self.for(base) namespace = Utils::String.new(base).namespace framework = Utils::Class.load!("(#{namespace}|Lotus)::Controller") framework.configuration.duplicate end |
Instance Method Details
#duplicate ⇒ Lotus::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.
468 469 470 471 472 473 474 475 476 477 |
# File 'lib/lotus/controller/configuration.rb', line 468 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_format = default_format end end |
#exception_code(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 the HTTP status for the given exception
Raised exceptions will return the configured HTTP status, only if
`handled_exceptions` is set on `true`.
195 196 197 |
# File 'lib/lotus/controller/configuration.rb', line 195 def exception_code(exception) @handled_exceptions.fetch(exception) { DEFAULT_ERROR_CODE } end |
#format(hash) ⇒ Object
Register a format
387 388 389 390 391 392 |
# File 'lib/lotus/controller/configuration.rb', line 387 def format(hash) symbol, mime_type = *Utils::Kernel.Array(hash) @formats.merge! Utils::Kernel.String(mime_type) => Utils::Kernel.Symbol(symbol) 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
449 450 451 |
# File 'lib/lotus/controller/configuration.rb', line 449 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`.
179 180 181 |
# File 'lib/lotus/controller/configuration.rb', line 179 def handle_exception(exception) @handled_exceptions.merge!(exception) end |
#load!(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.
Load the configuration for the given action
496 497 498 499 500 |
# File 'lib/lotus/controller/configuration.rb', line 496 def load!(base) modules.each do |mod| base.class_eval(&mod) end end |
#mime_type_for(format) ⇒ String?
Returns a mime type for the given format
458 459 460 |
# File 'lib/lotus/controller/configuration.rb', line 458 def mime_type_for(format) @formats.key(format) 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
483 484 485 486 487 488 489 490 |
# File 'lib/lotus/controller/configuration.rb', line 483 def reset! @handle_exceptions = true @handled_exceptions = {} @modules = [] @formats = DEFAULT_FORMATS.dup @default_format = nil @action_module = ::Lotus::Action end |