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.
Class.new(StandardError)
- AppLoadError =
Error raised when App fails to load.
Class.new(Error)
- SliceLoadError =
Error raised when an Slice fails to load.
Class.new(Error)
- ComponentLoadError =
Error raised when an individual component fails to load.
Class.new(Error)
- UnsupportedMiddlewareSpecError =
Error raised when unsupported middleware configuration is given.
Class.new(Error)
- VERSION =
Defines the full version
Version.version
Class Method Summary collapse
-
.app ⇒ Hanami::App
Returns the Hamami app class.
- .app=(klass) ⇒ Object private
-
.app? ⇒ Boolean
Returns true if the Hanami app class has been loaded.
-
.app_path(dir = Dir.pwd) ⇒ Pathname?
Finds and returns the absolute path for the Hanami app file (‘config/app.rb`).
-
.boot ⇒ Object
Boots the Hanami app.
- .bundled?(gem_name) ⇒ Boolean private
-
.bundler_groups ⇒ Object
private
Returns an array of bundler group names to be eagerly loaded by hanami-cli and other CLI extensions.
-
.env(e: ENV) ⇒ Symbol
Returns the Hanami app environment as loaded from the ‘HANAMI_ENV` environment variable.
-
.env?(*names) ⇒ Boolean
Returns true if Hanami.env matches any of the given names.
- .loader ⇒ Object private
-
.logger ⇒ Dry::Logger::Dispatcher
Returns the app’s logger.
-
.prepare ⇒ Object
Prepares the Hanami app.
-
.setup(raise_exception: true) ⇒ app
Finds and loads the Hanami app file (‘config/app.rb`).
-
.shutdown ⇒ Object
Shuts down the Hanami app.
Class Method Details
.app ⇒ Hanami::App
Returns the Hamami app class.
To ensure your Hanami app is loaded, run setup (or ‘require “hanami/setup”`) first.
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.
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.
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.
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). path = dir.join(APP_PATH) if path.file? path elsif !dir.root? app_path(dir.parent) end end |
.boot ⇒ Object
Boots the Hanami app.
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.
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_groups ⇒ 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.
Returns an array of bundler group names to be eagerly loaded by hanami-cli and other CLI extensions.
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.
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
163 164 165 |
# File 'lib/hanami.rb', line 163 def self.env?(*names) names.map(&:to_sym).include?(env) end |
.loader ⇒ 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.
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 |
.logger ⇒ Dry::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.
190 191 192 |
# File 'lib/hanami.rb', line 190 def self.logger app[:logger] end |
.prepare ⇒ Object
Prepares the Hanami app.
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.
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 |
.shutdown ⇒ Object
Shuts down the Hanami app.
220 221 222 |
# File 'lib/hanami.rb', line 220 def self.shutdown app.shutdown end |