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/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/extensions/action/slice_configured_action.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

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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hanami.rb', line 84

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



108
109
110
111
112
113
114
115
116
# File 'lib/hanami.rb', line 108

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



102
103
104
# File 'lib/hanami.rb', line 102

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



130
131
132
133
134
135
136
137
138
139
# File 'lib/hanami.rb', line 130

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



213
214
215
# File 'lib/hanami.rb', line 213

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



229
230
231
232
233
234
235
236
237
# File 'lib/hanami.rb', line 229

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



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

def self.bundler_groups
  [:plugins]
end

.env(e: ENV) ⇒ Symbol

Returns the Hanami app environment as loaded from the ‘HANAMI_ENV` environment variable.

Examples:

Hanami.env # => :development

Returns:

  • (Symbol)

    the environment name

Since:

  • 2.0.0



150
151
152
# File 'lib/hanami.rb', line 150

def self.env(e: ENV)
  e.fetch("HANAMI_ENV") { e.fetch("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



166
167
168
# File 'lib/hanami.rb', line 166

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
# 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 "sql_adapter" => "SQLAdapter"
    loader.ignore(
      "#{loader.dirs.first}/hanami/{constants,boot,errors,extensions/router/errors,prepare,rake_tasks,setup}.rb"
    )
  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



193
194
195
# File 'lib/hanami.rb', line 193

def self.logger
  app[:logger]
end

.prepareObject

Prepares the Hanami app.

See Also:

  • App::ClassMethods#prepare

Since:

  • 2.0.0



203
204
205
# File 'lib/hanami.rb', line 203

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hanami.rb', line 37

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



223
224
225
# File 'lib/hanami.rb', line 223

def self.shutdown
  app.shutdown
end