Class: Hanami::ApplicationConfiguration
- Inherits:
-
Object
- Object
- Hanami::ApplicationConfiguration
- Defined in:
- lib/hanami/application_configuration.rb
Overview
Configuration for a Hanami application
Constant Summary collapse
- SSL_SCHEME =
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.
'https'.freeze
- DEFAULT_SSL_PORT =
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.
443
Instance Attribute Summary collapse
- #namespace ⇒ Object readonly private
- #path_prefix ⇒ Object readonly private
Instance Method Summary collapse
-
#adapter(*options) ⇒ Object
Adapter configuration.
- #app_name ⇒ Object private
-
#assets ⇒ Hanami::Config::Assets
The application will serve the static assets under these directories.
-
#body_parsers(*parsers) ⇒ Object
Body parsing configuration.
-
#controller ⇒ Hanami::Config::FrameworkConfiguration
It lazily collects all the low level settings for Hanami::Controller’s configuration and applies them when the application is loaded.
-
#controller_pattern(value = nil) ⇒ Object
Defines a relative pattern to find controllers.
-
#cookies(options = nil) ⇒ Object
Configure cookies Enable cookies (disabled by default).
-
#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.
-
#force_ssl(value = nil) ⇒ Boolean
Force ssl redirection if http scheme is set.
-
#handle_exceptions(value = nil) ⇒ Object
Decide if handle exceptions with an HTTP status or let them uncaught.
-
#host(value = nil) ⇒ Object
The URI host for this application.
-
#initialize(namespace, configurations, path_prefix, env: Environment.new) ⇒ Hanami::Configuration
constructor
private
Initialize a new configuration instance.
-
#layout(value = nil) ⇒ Object
A Hanami::Layout for this application.
-
#load_paths ⇒ Hanami::Config::LoadPaths
Application load paths The application will recursively load all the Ruby files under these paths.
-
#middleware ⇒ Object
Application middleware.
-
#model ⇒ Hanami::Config::FrameworkConfiguration
It lazily collects all the low level settings for Hanami::Model’s configuration and applies them when the application is loaded.
-
#port(value = nil) ⇒ Object
The URI port for this application.
-
#root(value = nil) ⇒ Object
The root of the application.
-
#routes(path = nil, &blk) ⇒ Object
Application routes.
-
#scheme(value = nil) ⇒ Object
The URI scheme for this application.
-
#security ⇒ Hanami::Config::Security
Returns the security policy.
-
#sessions(adapter = nil, options = {}) ⇒ Object
Configure sessions Enable sessions (disabled by default).
-
#ssl? ⇒ FalseClass, TrueClass
Check if the application uses SSL.
-
#templates(value = nil) ⇒ Object
Templates root.
-
#view ⇒ Hanami::Config::FrameworkConfiguration
It lazily collects all the low level settings for Hanami::View’s configuration and applies them when the application is loaded.
-
#view_pattern(value = nil) ⇒ Object
Defines a relative pattern to find views:.
Constructor Details
#initialize(namespace, configurations, path_prefix, env: Environment.new) ⇒ Hanami::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.
Initialize a new configuration instance
42 43 44 45 46 47 48 49 50 |
# File 'lib/hanami/application_configuration.rb', line 42 def initialize(namespace, configurations, path_prefix, env: Environment.new) @namespace = namespace @configurations = configurations @path_prefix = path_prefix @env = env @handle_exceptions = true evaluate_configurations! end |
Instance Attribute Details
#namespace ⇒ 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.
28 29 30 |
# File 'lib/hanami/application_configuration.rb', line 28 def namespace @namespace end |
#path_prefix ⇒ 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.
32 33 34 |
# File 'lib/hanami/application_configuration.rb', line 32 def path_prefix @path_prefix end |
Instance Method Details
#adapter(options) ⇒ Object #adapter ⇒ Hash
Adapter configuration. The application will instantiate adapter instance based on this configuration.
The given options must have key pairs :type and :uri If it isn’t, at the runtime the framework will raise a ‘ArgumentError`.
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.
727 728 729 730 731 732 733 |
# File 'lib/hanami/application_configuration.rb', line 727 def adapter(*) if !.empty? @adapter = else @adapter end end |
#app_name ⇒ 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.
1480 1481 1482 |
# File 'lib/hanami/application_configuration.rb', line 1480 def app_name ApplicationName.new(namespace) end |
#assets ⇒ Hanami::Config::Assets
The application will serve the static assets under these directories.
By default it’s equal to the ‘public/` directory under the application `root`.
Otherwise, you can add different relatives paths under ‘root`.
335 336 337 338 339 340 341 |
# File 'lib/hanami/application_configuration.rb', line 335 def assets(&blk) if @assets @assets.__add(&blk) else @assets ||= Config::FrameworkConfiguration.new(&blk) end end |
#body_parsers(parsers) ⇒ Object #body_parsers ⇒ Array
Body parsing configuration.
Specify a set of parsers for specific mime types that your application will use. This method will return the application’s parsers which you can use to add existing and new custom parsers for your application to use.
By default it’s an empty ‘Array`
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.
653 654 655 656 657 658 659 |
# File 'lib/hanami/application_configuration.rb', line 653 def body_parsers(*parsers) if parsers.empty? @body_parsers ||= [] else @body_parsers = parsers end end |
#controller ⇒ Hanami::Config::FrameworkConfiguration
It lazily collects all the low level settings for Hanami::Controller’s configuration and applies them when the application is loaded.
NOTE: This forwards all the configurations to Hanami::Controller, without checking them. Before to use this feature, please have a look at the current Hanami::Controller version installed.
NOTE: This may override some configurations of your application.
1431 1432 1433 |
# File 'lib/hanami/application_configuration.rb', line 1431 def controller @controller ||= Config::FrameworkConfiguration.new end |
#controller_pattern(value) ⇒ Object #controller_pattern ⇒ String
Defines a relative pattern to find controllers.
By default this equals to "Controllers::%{controller}::%{action}"
That means controllers must be structured like this: Bookshelf::Controllers::Dashboard::Index
, where Bookshelf
is the application module, Controllers
is the first value specified in the pattern, Dashboard
the controller and Index
the action.
This pattern MUST always contain "%{controller}"
and %{action}
. This pattern SHOULD be used accordingly to #view_pattern
value.
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.
1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/hanami/application_configuration.rb', line 1133 def controller_pattern(value = nil) if value @controller_pattern = value else @controller_pattern ||= 'Controllers::%{controller}::%{action}' end end |
#cookies(options) ⇒ Object #cookies ⇒ Hanami::Config::Cookies
Configure cookies Enable cookies (disabled by default).
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.
382 383 384 385 386 387 388 |
# File 'lib/hanami/application_configuration.rb', line 382 def ( = nil) if .nil? @cookies ||= Config::Cookies.new(self, ) else @cookies = Config::Cookies.new(self, ) 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 ‘:html`.
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.
783 784 785 786 787 788 789 |
# File 'lib/hanami/application_configuration.rb', line 783 def default_request_format(format = nil) if format @default_request_format = Utils::Kernel.Symbol(format) else @default_request_format || :html 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 ‘:html`.
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.
838 839 840 841 842 843 844 |
# File 'lib/hanami/application_configuration.rb', line 838 def default_response_format(format = nil) if format @default_response_format = Utils::Kernel.Symbol(format) else @default_response_format end end |
#force_ssl(value = nil) ⇒ Boolean
Force ssl redirection if http scheme is set
97 98 99 100 101 102 103 |
# File 'lib/hanami/application_configuration.rb', line 97 def force_ssl(value = nil) if value @force_ssl = value else @force_ssl || false 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.
1339 1340 1341 1342 1343 1344 1345 |
# File 'lib/hanami/application_configuration.rb', line 1339 def handle_exceptions(value = nil) if value.nil? @handle_exceptions else @handle_exceptions = value end end |
#host(value) ⇒ Object #scheme ⇒ String
The URI host for this application. This is used by the router helpers to generate absolute URLs.
By default this value is ‘“localhost”`.
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.
951 952 953 954 955 956 957 |
# File 'lib/hanami/application_configuration.rb', line 951 def host(value = nil) if value @host = value else @host ||= @env.host end end |
#layout(value) ⇒ Object #layout ⇒ Symbol?
A Hanami::Layout for this application
By default it’s ‘nil`.
It accepts a Symbol as layout name. When the application is loaded, it will lookup for the corresponding class.
All the views will use this layout, unless otherwise specified.
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.
230 231 232 233 234 235 236 |
# File 'lib/hanami/application_configuration.rb', line 230 def layout(value = nil) if value @layout = value else @layout end end |
#load_paths ⇒ Hanami::Config::LoadPaths
Application load paths The application will recursively load all the Ruby files under these paths.
By default it’s empty in order to allow developers to decide their own app structure.
511 512 513 |
# File 'lib/hanami/application_configuration.rb', line 511 def load_paths @load_paths ||= Config::LoadPaths.new(root) end |
#middleware ⇒ Object
Application middleware.
Specify middleware that your application will use. This method will return the application’s underlying Middleware stack which you can use to add new middleware for your application to use. By default, the middleware stack will contain only ‘Rack::Static` and `Rack::MethodOverride`. However, if `assets false` was specified # in the configuration block, the default `Rack::Static` will be removed.
686 687 688 |
# File 'lib/hanami/application_configuration.rb', line 686 def middleware @middleware ||= Hanami::Middleware.new(self) end |
#model ⇒ Hanami::Config::FrameworkConfiguration
It lazily collects all the low level settings for Hanami::Model’s configuration and applies them when the application is loaded.
NOTE: This forwards all the configurations to Hanami::Model, without checking them. Before to use this feature, please have a look at the current Hanami::Model version installed.
NOTE: This may override some configurations of your application.
1388 1389 1390 |
# File 'lib/hanami/application_configuration.rb', line 1388 def model @model ||= Config::FrameworkConfiguration.new end |
#port(value) ⇒ Object #scheme ⇒ String
The URI port for this application. This is used by the router helpers to generate absolute URLs.
By default this value is ‘2300`.
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.
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 |
# File 'lib/hanami/application_configuration.rb', line 1003 def port(value = nil) if value @port = Integer(value) else return @port if defined?(@port) return @env.port unless @env.default_port? return DEFAULT_SSL_PORT if force_ssl @env.port end end |
#root(value) ⇒ Object #root ⇒ Pathname
The root of the application
By default it returns the current directory, for this reason, **all the commands must be executed from the top level directory of the project**.
If for some reason, that constraint above cannot be satisfied, please configure the root directory, so that commands can be executed from everywhere.
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/hanami/application_configuration.rb', line 151 def root(value = nil) if value @root = value else Utils::Kernel.Pathname(@root || Dir.pwd).realpath end end |
#routes(blk) ⇒ Object #routes(path) ⇒ Object #routes ⇒ Hanami::Config::Routes
Application routes.
Specify a set of routes for the application, by passing a block, or a relative path where to find the file that describes them.
By default it’s ‘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.
583 584 585 586 587 588 589 |
# File 'lib/hanami/application_configuration.rb', line 583 def routes(path = nil, &blk) if path or block_given? @routes = Config::Routes.new(root, path, &blk) else @routes end end |
#scheme(value) ⇒ Object #scheme ⇒ String
The URI scheme for this application. This is used by the router helpers to generate absolute URLs.
By default this value is ‘“http”`.
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.
889 890 891 892 893 894 895 |
# File 'lib/hanami/application_configuration.rb', line 889 def scheme(value = nil) if value @scheme = value else @scheme ||= 'http' end end |
#security ⇒ Hanami::Config::Security
Returns the security policy
86 87 88 |
# File 'lib/hanami/application_configuration.rb', line 86 def security @security ||= Config::Security.new end |
#sessions(adapter, options) ⇒ Object #sessions(false) ⇒ Object #sessions ⇒ Hanami::Config::Sessions
Configure sessions Enable sessions (disabled by default).
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.
Given Class as adapter it will be used as sessions middleware. Given String as adapter it will be resolved as class name and used as sessions middleware. Given Symbol as adapter it is assumed it’s name of the class under Rack::Session namespace that will be used as sessions middleware (e.g. :cookie for Rack::Session::Cookie).
By default options include domain inferred from host configuration, and secure flag inferred from scheme configuration.
464 465 466 467 468 469 470 |
# File 'lib/hanami/application_configuration.rb', line 464 def sessions(adapter = nil, = {}) if adapter.nil? @sessions ||= Config::Sessions.new else @sessions = Config::Sessions.new(adapter, , self) end end |
#ssl? ⇒ FalseClass, TrueClass
Check if the application uses SSL
904 905 906 |
# File 'lib/hanami/application_configuration.rb', line 904 def ssl? scheme == SSL_SCHEME end |
#templates(value) ⇒ Object #templates ⇒ Pathname
Templates root. The application will recursively look for templates under this path.
By default it’s equal to the application ‘root`.
Otherwise, you can specify a different relative path under ‘root`.
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.
286 287 288 289 290 291 292 |
# File 'lib/hanami/application_configuration.rb', line 286 def templates(value = nil) if value @templates = value else root.join @templates.to_s end end |
#view ⇒ Hanami::Config::FrameworkConfiguration
It lazily collects all the low level settings for Hanami::View’s configuration and applies them when the application is loaded.
NOTE: This forwards all the configurations to Hanami::View, without checking them. Before to use this feature, please have a look at the current Hanami::View version installed.
NOTE: This may override some configurations of your application.
1474 1475 1476 |
# File 'lib/hanami/application_configuration.rb', line 1474 def view @view ||= Config::FrameworkConfiguration.new end |
#view_pattern(value) ⇒ Object #controller_pattern ⇒ String
Defines a relative pattern to find views:.
By default this equals to "Views::%{controller}::%{action}"
That means views must be structured like this: Bookshelf::Views::Dashboard::Index
, where Bookshelf
is the application module, Views
is the first value specified in the pattern, Dashboard
a module corresponding to the controller name and Index
the view, corresponding to the action name.
This pattern MUST always contain "%{controller}"
and %{action}
. This pattern SHOULD be used accordingly to #controller_pattern
value.
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.
1252 1253 1254 1255 1256 1257 1258 |
# File 'lib/hanami/application_configuration.rb', line 1252 def view_pattern(value = nil) if value @view_pattern = value else @view_pattern ||= 'Views::%{controller}::%{action}' end end |