module Hanami

A complete web framework for Ruby

@since 0.1.0

@see hanamirb.org

rubocop:disable Metrics/ModuleLength

rubocop:disable Lint/RescueException

@api private @since 2.2.0

Constants

APP_DIR

@api private

APP_PATH

@api private

AppLoadError

Error raised when {Hanami::App} fails to load.

@api public @since 2.0.0

CONFIG_DIR

@api private

CONTAINER_KEY_DELIMITER

@api private

ComponentLoadError

Error raised when an individual component fails to load.

@api public @since 2.0.0

Error

Base class for all Hanami errors.

@api public @since 2.0.0

LIB_DIR

@api private

MODULE_DELIMITER

@api private

PATH_DELIMITER

@api private

RB_EXT

@api private

RB_EXT_REGEXP

@api private

ROUTES_CLASS_NAME

@api private

ROUTES_PATH

@api private

SETTINGS_CLASS_NAME

@api private

SETTINGS_PATH

@api private

SLICES_DIR

@api private

SliceLoadError

Error raised when an {Hanami::Slice} fails to load.

@api public @since 2.0.0

UnsupportedMiddlewareSpecError

Error raised when unsupported middleware configuration is given.

@see Hanami::Slice::Routing::Middleware::Stack#use

@api public @since 2.0.0

VERSION

Defines the full version

@since 0.1.0

Public Class Methods

app() click to toggle source

Returns the Hamami app class.

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

@return [Hanami::App] the app class

@raise [AppLoadError] if the app has not been loaded

@see .setup

@api public @since 2.0.0

# 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) click to toggle source

@api private @since 2.0.0

# 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?() click to toggle source

Returns true if the Hanami app class has been loaded.

@return [Boolean]

@api public @since 2.0.0

# File lib/hanami.rb, line 102
def self.app?
  instance_variable_defined?(:@_app)
end
app_path(dir = Dir.pwd) click to toggle source

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.

@param dir [String, Pathname] The directory from which to start searching. Defaults to the

current directory.

@return [Pathname, nil] the app file path, or nil if not found.

@api public @since 2.0.0

# 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
boot() click to toggle source

Boots the Hanami app.

@see App::ClassMethods#boot

@api public @since 2.0.0

# File lib/hanami.rb, line 213
def self.boot
  app.boot
end
bundled?(gem_name) click to toggle source

@api private @since 2.0.0

# 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_groups() click to toggle source

Returns an array of bundler group names to be eagerly loaded by hanami-cli and other CLI extensions.

@api private @since 2.0.0

# File lib/hanami.rb, line 244
def self.bundler_groups
  [:plugins]
end
env(e: ENV) click to toggle source

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

@example

Hanami.env # => :development

@return [Symbol] the environment name

@api public @since 2.0.0

# 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) click to toggle source

Returns true if {.env} matches any of the given names

@example

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

@param names [Array<Symbol>] the environment names to check

@return [Boolean]

@api public @since 2.0.0

# File lib/hanami.rb, line 166
def self.env?(*names)
  names.map(&:to_sym).include?(env)
end
loader() click to toggle source

@api private @since 2.0.0

# 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
logger() click to toggle source

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.

@example

# app/my_component.rb

module MyApp
  class MyComponent
    include Deps["logger"]

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

@return [Dry::Logger::Dispatcher]

@api public @since 1.0.0

# File lib/hanami.rb, line 193
def self.logger
  app[:logger]
end
prepare() click to toggle source

Prepares the Hanami app.

@see App::ClassMethods#prepare

@api public @since 2.0.0

# File lib/hanami.rb, line 203
def self.prepare
  app.prepare
end
prepare_load_path() click to toggle source

Prepare the load path as early as possible (based on the default root inferred from the location of ‘config/app.rb`), so `require` can work at the top of `config/app.rb`. This may be useful when external classes are needed for configuring certain aspects of the app.

@api private @since 2.0.0

# File lib/hanami.rb, line 61
                     def self.prepare_load_path
  lib_path = app_path&.join("..", "..", LIB_DIR)

  if lib_path&.directory?
    path = lib_path.realpath.to_s
    $LOAD_PATH.prepend(path) unless $LOAD_PATH.include?(path)
  end

  lib_path
end
setup(raise_exception: true) click to toggle source

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

Raises an exception if the app file cannot be found.

@return [app] the loaded app class

@api public @since 2.0.0

# 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
shutdown() click to toggle source

Shuts down the Hanami app.

@see App::ClassMethods#shutdown

@api public @since 2.0.0

# File lib/hanami.rb, line 223
def self.shutdown
  app.shutdown
end