Class: Hanami::Config
- Inherits:
-
Object
- Object
- Hanami::Config
- Includes:
- Dry::Configurable
- Defined in:
- lib/hanami/config.rb,
lib/hanami/config/views.rb,
lib/hanami/config/logger.rb,
lib/hanami/config/router.rb,
lib/hanami/config/actions.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, 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 ⇒ Object
readonly
private
Returns the app’s assets config.
-
#base_url ⇒ URI
Sets the base URL for app’s web server.
-
#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.
-
#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 ⇒ Object
readonly
private
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
A new instance of Config.
-
#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.
Returns a new instance of Config.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/hanami/config.rb', line 214 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 load_from_env @logger = Config::Logger.new(env: env, app_name: app_name) # TODO: Make assets config dependent require "hanami/assets/app_config" @assets = Hanami::Assets::AppConfig.new @actions = load_dependent_config("hanami-controller") { require_relative "config/actions" Actions.new } @router = load_dependent_config("hanami-router") { require_relative "config/router" @middleware = Slice::Routing::Middleware::Stack.new 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)
405 406 407 408 409 410 411 |
# File 'lib/hanami/config.rb', line 405 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.
165 166 167 |
# File 'lib/hanami/config.rb', line 165 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.
138 139 140 |
# File 'lib/hanami/config.rb', line 138 def app_name @app_name end |
#assets ⇒ 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.
Returns the app’s assets config.
This is NOT RELEASED as of 2.0.0.
211 212 213 |
# File 'lib/hanami/config.rb', line 211 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.
128 |
# File 'lib/hanami/config.rb', line 128 setting :base_url, default: "http://0.0.0.0:2300", constructor: ->(url) { URI(url) } |
#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.
151 152 153 |
# File 'lib/hanami/config.rb', line 151 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.
45 |
# File 'lib/hanami/config.rb', line 45 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.
179 180 181 |
# File 'lib/hanami/config.rb', line 179 def middleware @middleware end |
#no_auto_register_paths ⇒ Array<String>
Sets the paths to skip from container auto-registration.
Defaults to ‘[“entities”]`.
113 |
# File 'lib/hanami/config.rb', line 113 setting :no_auto_register_paths, default: %w[entities] |
#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`.
29 |
# File 'lib/hanami/config.rb', line 29 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.
197 198 199 |
# File 'lib/hanami/config.rb', line 197 def router @router end |
#settings_store ⇒ #fetch
Sets the store used to retrieve Settings values.
Defaults to an instance of Settings::EnvStore.
59 |
# File 'lib/hanami/config.rb', line 59 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.
95 96 97 98 99 100 101 102 |
# File 'lib/hanami/config.rb', line 95 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.
80 |
# File 'lib/hanami/config.rb', line 80 setting :slices |
#views ⇒ 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.
Returns the app’s views config, or a null config if hanami-view is not bundled.
This is NOT RELEASED as of 2.0.0.
204 205 206 |
# File 'lib/hanami/config.rb', line 204 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.
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/hanami/config.rb', line 269 def finalize! # Finalize nested configs assets.finalize! actions.finalize! 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.
298 299 300 |
# File 'lib/hanami/config.rb', line 298 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.
319 320 321 |
# File 'lib/hanami/config.rb', line 319 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
331 332 333 |
# File 'lib/hanami/config.rb', line 331 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.
366 367 368 |
# File 'lib/hanami/config.rb', line 366 def logger_instance @logger_instance || logger.instance end |