Module: Hanami

Defined in:
lib/hanami.rb,
lib/hanami/app.rb,
lib/hanami/env.rb,
lib/hanami/routes.rb,
lib/hanami/server.rb,
lib/hanami/static.rb,
lib/hanami/version.rb,
lib/hanami/welcome.rb,
lib/hanami/hanamirc.rb,
lib/hanami/components.rb,
lib/hanami/application.rb,
lib/hanami/early_hints.rb,
lib/hanami/environment.rb,
lib/hanami/rake_helper.rb,
lib/hanami/assets/asset.rb,
lib/hanami/cli/commands.rb,
lib/hanami/assets/static.rb,
lib/hanami/common_logger.rb,
lib/hanami/config/mapper.rb,
lib/hanami/config/routes.rb,
lib/hanami/configuration.rb,
lib/hanami/views/default.rb,
lib/hanami/config/cookies.rb,
lib/hanami/cli/commands/db.rb,
lib/hanami/config/security.rb,
lib/hanami/config/sessions.rb,
lib/hanami/routing/default.rb,
lib/hanami/views/null_view.rb,
lib/hanami/application_name.rb,
lib/hanami/cli/commands/new.rb,
lib/hanami/middleware_stack.rb,
lib/hanami/rendering_policy.rb,
lib/hanami/config/load_paths.rb,
lib/hanami/configuration/app.rb,
lib/hanami/cli/commands/assets.rb,
lib/hanami/cli/commands/routes.rb,
lib/hanami/cli/commands/server.rb,
lib/hanami/components/app/view.rb,
lib/hanami/cli/commands/command.rb,
lib/hanami/cli/commands/console.rb,
lib/hanami/cli/commands/db/drop.rb,
lib/hanami/cli/commands/destroy.rb,
lib/hanami/cli/commands/project.rb,
lib/hanami/cli/commands/version.rb,
lib/hanami/components/component.rb,
lib/hanami/application_namespace.rb,
lib/hanami/cli/commands/db/apply.rb,
lib/hanami/cli/commands/generate.rb,
lib/hanami/components/app/assets.rb,
lib/hanami/components/app/routes.rb,
lib/hanami/components/components.rb,
lib/hanami/action/csrf_protection.rb,
lib/hanami/action/routing_helpers.rb,
lib/hanami/cli/commands/db/create.rb,
lib/hanami/cli/commands/templates.rb,
lib/hanami/cli/commands/db/console.rb,
lib/hanami/cli/commands/db/migrate.rb,
lib/hanami/cli/commands/db/prepare.rb,
lib/hanami/cli/commands/db/version.rb,
lib/hanami/cli/commands/db/rollback.rb,
lib/hanami/cli/commands/destroy/app.rb,
lib/hanami/configuration/middleware.rb,
lib/hanami/application_configuration.rb,
lib/hanami/cli/commands/generate/app.rb,
lib/hanami/components/app/controller.rb,
lib/hanami/cli/commands/destroy/model.rb,
lib/hanami/cli/commands/destroy/action.rb,
lib/hanami/cli/commands/destroy/mailer.rb,
lib/hanami/cli/commands/generate/model.rb,
lib/hanami/components/routes_inspector.rb,
lib/hanami/cli/commands/generate/action.rb,
lib/hanami/cli/commands/generate/mailer.rb,
lib/hanami/cli/commands/generate/secret.rb,
lib/hanami/views/default_template_finder.rb,
lib/hanami/cli/commands/assets/precompile.rb,
lib/hanami/cli/commands/destroy/migration.rb,
lib/hanami/config/framework_configuration.rb,
lib/hanami/cli/commands/generate/migration.rb,
lib/hanami/environment_application_configurations.rb

Overview

Copyright notice

This file contains a method copied from Rack::Static (rack gem).

Rack - Copyright © 2007 Christian Neukirchen Released under the MIT License

Defined Under Namespace

Modules: Action, Assets, Components, Config, Mailer, Routing, Version, Views Classes: App, Application, ApplicationConfiguration, ApplicationName, ApplicationNamespace, CLI, CommonLogger, Configuration, EarlyHints, Env, Environment, EnvironmentApplicationConfigurations, Hanamirc, MiddlewareStack, RakeHelper, RenderingPolicy, Routes, Server, Static, Welcome

Constant Summary collapse

DEFAULT_PUBLIC_DIRECTORY =

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

'public'.freeze
VERSION =

Defines the full version

Since:

  • 0.1.0

Version.version

Class Method Summary collapse

Class Method Details

.appHanami::App

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.

Main application that mounts many Rack and/or Hanami applications.

This is used as integration point for:

* `config.ru` (`run Hanami.app`)
* Feature tests (`Capybara.app = Hanami.app`)

It lazily loads your Hanami project, in case it wasn’t booted on before. This is the case when ‘hanami server` isn’t invoked, but we use different ways to run the project (eg. ‘rackup`).

Returns:

See Also:

Since:

  • 0.9.0



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

def self.app
  boot
  App.new(configuration, environment)
end

.app?(app) ⇒ TrueClass, FalseClass

Check if an application is allowed to load.

The list of applications to be loaded can be set via the ‘HANAMI_APPS` env variable. If the HANAMI_APPS env variable is not set, it defaults to loading all applications.

Examples:


# Mount hanami app for specific app
Hanami.configure do
  if Hanami.app?(:web)
    require_relative '../apps/web/application'
    mount Web::Application, at: '/'
  end
end

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 1.1.0



175
176
177
178
179
180
# File 'lib/hanami.rb', line 175

def self.app?(app)
  return true unless ENV.key?('HANAMI_APPS')

  allowed_apps = ENV['HANAMI_APPS'].to_s.split(',')
  allowed_apps.include?(app.to_s.downcase)
end

.bootNilClass

Boot your Hanami project

NOTE: In case this is invoked many times, it guarantees that the boot

process happens only once.

NOTE: This MUST NOT be wrapped by a Mutex, because it would cause a deadlock.

Returns:

  • (NilClass)

Since:

  • 0.9.0



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

def self.boot
  Components.release if code_reloading?
  Components.resolve('all')
  Hanami::Model.disconnect if defined?(Hanami::Model)
  nil
end

.code_reloading?TrueClass, FalseClass

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.

Check if code reloading is enabled.

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 1.0.0



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

def self.code_reloading?
  environment
  Components.resolve('code_reloading')
  Components['code_reloading']
end

.configurationHanami::Configuration

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.

Hanami configuration

Returns:

See Also:

Since:

  • 0.9.0



74
75
76
77
78
79
# File 'lib/hanami.rb', line 74

def self.configuration
  @_mutex.synchronize do
    raise "Hanami not configured" unless defined?(@_configuration)
    @_configuration
  end
end

.configure(&blk) ⇒ Object

Configure Hanami project

Please note that the code for this method is generated by ‘hanami new`.

Examples:

# config/environment.rb

# ...

Hanami.configure do
  mount Admin::Application, at: "/admin"
  mount Web::Application,   at: "/"

  model do
    adapter :sql, ENV['DATABASE_URL']

    migrations "db/migrations"
    schema     "db/schema.sql"
  end

  mailer do
    root "lib/bookshelf/mailers"

    delivery do
      development :test
      test        :test
      # production :smtp, address: ENV['SMTP_HOST'], port: ENV['SMTP_PORT']
    end
  end
end

Parameters:

  • blk (Proc)

    the configuration block

Since:

  • 0.9.0



60
61
62
63
64
# File 'lib/hanami.rb', line 60

def self.configure(&blk)
  @_mutex.synchronize do
    @_configuration = Hanami::Configuration.new(&blk)
  end
end

.envString

Return the current environment

Examples:

Hanami.env => "development"

Returns:

  • (String)

    the current environment

See Also:

Since:

  • 0.3.1



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

def self.env
  environment.environment
end

.env?(*names) ⇒ TrueClass, FalseClass

Check to see if specified environment(s) matches the current environment.

If multiple names are given, it returns true, if at least one of them matches the current environment.

Examples:

Single name

puts ENV['HANAMI_ENV'] # => "development"

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

Hanami.env?(:production)   # => false

Multiple names

puts ENV['HANAMI_ENV'] # => "development"

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

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 0.3.1



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

def self.env?(*names)
  environment.environment?(*names)
end

.environmentHanami::Environment

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.

Current environment

Returns:

Since:

  • 0.3.2



254
255
256
257
258
# File 'lib/hanami.rb', line 254

def self.environment
  Components.resolved('environment') do
    Environment.new
  end
end

.loggerHanami::Logger

Project logger

Returns:

  • (Hanami::Logger)

    the logger

Since:

  • 1.0.0



279
280
281
# File 'lib/hanami.rb', line 279

def self.logger
  Components['logger']
end

.plugin(&blk) ⇒ Object

Configure a plugin

See Also:

Since:

  • 1.2.0



86
87
88
# File 'lib/hanami.rb', line 86

def self.plugin(&blk)
  @_plugins << blk
end

.pluginsObject

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.

Plugins registry

NOTE: We can’t use ‘Components` registry.

Plugins are loaded when Bundler requires ‘Gemfile` gems. During this phase the `Components` that we can resolve are erased by the first incoming request in development. They are erased by a workaround that we had to put in place in `Hanami.boot`. This workaround is `Components.release` and it was introduced because `shotgun` failed to reload components, so we have to release for each incoming request. After the `Components` registry is cleared up, Hanami is able to resolve all the components from scratch.

When we’ll switch to ‘hanami-reloader` for development, we can remove `Components.release` and we’ll be able to store plugins in ‘Components` and remove `Hanami.plugins` as well.

Since:

  • 1.2.0



110
111
112
# File 'lib/hanami.rb', line 110

def self.plugins
  @_plugins
end

.public_directoryPathname

Project public directory

Examples:

Hanami.public_directory # => #<Pathname:/Users/luca/Code/bookshelf/public>

Returns:

  • (Pathname)

    public directory

Since:

  • 0.6.0



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

def self.public_directory
  root.join(DEFAULT_PUBLIC_DIRECTORY)
end

.rootPathname

Return root of the project (top level directory).

Examples:

Hanami.root # => #<Pathname:/Users/luca/Code/bookshelf>

Returns:

  • (Pathname)

    root path

Since:

  • 0.3.2



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

def self.root
  environment.root
end