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/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/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/extensions/action.rb,
lib/hanami/middleware/assets.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/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/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/providers/db/adapters.rb,
lib/hanami/config/actions/cookies.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/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/operation/slice_configured_db_operation.rb

Overview

rubocop:disable Lint/RescueException

Defined Under Namespace

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

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

Raises:

See Also:

Since:

  • 2.0.0



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

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



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

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.

Since:

  • 2.0.0



116
117
118
# File 'lib/hanami.rb', line 116

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.

Since:

  • 2.0.0



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

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



235
236
237
# File 'lib/hanami.rb', line 235

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.

Since:

  • 2.0.0



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

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



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

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

Since:

  • 2.0.0



172
173
174
# File 'lib/hanami.rb', line 172

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

Since:

  • 2.0.0



188
189
190
# File 'lib/hanami.rb', line 188

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
# 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"

    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

Since:

  • 1.0.0



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

def self.logger
  app[:logger]
end

.prepareObject

Prepares the Hanami app.

See Also:

  • App::ClassMethods#prepare

Since:

  • 2.0.0



225
226
227
# File 'lib/hanami.rb', line 225

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.

Since:

  • 2.0.0



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

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



245
246
247
# File 'lib/hanami.rb', line 245

def self.shutdown
  app.shutdown
end