Module: Hanami

Defined in:
lib/hanami.rb,
lib/hanami/app.rb,
lib/hanami/env.rb,
lib/hanami/port.rb,
lib/hanami/slice.rb,
lib/hanami/config.rb,
lib/hanami/errors.rb,
lib/hanami/routes.rb,
lib/hanami/version.rb,
lib/hanami/settings.rb,
lib/hanami/config/db.rb,
lib/hanami/constants.rb,
lib/hanami/slice_name.rb,
lib/hanami/config/i18n.rb,
lib/hanami/web/welcome.rb,
lib/hanami/config/views.rb,
lib/hanami/providers/db.rb,
lib/hanami/slice/router.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/providers/i18n.rb,
lib/hanami/providers/rack.rb,
lib/hanami/extensions/view.rb,
lib/hanami/provider/source.rb,
lib/hanami/slice_registrar.rb,
lib/hanami/web/rack_logger.rb,
lib/hanami/providers/assets.rb,
lib/hanami/providers/logger.rb,
lib/hanami/providers/routes.rb,
lib/hanami/universal_logger.rb,
lib/hanami/extensions/action.rb,
lib/hanami/extensions/mailer.rb,
lib/hanami/logger/sql_logger.rb,
lib/hanami/middleware/assets.rb,
lib/hanami/providers/mailers.rb,
lib/hanami/config/null_config.rb,
lib/hanami/extensions/db/repo.rb,
lib/hanami/provider_registrar.rb,
lib/hanami/settings/env_store.rb,
lib/hanami/slice_configurable.rb,
lib/hanami/helpers/form_helper.rb,
lib/hanami/helpers/i18n_helper.rb,
lib/hanami/providers/db/config.rb,
lib/hanami/providers/inflector.rb,
lib/hanami/providers/relations.rb,
lib/hanami/slice/routes_helper.rb,
lib/hanami/extensions/operation.rb,
lib/hanami/extensions/view/part.rb,
lib/hanami/logger/sql_formatter.rb,
lib/hanami/providers/db/adapter.rb,
lib/hanami/providers/db/gateway.rb,
lib/hanami/providers/db_logging.rb,
lib/hanami/extensions/view/scope.rb,
lib/hanami/helpers/assets_helper.rb,
lib/hanami/logger/rack_formatter.rb,
lib/hanami/providers/db/adapters.rb,
lib/hanami/config/actions/cookies.rb,
lib/hanami/providers/i18n/backend.rb,
lib/hanami/slice/routing/resolver.rb,
lib/hanami/config/actions/sessions.rb,
lib/hanami/extensions/view/context.rb,
lib/hanami/extensions/router/errors.rb,
lib/hanami/middleware/render_errors.rb,
lib/hanami/providers/db/sql_adapter.rb,
lib/hanami/settings/composite_store.rb,
lib/hanami/slice/view_name_inferrer.rb,
lib/hanami/helpers/form_helper/values.rb,
lib/hanami/middleware/public_errors_app.rb,
lib/hanami/slice/routing/middleware/stack.rb,
lib/hanami/extensions/view/standard_helpers.rb,
lib/hanami/helpers/form_helper/form_builder.rb,
lib/hanami/extensions/view/slice_configured_part.rb,
lib/hanami/extensions/view/slice_configured_view.rb,
lib/hanami/config/actions/content_security_policy.rb,
lib/hanami/extensions/view/slice_configured_context.rb,
lib/hanami/extensions/view/slice_configured_helpers.rb,
lib/hanami/middleware/content_security_policy_nonce.rb,
lib/hanami/extensions/action/slice_configured_action.rb,
lib/hanami/extensions/mailer/slice_configured_mailer.rb,
lib/hanami/extensions/operation/slice_configured_db_operation.rb

Overview

rubocop:disable Lint/RescueException

Defined Under Namespace

Modules: Env, Extensions, Helpers, Logger, Middleware, Port, Provider, Providers, SliceConfigurable, Version, Web Classes: App, Config, ProviderRegistrar, Router, Routes, Settings, Slice, SliceName, SliceRegistrar, UniversalLogger

Constant Summary collapse

Error =

Base class for all Hanami errors.

Since:

  • 2.0.0

Class.new(StandardError)
AppLoadError =

Error raised when App fails to load.

Since:

  • 2.0.0

Class.new(Error)
SliceLoadError =

Error raised when an Slice fails to load.

Since:

  • 2.0.0

Class.new(Error)
ComponentLoadError =

Error raised when an individual component fails to load.

Since:

  • 2.0.0

Class.new(Error)
NoRoutesDefinedError =

Error raised when there are no routes defined.

Since:

  • 2.4.0

Class.new(Error)
UnsupportedMiddlewareSpecError =

Error raised when unsupported middleware configuration is given.

See Also:

Since:

  • 2.0.0

Class.new(Error)
VERSION =

Defines the full version

Since:

  • 0.1.0

Version.version
CONTENT_SECURITY_POLICY_NONCE_REQUEST_KEY =

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.

Since:

  • 0.1.0

"hanami.content_security_policy_nonce"

Class Method Summary collapse

Class Method Details

.appHanami::App

Returns the Hamami app class.

To ensure your Hanami app is loaded, run setup (or ‘require “hanami/setup”`) first.

Returns:

Raises:

See Also:

Since:

  • 2.0.0



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hanami.rb', line 100

def self.app
  @_mutex.synchronize do
    unless defined?(@_app)
      raise AppLoadError,
        "Hanami.app is not yet configured. " \
        "You may need to `require \"hanami/setup\"` to load your config/app.rb file."
    end

    @_app
  end
end

.app=(klass) ⇒ 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.

Since:

  • 2.0.0



124
125
126
127
128
129
130
131
132
# File 'lib/hanami.rb', line 124

def self.app=(klass)
  @_mutex.synchronize do
    if instance_variable_defined?(:@_app)
      raise AppLoadError, "Hanami.app is already configured."
    end

    @_app = klass unless klass.name.nil?
  end
end

.app?Boolean

Returns true if the Hanami app class has been loaded.

Returns:

  • (Boolean)

Since:

  • 2.0.0



118
119
120
# File 'lib/hanami.rb', line 118

def self.app?
  instance_variable_defined?(:@_app)
end

.app_path(dir = Dir.pwd) ⇒ Pathname?

Finds and returns the absolute path for the Hanami app file (‘config/app.rb`).

Searches within the given directory, then searches upwards through parent directories until the app file can be found.

Parameters:

  • dir (String, Pathname) (defaults to: Dir.pwd)

    The directory from which to start searching. Defaults to the current directory.

Returns:

  • (Pathname, nil)

    the app file path, or nil if not found.

Since:

  • 2.0.0



146
147
148
149
150
151
152
153
154
155
# File 'lib/hanami.rb', line 146

def self.app_path(dir = Dir.pwd)
  dir = Pathname(dir).expand_path
  path = dir.join(APP_PATH)

  if path.file?
    path
  elsif !dir.root?
    app_path(dir.parent)
  end
end

.bootObject

Boots the Hanami app.

See Also:

  • App::ClassMethods#boot

Since:

  • 2.0.0



237
238
239
# File 'lib/hanami.rb', line 237

def self.boot
  app.boot
end

.bundled?(gem_name) ⇒ Boolean

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:

  • (Boolean)

Since:

  • 2.0.0



253
254
255
256
257
258
259
260
261
# File 'lib/hanami.rb', line 253

def self.bundled?(gem_name)
  @_mutex.synchronize do
    @_bundled[gem_name] ||= begin
      gem(gem_name)
    rescue Gem::LoadError
      false
    end
  end
end

.bundler_groupsObject

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 an array of bundler group names to be eagerly loaded by hanami-cli and other CLI extensions.

Since:

  • 2.0.0



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

def self.bundler_groups
  [:plugins]
end

.env(e: ENV) ⇒ Symbol

Returns the Hanami app environment as determined from the environment.

Checks the following environment variables in order:

  • ‘HANAMI_ENV`

  • ‘APP_ENV`

  • ‘RACK_ENV`

Defaults to ‘:development` if no environment variable is set.

Examples:

Hanami.env # => :development

Returns:

  • (Symbol)

    the environment name

Since:

  • 2.0.0



174
175
176
# File 'lib/hanami.rb', line 174

def self.env(e: ENV)
  (e["HANAMI_ENV"] || e["APP_ENV"] || e["RACK_ENV"] || :development).to_sym
end

.env?(*names) ⇒ Boolean

Returns true if env matches any of the given names

Examples:

Hanami.env # => :development
Hanami.env?(:development, :test) # => true

Parameters:

  • names (Array<Symbol>)

    the environment names to check

Returns:

  • (Boolean)

Since:

  • 2.0.0



190
191
192
# File 'lib/hanami.rb', line 190

def self.env?(*names)
  names.map(&:to_sym).include?(env)
end

.loaderObject

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.

Since:

  • 2.0.0



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hanami.rb', line 18

def self.loader
  @loader ||= Zeitwerk::Loader.for_gem.tap do |loader|
    loader.inflector.inflect "db" => "DB"
    loader.inflector.inflect "db_logging" => "DBLogging"
    loader.inflector.inflect "slice_configured_db_operation" => "SliceConfiguredDBOperation"
    loader.inflector.inflect "sql_adapter" => "SQLAdapter"
    loader.inflector.inflect "sql_logger" => "SQLLogger"
    loader.inflector.inflect "sql_formatter" => "SQLFormatter"

    gem_lib = loader.dirs.first
    loader.ignore(
      "#{gem_lib}/hanami/{constants,boot,errors,extensions/router/errors,prepare,rake_tasks,setup}.rb",
      # Ignore conditionally-loaded classes dependent on gems that may not be included in the
      # user's Gemfile
      "#{gem_lib}/hanami/config/{assets,router,views}.rb",
      "#{gem_lib}/hanami/slice/router.rb",
      "#{gem_lib}/hanami/slice/routing/resolver.rb",
      "#{gem_lib}/hanami/slice/routing/middleware/stack.rb",
      "#{gem_lib}/hanami/extensions/**/*"
    )

    unless Hanami.bundled?("hanami-router")
      loader.ignore("#{gem_lib}/hanami/routes.rb")
    end
  end
end

.loggerDry::Logger::Dispatcher

Returns the app’s logger.

Direct global access to the logger via this method is not recommended. Instead, consider accessing the logger via the app or slice container, in most cases as an dependency using the ‘Deps` mixin.

Examples:

# app/my_component.rb

module MyApp
  class MyComponent
    include Deps["logger"]

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

Returns:

  • (Dry::Logger::Dispatcher)

Since:

  • 1.0.0



217
218
219
# File 'lib/hanami.rb', line 217

def self.logger
  app[:logger]
end

.prepareObject

Prepares the Hanami app.

See Also:

  • App::ClassMethods#prepare

Since:

  • 2.0.0



227
228
229
# File 'lib/hanami.rb', line 227

def self.prepare
  app.prepare
end

.setup(raise_exception: true) ⇒ app

Finds and loads the Hanami app file (‘config/app.rb`).

Raises an exception if the app file cannot be found.

Returns:

  • (app)

    the loaded app class

Since:

  • 2.0.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hanami.rb', line 53

def self.setup(raise_exception: true)
  return app if app?

  app_path = self.app_path

  if app_path
    prepare_load_path
    require(app_path.to_s)
    app
  elsif raise_exception
    raise(
      AppLoadError,
      "Could not locate your Hanami app file.\n\n" \
      "Your app file should be at `config/app.rb` in your project's root directory."
    )
  end
end

.shutdownObject

Shuts down the Hanami app.

See Also:

  • App::ClassMethods#shutdown

Since:

  • 2.0.0



247
248
249
# File 'lib/hanami.rb', line 247

def self.shutdown
  app.shutdown
end