Class: Hanami::Config
- Inherits:
-
Object
- Object
- Hanami::Config
- Includes:
- Dry::Configurable
- Defined in:
- lib/hanami/config.rb,
lib/hanami/config/db.rb,
lib/hanami/config/views.rb,
lib/hanami/config/assets.rb,
lib/hanami/config/logger.rb,
lib/hanami/config/router.rb,
lib/hanami/config/actions.rb,
lib/hanami/config/console.rb,
lib/hanami/config/null_config.rb,
lib/hanami/config/actions/cookies.rb,
lib/hanami/config/actions/sessions.rb,
lib/hanami/config/actions/content_security_policy.rb
Overview
Hanami app config
Defined Under Namespace
Classes: Actions, Assets, Console, DB, Logger, NullConfig, Router, Views
Instance Attribute Summary collapse
-
#actions ⇒ Hanami::Config::Actions, Hanami::Config::NullConfig
readonly
Returns the app’s actions config, or a null config if hanami-controller is not bundled.
-
#app_name ⇒ Hanami::SliceName
readonly
private
Returns the app or slice’s slice_name.
-
#assets ⇒ Hanami::Config::Assets, Hanami::Config::NullConfig
readonly
Returns the app’s views config, or a null config if hanami-view is not bundled.
-
#base_url ⇒ URI
Sets the base URL for app’s web server.
-
#console ⇒ Hanami::Config::Console
Returns the app’s console config.
-
#db ⇒ Hanami::Config::DB, Hanami::Config::NullConfig
readonly
Returns the app’s db config, or a null config if hanami-db is not bundled.
-
#env ⇒ Symbol
readonly
private
Returns the app’s environment.
-
#inflector ⇒ Dry::Inflector
Sets the app’s inflector.
-
#middleware ⇒ Hanami::Slice::Routing::Middleware::Stack?
(also: #middleware_stack)
readonly
Returns the app’s middleware stack, or nil if hanami-router is not bundled.
-
#no_auto_register_paths ⇒ Array<String>
Sets the paths to skip from container auto-registration.
-
#render_detailed_errors ⇒ Boolean
Sets whether to catch exceptions and render detailed, interactive error pages.
-
#render_error_responses ⇒ Hash{String => Symbol}
Sets a mapping of exception class names (as strings) to symbolic response status codes used for rendering error responses.
-
#render_errors ⇒ Boolean
Sets whether to catch exceptions and render error pages.
-
#root ⇒ Pathname
Sets the root for the app or slice.
-
#router ⇒ Hanami::Config::Router, Hanami::Config::NullConfig
readonly
Returns the app’s router config, or a null config if hanami-router is not bundled.
-
#settings_store ⇒ #fetch
Sets the store used to retrieve Settings values.
-
#shared_app_component_keys ⇒ Array<String>
Sets the keys for the components to be imported from the app into all other slices.
-
#slices ⇒ Array<String>?
Sets the slices to load when the app is preared or booted.
-
#views ⇒ Hanami::Config::Views, Hanami::Config::NullConfig
readonly
Returns the app’s views config, or a null config if hanami-view is not bundled.
Instance Method Summary collapse
-
#finalize! ⇒ Object
private
Finalizes the config.
-
#inflections(&block) ⇒ Dry::Inflector
Configures the app’s custom inflections.
-
#initialize(app_name:, env:) {|_self| ... } ⇒ Config
constructor
private
rubocop:disable Metrics/AbcSize.
-
#logger ⇒ Hanami::Config::Logger
Returns the logger config.
-
#logger=(logger_instance) ⇒ Object
Sets the app’s logger instance.
-
#logger_instance ⇒ Dry::Logger::Dispatcher
Returns the configured logger instance.
Constructor Details
#initialize(app_name:, env:) {|_self| ... } ⇒ Config
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.
rubocop:disable Metrics/AbcSize
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/hanami/config.rb', line 313 def initialize(app_name:, env:) @app_name = app_name @env = env # Apply default values that are only knowable at initialize-time (vs require-time) self.root = Dir.pwd self.render_errors = (env == :production) self.render_detailed_errors = (env == :development) load_from_env @actions = load_dependent_config("hanami-controller") { require_relative "config/actions" Actions.new } @assets = load_dependent_config("hanami-assets") { require_relative "config/assets" Hanami::Config::Assets.new } @db = load_dependent_config("hanami-db") { DB.new } @logger = Config::Logger.new(env: env, app_name: app_name) @middleware = load_dependent_config("hanami-router") { require_relative "slice/routing/middleware/stack" Slice::Routing::Middleware::Stack.new } @router = load_dependent_config("hanami-router") { require_relative "config/router" Router.new(self) } @views = load_dependent_config("hanami-view") { require_relative "config/views" Views.new } yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
508 509 510 511 512 513 514 |
# File 'lib/hanami/config.rb', line 508 def method_missing(name, *args, &block) if config.respond_to?(name) config.public_send(name, *args, &block) else super end end |
Instance Attribute Details
#actions ⇒ Hanami::Config::Actions, Hanami::Config::NullConfig (readonly)
Returns the app’s actions config, or a null config if hanami-controller is not bundled.
235 236 237 |
# File 'lib/hanami/config.rb', line 235 def actions @actions end |
#app_name ⇒ Hanami::SliceName (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.
Returns the app or slice’s slice_name.
This is useful for default config values that depend on this name.
208 209 210 |
# File 'lib/hanami/config.rb', line 208 def app_name @app_name end |
#assets ⇒ Hanami::Config::Assets, Hanami::Config::NullConfig (readonly)
Returns the app’s views config, or a null config if hanami-view is not bundled.
309 310 311 |
# File 'lib/hanami/config.rb', line 309 def assets @assets end |
#base_url ⇒ URI
Sets the base URL for app’s web server.
This is passed to the router and used for generating links.
Defaults to ‘“0.0.0.0:2300”`. String values passed are turned into `URI` instances.
134 |
# File 'lib/hanami/config.rb', line 134 setting :base_url, default: "http://0.0.0.0:2300", constructor: ->(url) { URI(url) } |
#console ⇒ Hanami::Config::Console
Returns the app’s console config
198 |
# File 'lib/hanami/config.rb', line 198 setting :console, default: Hanami::Config::Console.new |
#db ⇒ Hanami::Config::DB, Hanami::Config::NullConfig (readonly)
Returns the app’s db config, or a null config if hanami-db is not bundled.
249 250 251 |
# File 'lib/hanami/config.rb', line 249 def db @db end |
#env ⇒ Symbol (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.
Returns the app’s environment.
221 222 223 |
# File 'lib/hanami/config.rb', line 221 def env @env end |
#inflector ⇒ Dry::Inflector
Sets the app’s inflector.
This expects a ‘Dry::Inflector` (or compatible) inflector instance.
To configure custom inflection rules without having to assign a whole inflector, see #inflections.
46 |
# File 'lib/hanami/config.rb', line 46 setting :inflector, default: Dry::Inflector.new |
#middleware ⇒ Hanami::Slice::Routing::Middleware::Stack? (readonly) Also known as: middleware_stack
Returns the app’s middleware stack, or nil if hanami-router is not bundled.
Use this to configure middleware that should apply to all routes.
263 264 265 |
# File 'lib/hanami/config.rb', line 263 def middleware @middleware end |
#no_auto_register_paths ⇒ Array<String>
Sets the paths to skip from container auto-registration.
Defaults to ‘[“entities”]`.
114 115 116 117 118 119 |
# File 'lib/hanami/config.rb', line 114 setting :no_auto_register_paths, default: [ "db", "entities", "relations", "structs" ] |
#render_detailed_errors ⇒ Boolean
Sets whether to catch exceptions and render detailed, interactive error pages.
Requires the hanami-webconsole gem to be available.
Defaults to ‘false` in production mode, `true` in all others.
160 |
# File 'lib/hanami/config.rb', line 160 setting :render_detailed_errors, default: false |
#render_error_responses ⇒ Hash{String => Symbol}
Sets a mapping of exception class names (as strings) to symbolic response status codes used for rendering error responses.
The response status codes will be passed to ‘Rack::Utils.status_code`.
In ordinary usage, you should not replace this hash. Instead, add keys and values for the errors you want handled.
183 184 185 186 |
# File 'lib/hanami/config.rb', line 183 setting :render_error_responses, default: Hash.new(:internal_server_error).merge!( "Hanami::Router::NotAllowedError" => :not_found, "Hanami::Router::NotFoundError" => :not_found, ) |
#render_errors ⇒ Boolean
Sets whether to catch exceptions and render error pages.
For HTML responses, these error pages are in ‘public/404,500.html`.
Defaults to ‘true` in production mode, `false` in all others.
147 |
# File 'lib/hanami/config.rb', line 147 setting :render_errors, default: false |
#root ⇒ Pathname
Sets the root for the app or slice.
For the app, this defaults to ‘Dir.pwd`. For slices detected in `slices/` `config/slices/`, this defaults to `slices//`.
Accepts a string path and will return a ‘Pathname`.
30 |
# File 'lib/hanami/config.rb', line 30 setting :root, constructor: ->(path) { Pathname(path) if path } |
#router ⇒ Hanami::Config::Router, Hanami::Config::NullConfig (readonly)
Returns the app’s router config, or a null config if hanami-router is not bundled.
281 282 283 |
# File 'lib/hanami/config.rb', line 281 def router @router end |
#settings_store ⇒ #fetch
Sets the store used to retrieve Settings values.
Defaults to an instance of Settings::EnvStore.
60 |
# File 'lib/hanami/config.rb', line 60 setting :settings_store, default: Hanami::Settings::EnvStore.new |
#shared_app_component_keys ⇒ Array<String>
Sets the keys for the components to be imported from the app into all other slices.
You should append items to this array, since the default shared components are essential for slices to operate within the app.
96 97 98 99 100 101 102 103 |
# File 'lib/hanami/config.rb', line 96 setting :shared_app_component_keys, default: %w[ inflector logger notifications rack.monitor routes settings ] |
#slices ⇒ Array<String>?
Sets the slices to load when the app is preared or booted.
Defaults to ‘nil`, which will load all slices. Set this to an array of slice names to load only those slices.
This attribute is also populated from the ‘HANAMI_SLICES` environment variable.
81 |
# File 'lib/hanami/config.rb', line 81 setting :slices |
#views ⇒ Hanami::Config::Views, Hanami::Config::NullConfig (readonly)
Returns the app’s views config, or a null config if hanami-view is not bundled.
295 296 297 |
# File 'lib/hanami/config.rb', line 295 def views @views end |
Instance Method Details
#finalize! ⇒ 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.
Finalizes the config.
This is called when the app or slice is prepared. After this, no further changes to config can be made.
380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/hanami/config.rb', line 380 def finalize! # Finalize nested configs assets.finalize! actions.finalize!(self) views.finalize! logger.finalize! router.finalize! use_body_parser_middleware super end |
#inflections(&block) ⇒ Dry::Inflector
Configures the app’s custom inflections.
You should call this one time only. Subsequent calls will override previously configured inflections.
409 410 411 |
# File 'lib/hanami/config.rb', line 409 def inflections(&block) self.inflector = Dry::Inflector.new(&block) end |
#logger ⇒ Hanami::Config::Logger
Returns the logger config.
Use this to configure various options for the default ‘Dry::Logger::Dispatcher` logger instance.
430 431 432 |
# File 'lib/hanami/config.rb', line 430 def logger @logger end |
#logger=(logger_instance) ⇒ Object
Sets the app’s logger instance.
This entirely replaces the default ‘Dry::Logger::Dispatcher` instance that would have been
442 443 444 |
# File 'lib/hanami/config.rb', line 442 def logger=(logger_instance) @logger_instance = logger_instance end |
#logger_instance ⇒ Dry::Logger::Dispatcher
Returns the configured logger instance.
Unless you’ve replaced the logger with #logger=, this returns a ‘Dry::Logger::Dispatcher` configured with the options configured through #logger.
This configured logger is registered in all app and slice containers as ‘“logger”`. For typical usage, you should access the logger via this component, not directly from config.
477 478 479 |
# File 'lib/hanami/config.rb', line 477 def logger_instance @logger_instance || logger.instance end |