Class: Hanami::Config Private

Inherits:
Object
  • Object
show all
Includes:
Dry::Configurable
Defined in:
lib/hanami/config.rb,
lib/hanami/config/db.rb,
lib/hanami/config/i18n.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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Hanami app config

Since:

  • 2.0.0

Defined Under Namespace

Classes: Actions, Assets, Console, DB, I18n, Logger, NullConfig, Router, Views

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Yields:

  • (_self)

Yield Parameters:

Since:

  • 2.0.0



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/hanami/config.rb', line 359

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-action") {
    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 }

  @i18n = load_dependent_config("i18n") {
    require_relative "config/i18n"
    I18n.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)

Since:

  • 2.0.0



552
553
554
555
556
557
558
# File 'lib/hanami/config.rb', line 552

def method_missing(name, *args, &block)
  if config.respond_to?(name)
    config.public_send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#actionsHanami::Config::Actions, Hanami::Config::NullConfig (readonly)

Returns the app’s actions config, or a null config if hanami-action is not bundled.

Examples:

When hanami-action is bundled

config.actions.default_request_format # => :html

When hanami-action is not bundled

config.actions.default_request_format # => NoMethodError

Returns:

Since:

  • 2.0.0



267
268
269
# File 'lib/hanami/config.rb', line 267

def actions
  @actions
end

#app_nameHanami::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.

Returns:

Since:

  • 2.0.0



240
241
242
# File 'lib/hanami/config.rb', line 240

def app_name
  @app_name
end

#assetsHanami::Config::Assets, Hanami::Config::NullConfig (readonly)

Returns the app’s views config, or a null config if hanami-view is not bundled.

Examples:

When hanami-view is bundled

config.views.paths # => [...]

When hanami-view is not bundled

config.views.paths # => NoMethodError

Returns:

Since:

  • 2.1.0



355
356
357
# File 'lib/hanami/config.rb', line 355

def assets
  @assets
end

#base_urlURI

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.

Returns:

  • (URI)

See Also:

Since:

  • 2.0.0



166
# File 'lib/hanami/config.rb', line 166

setting :base_url, default: "http://0.0.0.0:2300", constructor: ->(url) { URI(url) }

#consoleHanami::Config::Console

Returns the app’s console config

Examples:

config.console.engine # => :irb

Returns:

Since:

  • 2.3.0



230
# File 'lib/hanami/config.rb', line 230

setting :console, default: Hanami::Config::Console.new

#dbHanami::Config::DB, Hanami::Config::NullConfig (readonly)

Returns the app’s db config, or a null config if hanami-db is not bundled.

Examples:

When hanami-db is bundled

config.db.import_from_parent # => false

When hanami-db is not bundle

config.db.import_from_parent # => NoMethodError

Returns:

Since:

  • 2.2.0



281
282
283
# File 'lib/hanami/config.rb', line 281

def db
  @db
end

#envSymbol (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.

Examples:

config.env # => :development

Returns:

  • (Symbol)

See Also:

  • #environment

Since:

  • 2.0.0



253
254
255
# File 'lib/hanami/config.rb', line 253

def env
  @env
end

#i18nHanami::Config::I18n, Hanami::Config::NullConfig (readonly)

Returns the app’s i18n config, or a null config if i18n is not bundled.

Examples:

When i18n is bundled

config.i18n.default_locale # => :en

When i18n is not bundled

config.i18n.default_locale # => NoMethodError

Returns:

Since:

  • 2.2.0



295
296
297
# File 'lib/hanami/config.rb', line 295

def i18n
  @i18n
end

#inflectorDry::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.

Returns:

  • (Dry::Inflector)

See Also:

Since:

  • 2.0.0



46
# File 'lib/hanami/config.rb', line 46

setting :inflector, default: Dry::Inflector.new

#middlewareHanami::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.

Examples:

config.middleware.use :body_parser, :json
config.middleware.use MyCustomMiddleware

Returns:

Since:

  • 2.0.0



309
310
311
# File 'lib/hanami/config.rb', line 309

def middleware
  @middleware
end

#no_auto_register_pathsArray<String>

Sets the paths to skip from container auto-registration.

Paths are relative to ‘/app` or a slice root.

Defaults to ‘[“db”, “entities”, “relations”, “structs”]`.

Returns:

  • (Array<String>)

    array of relative paths

Since:

  • 2.0.0



116
117
118
119
120
121
# File 'lib/hanami/config.rb', line 116

setting :no_auto_register_paths, default: [
  "db",
  "entities",
  "relations",
  "structs"
]

#no_memoizeArray<String>, Proc

Sets the components that should not be memoized in the container.

All auto-registered components are memoized by default, meaning each component is resolved only once, with the same instance returned on every subsequent resolution.

Use this setting to opt specific components out of memoization. It accepts an array of key prefixes (strings) for the simple case, or a proc for full control. The proc receives a ‘Dry::System::Component` and should return `true` for components that should not be memoized.

Individual components can also opt out by adding a ‘# memoize: false` magic comment at the top of their source file.

Defaults to ‘[]` (all components memoized).

Examples:

Opt out by key prefix

config.no_memoize = ["workers", "jobs"]

Opt out with a proc

config.no_memoize = ->(component) {
  component.key.start_with?("workers")
}

Returns:

  • (Array<String>, Proc)

Since:

  • 2.3.0



151
# File 'lib/hanami/config.rb', line 151

setting :no_memoize, default: []

#render_detailed_errorsBoolean

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.

Returns:

  • (Boolean)

Since:

  • 2.1.0



192
# File 'lib/hanami/config.rb', line 192

setting :render_detailed_errors, default: false

#render_error_responsesHash{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.

Examples:

config.render_error_responses
# => {"Hanami::Router::NotFoundError" => :not_found}

config.render_error_responses["MyNotFoundError"] = :not_found

Returns:

  • (Hash{String => Symbol})

See Also:

Since:

  • 2.1.0



215
216
217
218
# File 'lib/hanami/config.rb', line 215

setting :render_error_responses, default: Hash.new(:internal_server_error).merge!(
  "Hanami::Router::NotAllowedError" => :not_found,
  "Hanami::Router::NotFoundError" => :not_found
)

#render_errorsBoolean

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.

Returns:

  • (Boolean)

Since:

  • 2.1.0



179
# File 'lib/hanami/config.rb', line 179

setting :render_errors, default: false

#rootPathname

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`.

Returns:

  • (Pathname)

Since:

  • 2.0.0



30
# File 'lib/hanami/config.rb', line 30

setting :root, constructor: ->(path) { Pathname(path) if path }

#routerHanami::Config::Router, Hanami::Config::NullConfig (readonly)

Returns the app’s router config, or a null config if hanami-router is not bundled.

Examples:

When hanami-router is bundled

config.router.resolver # => Hanami::Slice::Routing::Resolver

When hanami-router is not bundled

config.router.resolver # => NoMethodError

Returns:

Since:

  • 2.0.0



327
328
329
# File 'lib/hanami/config.rb', line 327

def router
  @router
end

#settings_store#fetch

Sets the store used to retrieve Settings values.

Defaults to an instance of Settings::EnvStore.

Returns:

  • (#fetch)

See Also:

Since:

  • 2.0.0



60
# File 'lib/hanami/config.rb', line 60

setting :settings_store, default: Hanami::Settings::EnvStore.new

#shared_app_component_keysArray<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.

Examples:

config.shared_app_component_keys += ["shared_component_a", "shared_component_b"]

Returns:

  • (Array<String>)

Since:

  • 2.0.0



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
]

#slicesArray<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.

Examples:

config.slices = ["admin", "search"]
ENV["HANAMI_SLICES"] # => "admin,search"
config.slices # => ["admin", "search"]

Returns:

  • (Array<String>, nil)

Since:

  • 2.0.0



81
# File 'lib/hanami/config.rb', line 81

setting :slices

#viewsHanami::Config::Views, Hanami::Config::NullConfig (readonly)

Returns the app’s views config, or a null config if hanami-view is not bundled.

Examples:

When hanami-view is bundled

config.views.paths # => [...]

When hanami-view is not bundled

config.views.paths # => NoMethodError

Returns:

Since:

  • 2.1.0



341
342
343
# File 'lib/hanami/config.rb', line 341

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.

Since:

  • 2.0.0



432
433
434
435
436
437
438
439
440
441
# File 'lib/hanami/config.rb', line 432

def finalize!
  # Finalize nested configs
  assets.finalize!
  actions.finalize!(self)
  views.finalize!
  logger.finalize!
  router.finalize!

  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.

Examples:

config.inflections do |inflections|
  inflections.acronym "WNBA"
end

Returns:

  • (Dry::Inflector)

    the configured inflector

See Also:

Since:

  • 2.0.0



459
460
461
# File 'lib/hanami/config.rb', line 459

def inflections(&block)
  self.inflector = Dry::Inflector.new(&block)
end

#loggerHanami::Config::Logger

Returns the logger config.

Use this to configure various options for the default ‘Dry::Logger::Dispatcher` logger instance.

Examples:

config.logger.level = :debug

Returns:

See Also:

Since:

  • 2.0.0



480
481
482
# File 'lib/hanami/config.rb', line 480

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 configured via #logger.

The provided logger will be wrapped with UniversalLogger if it doesn’t support both keyword arguments and ‘#tagged` logging, ensuring compatibility with Hanami’s logging interface.

See Also:

Since:

  • 2.0.0



498
499
500
# File 'lib/hanami/config.rb', line 498

def logger=(logger_instance)
  @logger_instance = Hanami::UniversalLogger[logger_instance]
end

#logger_instanceDry::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.

Examples:

Accessing the logger component

Hanami.app["logger"] # => #<Dry::Logger::Dispatcher>

Injecting the logger as a dependency

module MyApp
  class MyClass
    include Deps["logger"]

    def my_method
      logger.info("hello")
    end
  end
end

Returns:

  • (Dry::Logger::Dispatcher)

See Also:

Since:

  • 2.0.0



533
534
535
# File 'lib/hanami/config.rb', line 533

def logger_instance
  @logger_instance || logger.instance
end