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.
-
#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_format(format = nil) ⇒ Object
readonly
Set a format as default fallback for all the requests without a strict requirement for the mime type.
-
#default_headers(headers = nil) ⇒ Object
readonly
Set default headers for all responses.
-
#handle_exceptions(value = nil) ⇒ Object
Handle exceptions with an HTTP status or let them uncaught.
-
#modules ⇒ Array<Proc>
readonly
private
Return included modules.
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
-
#copy!(base) ⇒ Object
private
Copy the configuration for the given action.
-
#duplicate ⇒ Lotus::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.
-
#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 ⇒ Lotus::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.
-
#prepare(&blk) ⇒ void
Configure the logic to be executed when Lotus::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.
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.
281 282 283 284 285 286 287 |
# File 'lib/lotus/controller/configuration.rb', line 281 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.
523 524 525 526 527 528 529 530 531 |
# File 'lib/lotus/controller/configuration.rb', line 523 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.
464 465 466 467 468 469 470 |
# File 'lib/lotus/controller/configuration.rb', line 464 def default_charset(charset = nil) if charset @default_charset = charset else @default_charset 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.
436 437 438 439 440 441 442 |
# File 'lib/lotus/controller/configuration.rb', line 436 def default_format(format = nil) if format @default_format = Utils::Kernel.Symbol(format) else @default_format end end |
#default_headers(headers = nil) ⇒ Object
Set default headers for all responses
By default this value is an empty hash.
491 492 493 494 495 496 497 498 499 |
# File 'lib/lotus/controller/configuration.rb', line 491 def default_headers(headers = nil) if headers @default_headers.merge!( headers.reject {|_,v| v.nil? } ) else @default_headers 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.
153 154 155 156 157 158 159 |
# File 'lib/lotus/controller/configuration.rb', line 153 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
587 588 589 |
# File 'lib/lotus/controller/configuration.rb', line 587 def modules @modules 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_from_pattern!("(#{namespace}|Lotus)::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
615 616 617 618 619 |
# File 'lib/lotus/controller/configuration.rb', line 615 def copy!(base) modules.each do |mod| base.class_eval(&mod) end end |
#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.
565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/lotus/controller/configuration.rb', line 565 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 c.default_charset = default_charset c.default_headers = default_headers.dup 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
193 194 195 |
# File 'lib/lotus/controller/configuration.rb', line 193 def exception_handler(exception) @handled_exceptions.fetch(exception.class) { DEFAULT_ERROR_CODE } end |
#format(hash) ⇒ Object
Register a format
392 393 394 395 396 397 |
# File 'lib/lotus/controller/configuration.rb', line 392 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
543 544 545 |
# File 'lib/lotus/controller/configuration.rb', line 543 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`.
181 182 183 |
# File 'lib/lotus/controller/configuration.rb', line 181 def handle_exception(exception) @handled_exceptions.merge!(exception) 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.
205 206 207 208 |
# File 'lib/lotus/controller/configuration.rb', line 205 def handled_exception?(exception) handled_exceptions && !!@handled_exceptions.fetch(exception.class) { false } 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
625 626 627 |
# File 'lib/lotus/controller/configuration.rb', line 625 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
555 556 557 |
# File 'lib/lotus/controller/configuration.rb', line 555 def mime_type_for(format) @formats.key(format) end |
#prepare(&blk) ⇒ void
This method returns an undefined value.
Configure the logic to be executed when Lotus::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.
332 333 334 335 336 337 338 |
# File 'lib/lotus/controller/configuration.rb', line 332 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
593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/lotus/controller/configuration.rb', line 593 def reset! @handle_exceptions = true @handled_exceptions = {} @modules = [] @formats = DEFAULT_FORMATS.dup @default_format = nil @default_charset = nil @default_headers = {} @cookies = {} @action_module = ::Lotus::Action end |