Class: Hanami::Environment Private
- Inherits:
-
Object
- Object
- Hanami::Environment
- Defined in:
- lib/hanami/environment.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Define and expose information about the Hanami environment.
Constant Summary collapse
- LOCK =
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.
Global lock (used to serialize process of environment configuration)
Mutex.new
- RACK_ENV =
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.
Standard Rack ENV key
'RACK_ENV'.freeze
- HANAMI_ENV =
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.
Standard Hanami ENV key
'HANAMI_ENV'.freeze
- DEFAULT_ENV =
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.
Default Hanami environment
'development'.freeze
- PRODUCTION_ENV =
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.
Production environment
'production'.freeze
- RACK_ENV_DEPLOYMENT =
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.
Rack production environment (aka deployment)
'deployment'.freeze
- DEFAULT_DOTENV_ENV =
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.
Default ‘.env` per environment file name
'.env.%s'.freeze
- DEFAULT_CONFIG =
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.
Default configuration directory under application root
'config'.freeze
- HANAMI_HOST =
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.
Standard Hanami host ENV key
'HANAMI_HOST'.freeze
- DEFAULT_HOST =
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.
Default HTTP host
'localhost'.freeze
- LISTEN_ALL_HOST =
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.
Default IP address listen
'0.0.0.0'.freeze
- HANAMI_PORT =
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.
Standard Hanami port ENV key
'HANAMI_PORT'.freeze
- DEFAULT_PORT =
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.
Default Hanami HTTP port
2300
- DEFAULT_RACKUP =
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.
Default Rack configuration file
'config.ru'.freeze
- DEFAULT_ENVIRONMENT_CONFIG =
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.
Default environment configuration file
'environment'.freeze
- CODE_RELOADING =
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.
Code reloading per environment
{ 'development' => true }.freeze
- CONTAINER =
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.
'container'.freeze
- CONTAINER_PATH =
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.
'apps'.freeze
- APPLICATION =
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.
'app'.freeze
- APPLICATION_PATH =
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.
'app'.freeze
- SERVE_STATIC_ASSETS =
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.
'SERVE_STATIC_ASSETS'.freeze
- SERVE_STATIC_ASSETS_ENABLED =
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.
'true'.freeze
Instance Method Summary collapse
- #apps_path ⇒ Object private
- #architecture ⇒ Object private
-
#bundler_groups ⇒ Array
private
A set of Bundler groups.
-
#code_reloading? ⇒ TrueClass, FalseClass
private
Determine if activate code reloading for the current environment while running the server.
-
#config ⇒ Pathname
private
Application’s config directory.
- #container? ⇒ Boolean private
-
#default_port? ⇒ Boolean
private
Check if the current port is the default one.
-
#env_config ⇒ Pathname
(also: #project_environment_configuration)
private
Path to environment configuration file.
-
#environment ⇒ String
private
The current environment.
- #environment?(*names) ⇒ Boolean private
-
#host ⇒ String
private
The HTTP host name.
-
#initialize(options = {}) ⇒ Hanami::Environment
constructor
private
Initialize a Hanami environment.
-
#port ⇒ Integer
private
The HTTP port.
-
#project_name ⇒ String
private
Project name.
-
#rackup ⇒ Pathname
private
Path to the Rack configuration file.
-
#require_application_environment ⇒ Object
(also: #require_project_environment)
private
Require application environment.
-
#root ⇒ Pathname
private
Application’s root.
- #serve_static_assets? ⇒ Boolean private
- #static_assets_middleware ⇒ Object private
-
#to_options ⇒ Hanami::Utils::Hash
private
Serialize the most relevant settings into a Hash.
Constructor Details
#initialize(options = {}) ⇒ Hanami::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.
Initialize a Hanami environment
It accepts an optional set of configurations from the CLI commands. Those settings override the defaults defined by this object.
When initialized, it sets standard ‘ENV` variables for Rack and Hanami, such as `RACK_ENV` and `HANAMI_ENV`.
It also evaluates configuration from ‘.env` and `.env.<environment>` located under the config directory. All the settings in those files will be exported as `ENV` variables.
The format of those ‘.env` files is compatible with `dotenv` and `foreman` gems.
196 197 198 199 200 201 202 |
# File 'lib/hanami/environment.rb', line 196 def initialize( = {}) opts = .to_h.dup @env = Hanami::Env.new(env: opts.delete(:env) || ENV) @options = Hanami::Hanamirc.new(root). @options.merge! Utils::Hash.new(opts.clone).symbolize! LOCK.synchronize { set_env_vars! } end |
Instance Method Details
#apps_path ⇒ 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.
454 455 456 457 458 459 460 461 |
# File 'lib/hanami/environment.rb', line 454 def apps_path @options.fetch(:path) { case architecture when CONTAINER then CONTAINER_PATH when APPLICATION then APPLICATION_PATH end } end |
#architecture ⇒ 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.
419 420 421 422 423 424 |
# File 'lib/hanami/environment.rb', line 419 def architecture @options.fetch(:architecture) do puts "Cannot recognize Hanami architecture, please check `.hanamirc'" exit 1 end end |
#bundler_groups ⇒ Array
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.
A set of Bundler groups
239 240 241 |
# File 'lib/hanami/environment.rb', line 239 def bundler_groups [:default, environment] 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.
Determine if activate code reloading for the current environment while running the server.
In order to decide the value, it looks up the following sources:
* CLI option `code_reloading`
If those are missing it falls back to the following defaults:
* true for development
* false for all the other environments
413 414 415 |
# File 'lib/hanami/environment.rb', line 413 def code_reloading? @options.fetch(:code_reloading) { !!CODE_RELOADING[environment] } end |
#config ⇒ Pathname
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.
Application’s config directory
It’s the application where all the configurations are stored.
In order to decide the value, it looks up the following sources:
* CLI option `config`
If those are missing it falls back to the default one: ‘“config/”`.
When a relative path is given via CLI option, it assumes to be located under application’s root. If absolute path, it will be used as it is.
284 285 286 |
# File 'lib/hanami/environment.rb', line 284 def config @config ||= root.join(@options.fetch(:config) { DEFAULT_CONFIG }) end |
#container? ⇒ 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.
428 429 430 |
# File 'lib/hanami/environment.rb', line 428 def container? architecture == CONTAINER end |
#default_port? ⇒ 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.
Check if the current port is the default one
338 339 340 |
# File 'lib/hanami/environment.rb', line 338 def default_port? port == DEFAULT_PORT end |
#env_config ⇒ Pathname Also known as: project_environment_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.
Path to environment configuration file.
In order to decide the value, it looks up the following sources:
* CLI option `environment`
If those are missing it falls back to the default one: ‘“config/environment.rb”`.
When a relative path is given via CLI option, it assumes to be located under application’s root. If absolute path, it will be used as it is.
377 378 379 |
# File 'lib/hanami/environment.rb', line 377 def env_config root.join(@options.fetch(:environment) { config.join(DEFAULT_ENVIRONMENT_CONFIG) }) end |
#environment ⇒ String
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.
The current environment
In order to decide the value, it looks up to the following ‘ENV` vars:
* HANAMI_ENV
* RACK_ENV
If those are missing it falls back to the default one: ‘“development”`.
Rack environment ‘“deployment”` is translated to Hanami `“production”`.
220 221 222 |
# File 'lib/hanami/environment.rb', line 220 def environment @environment ||= env[HANAMI_ENV] || rack_env || DEFAULT_ENV end |
#environment?(*names) ⇒ 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.
227 228 229 |
# File 'lib/hanami/environment.rb', line 227 def environment?(*names) names.map(&:to_s).include?(environment) end |
#host ⇒ String
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.
The HTTP host name
In order to decide the value, it looks up the following sources:
* CLI option `host`
* HANAMI_HOST ENV var
If those are missing it falls back to the following defaults:
* `"localhost"` for development
* `"0.0.0.0"` for all the other environments
306 307 308 309 310 |
# File 'lib/hanami/environment.rb', line 306 def host @host ||= @options.fetch(:host) do env[HANAMI_HOST] || default_host end end |
#port ⇒ Integer
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.
The HTTP port
In order to decide the value, it looks up the following sources:
* CLI option `port`
* HANAMI_PORT ENV var
If those are missing it falls back to the default one: ‘2300`.
326 327 328 329 330 |
# File 'lib/hanami/environment.rb', line 326 def port @port ||= @options.fetch(:port) do env[HANAMI_PORT] || DEFAULT_PORT end.to_i end |
#project_name ⇒ String
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.
Project name
249 250 251 |
# File 'lib/hanami/environment.rb', line 249 def project_name @options.fetch(:project) end |
#rackup ⇒ Pathname
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.
Path to the Rack configuration file
In order to decide the value, it looks up the following sources:
* CLI option `rackup`
If those are missing it falls back to the default one: ‘“config.ru”`.
When a relative path is given via CLI option, it assumes to be located under application’s root. If absolute path, it will be used as it is.
356 357 358 |
# File 'lib/hanami/environment.rb', line 356 def rackup root.join(@options.fetch(:rackup) { DEFAULT_RACKUP }) end |
#require_application_environment ⇒ Object Also known as: require_project_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.
Require application environment
Eg require "config/environment"
.
389 390 391 |
# File 'lib/hanami/environment.rb', line 389 def require_application_environment require project_environment_configuration.to_s # if project_environment_configuration.exist? end |
#root ⇒ Pathname
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.
Application’s root
It defaults to the current working directory. Hanami assumes that all the commands are executed from there.
261 262 263 |
# File 'lib/hanami/environment.rb', line 261 def root @root ||= Pathname.new(Dir.pwd) end |
#serve_static_assets? ⇒ 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.
434 435 436 |
# File 'lib/hanami/environment.rb', line 434 def serve_static_assets? SERVE_STATIC_ASSETS_ENABLED == env[SERVE_STATIC_ASSETS] end |
#static_assets_middleware ⇒ 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.
440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/hanami/environment.rb', line 440 def static_assets_middleware return unless serve_static_assets? if environment?(:development, :test) require 'hanami/assets/static' Hanami::Assets::Static else require 'hanami/static' Hanami::Static end end |
#to_options ⇒ Hanami::Utils::Hash
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.
Serialize the most relevant settings into a Hash
469 470 471 472 473 474 475 476 477 478 |
# File 'lib/hanami/environment.rb', line 469 def @options.merge( environment: environment, env_config: env_config, apps_path: apps_path, rackup: rackup, host: host, port: port ) end |