Class: Lotus::Configuration
- Inherits:
-
Object
- Object
- Lotus::Configuration
- Defined in:
- lib/lotus/configuration.rb
Overview
Configuration for a Lotus 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
Instance Method Summary collapse
-
#adapter(options = {}) ⇒ Object
Adapter configuration.
-
#assets ⇒ Lotus::Config::Assets
The application will serve the static assets under these directories.
-
#body_parsers(*parsers) ⇒ Object
Body parsing configuration.
-
#configure(environment = nil, path = nil, &blk) ⇒ self
private
Set a block yield when the configuration will be loaded or set a path for the specific environment.
-
#controller ⇒ Lotus::Config::FrameworkConfiguration
It lazily collects all the low level settings for Lotus::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_format(format = nil) ⇒ Object
Set a format as default fallback for all the requests without a strict requirement for the mime 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 ⇒ Lotus::Configuration
constructor
private
Initialize a new configuration instance.
-
#layout(value = nil) ⇒ Object
A Lotus::Layout for this application.
-
#load!(namespace = nil) ⇒ self
private
Load the configuration.
-
#load_paths ⇒ Lotus::Config::LoadPaths
Application load paths The application will recursively load all the Ruby files under these paths.
-
#mapping(path = nil, &blk) ⇒ Object
Application collection mapping.
-
#middleware ⇒ Object
Application middleware.
-
#model ⇒ Lotus::Config::FrameworkConfiguration
It lazily collects all the low level settings for Lotus::Model’s configuration and applies them when the application is loaded.
-
#namespace(value = nil) ⇒ Object
The application namespace.
-
#path_prefix(value = nil) ⇒ String, NilClass
private
This options is used as a bridge between container and router application.
-
#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 ⇒ Lotus::Config::Security
Returns the security policy.
-
#serve_assets(value = nil) ⇒ Object
Configure serving of assets Enable static assets (disabled by default).
-
#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 ⇒ Lotus::Config::FrameworkConfiguration
It lazily collects all the low level settings for Lotus::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 ⇒ Lotus::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
30 31 32 33 34 |
# File 'lib/lotus/configuration.rb', line 30 def initialize @blk = Proc.new{} @env = Environment.new @configurations = Hash.new { |k, v| k[v] = [] } 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.
940 941 942 943 944 945 946 |
# File 'lib/lotus/configuration.rb', line 940 def adapter( = {}) if !.empty? @adapter = else @adapter end end |
#assets ⇒ Lotus::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 differents relatives paths under ‘root`.
421 422 423 |
# File 'lib/lotus/configuration.rb', line 421 def assets @assets ||= Config::Assets.new(root) 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.
786 787 788 789 790 791 792 |
# File 'lib/lotus/configuration.rb', line 786 def body_parsers(*parsers) if parsers.empty? @body_parsers ||= [] else @body_parsers = parsers end end |
#configure(environment = nil, path = nil, &blk) ⇒ self
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.
Set a block yield when the configuration will be loaded or set a path for the specific environment.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lotus/configuration.rb', line 46 def configure(environment = nil, path = nil, &blk) if environment && path @configurations[environment.to_s] << Config::Configure.new(root, path, &blk) elsif environment @configurations[environment.to_s] << blk else @blk = blk end self end |
#controller ⇒ Lotus::Config::FrameworkConfiguration
It lazily collects all the low level settings for Lotus::Controller’s configuration and applies them when the application is loaded.
NOTE: This forwards all the configurations to Lotus::Controller, without checking them. Before to use this feature, please have a look at the current Lotus::Controller version installed.
NOTE: This may override some configurations of your application.
1598 1599 1600 |
# File 'lib/lotus/configuration.rb', line 1598 def controller @controller ||= Config::FrameworkConfiguration.new end |
#controller_pattern(value) ⇒ Object #controller_pattern ⇒ String
Defines a relative pattern to find controllers.
Lotus supports multiple architectures (aka application structures), this setting helps to understand the namespace where to find applications’ controllers and actions.
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.
1295 1296 1297 1298 1299 1300 1301 |
# File 'lib/lotus/configuration.rb', line 1295 def controller_pattern(value = nil) if value @controller_pattern = value else @controller_pattern ||= 'Controllers::%{controller}::%{action}' end end |
#cookies(options) ⇒ Object #cookies ⇒ Lotus::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.
515 516 517 518 519 520 521 |
# File 'lib/lotus/configuration.rb', line 515 def ( = nil) if .nil? @cookies ||= Config::Cookies.new(self, ) else @cookies = Config::Cookies.new(self, ) 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 ‘: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.
996 997 998 999 1000 1001 1002 |
# File 'lib/lotus/configuration.rb', line 996 def default_format(format = nil) if format @default_format = Utils::Kernel.Symbol(format) else @default_format || :html end end |
#force_ssl(value = nil) ⇒ Boolean
Force ssl redirection if http scheme is set
118 119 120 121 122 123 124 |
# File 'lib/lotus/configuration.rb', line 118 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.
1506 1507 1508 1509 1510 1511 1512 |
# File 'lib/lotus/configuration.rb', line 1506 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.
1109 1110 1111 1112 1113 1114 1115 |
# File 'lib/lotus/configuration.rb', line 1109 def host(value = nil) if value @host = value else @host ||= @env.host end end |
#layout(value) ⇒ Object #layout ⇒ Symbol?
A Lotus::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.
314 315 316 317 318 319 320 |
# File 'lib/lotus/configuration.rb', line 314 def layout(value = nil) if value @layout = value else @layout end end |
#load!(namespace = nil) ⇒ self
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
66 67 68 69 70 71 |
# File 'lib/lotus/configuration.rb', line 66 def load!(namespace = nil) @namespace = namespace evaluate_configurations! self end |
#load_paths ⇒ Lotus::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.
644 645 646 |
# File 'lib/lotus/configuration.rb', line 644 def load_paths @load_paths ||= Config::LoadPaths.new end |
#mapping(blk) ⇒ Object #mapping(path) ⇒ Object #mapping ⇒ Lotus::Config::Mapping
Application collection mapping.
Specify a set of collections 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.
895 896 897 898 899 900 901 |
# File 'lib/lotus/configuration.rb', line 895 def mapping(path = nil, &blk) if path or block_given? @mapping = Config::Mapping.new(root, path, &blk) else @mapping end 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.
819 820 821 |
# File 'lib/lotus/configuration.rb', line 819 def middleware @middleware ||= Lotus::Middleware.new(self) end |
#model ⇒ Lotus::Config::FrameworkConfiguration
It lazily collects all the low level settings for Lotus::Model’s configuration and applies them when the application is loaded.
NOTE: This forwards all the configurations to Lotus::Model, without checking them. Before to use this feature, please have a look at the current Lotus::Model version installed.
NOTE: This may override some configurations of your application.
1555 1556 1557 |
# File 'lib/lotus/configuration.rb', line 1555 def model @model ||= Config::FrameworkConfiguration.new end |
#namespace(value) ⇒ Object #namespace ⇒ Class, Module
The application namespace
By default it returns the Ruby namespace of the application. For instance for an application ‘Bookshelf::Application`, it returns `Bookshelf`.
This value isn’t set at the init time, but when the configuration is loaded with ‘#load!`.
Lotus applications are namespaced: all the controllers and views live under the application module, without polluting the global namespace. However, if for some reason, you want top level classes, set this value to ‘Object` (which is the top level namespace for Ruby).
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.
235 236 237 238 239 240 241 |
# File 'lib/lotus/configuration.rb', line 235 def namespace(value = nil) if value @namespace = value else @namespace end end |
#path_prefix(value = nil) ⇒ String, NilClass
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.
This options is used as a bridge between container and router application.
1651 1652 1653 1654 1655 1656 1657 |
# File 'lib/lotus/configuration.rb', line 1651 def path_prefix(value = nil) if value.nil? @path_prefix else @path_prefix = value end 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.
1161 1162 1163 1164 1165 1166 1167 |
# File 'lib/lotus/configuration.rb', line 1161 def port(value = nil) if value @port = Integer(value) else @port || @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.
172 173 174 175 176 177 178 |
# File 'lib/lotus/configuration.rb', line 172 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 ⇒ Lotus::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.
716 717 718 719 720 721 722 |
# File 'lib/lotus/configuration.rb', line 716 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.
1047 1048 1049 1050 1051 1052 1053 |
# File 'lib/lotus/configuration.rb', line 1047 def scheme(value = nil) if value @scheme = value else @scheme ||= 'http' end end |
#security ⇒ Lotus::Config::Security
Returns the security policy
107 108 109 |
# File 'lib/lotus/configuration.rb', line 107 def security @security ||= Config::Security.new end |
#serve_assets(value) ⇒ Object #serve_assets ⇒ TrueClass, FalseClass
Configure serving of assets Enable static assets (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.
468 469 470 471 472 473 474 |
# File 'lib/lotus/configuration.rb', line 468 def serve_assets(value = nil) if value.nil? @serve_assets || false else @serve_assets = value end end |
#sessions(adapter, options) ⇒ Object #sessions(false) ⇒ Object #sessions ⇒ Lotus::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.
597 598 599 600 601 602 603 |
# File 'lib/lotus/configuration.rb', line 597 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
1062 1063 1064 |
# File 'lib/lotus/configuration.rb', line 1062 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.
370 371 372 373 374 375 376 |
# File 'lib/lotus/configuration.rb', line 370 def templates(value = nil) if value @templates = value else root.join @templates.to_s end end |
#view ⇒ Lotus::Config::FrameworkConfiguration
It lazily collects all the low level settings for Lotus::View’s configuration and applies them when the application is loaded.
NOTE: This forwards all the configurations to Lotus::View, without checking them. Before to use this feature, please have a look at the current Lotus::View version installed.
NOTE: This may override some configurations of your application.
1641 1642 1643 |
# File 'lib/lotus/configuration.rb', line 1641 def view @view ||= Config::FrameworkConfiguration.new end |
#view_pattern(value) ⇒ Object #controller_pattern ⇒ String
Defines a relative pattern to find views:.
Lotus supports multiple architectures (aka application structures), this setting helps to understand the namespace where to find applications’ 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.
1419 1420 1421 1422 1423 1424 1425 |
# File 'lib/lotus/configuration.rb', line 1419 def view_pattern(value = nil) if value @view_pattern = value else @view_pattern ||= 'Views::%{controller}::%{action}' end end |