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/constants.rb,
lib/hanami/slice_name.rb,
lib/hanami/web/welcome.rb,
lib/hanami/config/views.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/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/settings/env_store.rb,
lib/hanami/slice_configurable.rb,
lib/hanami/helpers/form_helper.rb,
lib/hanami/providers/inflector.rb,
lib/hanami/slice/routes_helper.rb,
lib/hanami/extensions/view/part.rb,
lib/hanami/extensions/view/scope.rb,
lib/hanami/helpers/assets_helper.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/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, Providers, SliceConfigurable, Version, Web Classes: App, Config, 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



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hanami.rb', line 81

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



105
106
107
108
109
110
111
112
113
# File 'lib/hanami.rb', line 105

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



99
100
101
# File 'lib/hanami.rb', line 99

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



127
128
129
130
131
132
133
134
135
136
# File 'lib/hanami.rb', line 127

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



210
211
212
# File 'lib/hanami.rb', line 210

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



226
227
228
229
230
231
232
233
234
# File 'lib/hanami.rb', line 226

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



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

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



147
148
149
# File 'lib/hanami.rb', line 147

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



163
164
165
# File 'lib/hanami.rb', line 163

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
# File 'lib/hanami.rb', line 18

def self.loader
  @loader ||= Zeitwerk::Loader.for_gem.tap do |loader|
    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



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

def self.logger
  app[:logger]
end

.prepareObject

Prepares the Hanami app.

See Also:

  • App::ClassMethods#prepare

Since:

  • 2.0.0



200
201
202
# File 'lib/hanami.rb', line 200

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



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

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



220
221
222
# File 'lib/hanami.rb', line 220

def self.shutdown
  app.shutdown
end