Class: Hanami::Environment Private

Inherits:
Object
  • Object
show all
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.

Since:

  • 0.1.0

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)

Since:

  • 0.8.0

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

Since:

  • 0.1.0

'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

Since:

  • 0.1.0

'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

Since:

  • 0.1.0

'development'.freeze
TEST_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.

Test environment

Since:

  • 1.3.3

'test'.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

Since:

  • 0.6.0

'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)

Since:

  • 0.6.0

'deployment'.freeze
DOTENV_LOCAL_FILE =

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:

  • 1.3.3

'.env.local'.freeze
DOTENV_FILES =

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` files that are loaded. The entries are ordered from highest to lowest priority.

Since:

  • 1.3.3

[
  '.env.%{environment}.local'.freeze,
  DOTENV_LOCAL_FILE,
  '.env.%{environment}'.freeze,
  '.env'.freeze
].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

Since:

  • 0.2.0

'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

Since:

  • 0.1.0

'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

Since:

  • 0.1.0

'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

Since:

  • 0.1.0

'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

Since:

  • 0.1.0

'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

Since:

  • 0.1.0

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

Since:

  • 0.2.0

'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

Since:

  • 0.2.0

'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

Since:

  • 0.2.0

{ 'development' => true }.freeze
APPS_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.

Since:

  • 0.4.0

'apps'.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.

Since:

  • 0.4.0

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

Since:

  • 0.4.0

'true'.freeze

Instance Method Summary collapse

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 evaluates configuration ONLY from `.env.<environment>` file located under the config directory. All the settings in those files will be exported as `ENV` variables.

This table: github.com/bkeepers/dotenv#what-other-env-files-can-i-use has more info on the priority of the .env files.

The format of those `.env.<environment>` files follows UNIX and UNIX-like operating system environment variable declaration format and compatible with `dotenv` and `foreman` gems.

Examples:

Define ENV variables from .env


# % tree .
#   .
#   # ...
#   ├── .env.test
#   └── .env.development

# % cat .env.test
#   FOO="bar"
#   XYZ="yes"

# % cat .env.development
#   FOO="ok"

require 'hanami/environment'

env = Hanami::Environment.new
env.environment   # => "development"

# Framework defined ENV vars
ENV['HANAMI_ENV']  # => "development"
ENV['RACK_ENV']   # => "development"

ENV['HANAMI_HOST'] # => "localhost"
ENV['HANAMI_PORT'] # => "2300"

# User defined ENV vars
ENV['FOO']        # => "ok"
ENV['XYZ']        # => nil

# Hanami::Environment evaluates `.env.development` because the current
# environment is "development".
# Variables declared on `.env.development` will not override
# any variable declared on the shell when calling a `hanami` command.
# Eg. In `FOO="not ok" bundle exec hanami c` `FOO` will not be overwritten
# to `"ok"`.

Parameters:

  • options (Hash) (defaults to: {})

    override default options for various environment attributes

See Also:

  • Commands::Console
  • Commands::Routes
  • Commands::Server
  • #config

Since:

  • 0.1.0


207
208
209
210
211
212
213
# File 'lib/hanami/environment.rb', line 207

def initialize(options = {})
  opts     = options.to_h.dup
  @env     = Hanami::Env.new(env: opts.delete(:env) || ENV)
  @options = Hanami::Hanamirc.new(root).options
  @options.merge! Utils::Hash.symbolize(opts.clone)
  LOCK.synchronize { set_env_vars! }
end

Instance Method Details

#apps_pathObject

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:

  • 0.4.0


461
462
463
# File 'lib/hanami/environment.rb', line 461

def apps_path
  @options.fetch(:path, APPS_PATH)
end

#bundler_groupsArray

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

Returns:

  • (Array)

    A set of groups

See Also:

Since:

  • 0.2.0


252
253
254
# File 'lib/hanami/environment.rb', line 252

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

Returns:

  • (TrueClass, FalseClass)

    the result of the check

See Also:

Since:

  • 0.2.0


435
436
437
# File 'lib/hanami/environment.rb', line 435

def code_reloading?
  @options.fetch(:code_reloading) { !!CODE_RELOADING[environment] }
end

#configPathname

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.

Returns:

  • (Pathname)

    the config directory

See Also:

Since:

  • 0.2.0


299
300
301
# File 'lib/hanami/environment.rb', line 299

def config
  @config ||= root.join(@options.fetch(:config) { DEFAULT_CONFIG })
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

Returns:

  • (Boolean)

See Also:

Since:

  • 1.0.0


355
356
357
# File 'lib/hanami/environment.rb', line 355

def default_port?
  port == DEFAULT_PORT
end

#env_configPathname 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.

Returns:

  • (Pathname)

    path to applications

See Also:

Since:

  • 0.1.0


396
397
398
# File 'lib/hanami/environment.rb', line 396

def env_config
  root.join("config", "environment.rb")
end

#environmentString

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”`.

Returns:

  • (String)

    the current environment

See Also:

Since:

  • 0.1.0


232
233
234
# File 'lib/hanami/environment.rb', line 232

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.

Returns:

  • (Boolean)

See Also:

  • Hanami.env?(name)

Since:

  • 0.3.1


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

def environment?(*names)
  names.map(&:to_s).include?(environment)
end

#hostString

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

Returns:

  • (String)

    the HTTP host name

See Also:

Since:

  • 0.1.0


322
323
324
325
326
# File 'lib/hanami/environment.rb', line 322

def host
  @host ||= @options.fetch(:host) do
    env[HANAMI_HOST] || default_host
  end
end

#portInteger

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

Returns:

  • (Integer)

    the default port

See Also:

Since:

  • 0.1.0


343
344
345
346
347
# File 'lib/hanami/environment.rb', line 343

def port
  @port ||= @options.fetch(:port) do
    env[HANAMI_PORT] || DEFAULT_PORT
  end.to_i
end

#project_nameString

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

Returns:

  • (String)

    Project name

Since:

  • 0.8.0


262
263
264
# File 'lib/hanami/environment.rb', line 262

def project_name
  @options.fetch(:project)
end

#rackupPathname

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.

Returns:

  • (Pathname)

    path to the Rack configuration file

Since:

  • 0.2.0


374
375
376
# File 'lib/hanami/environment.rb', line 374

def rackup
  root.join(@options.fetch(:rackup) { DEFAULT_RACKUP })
end

#require_application_environmentObject 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".

Since:

  • 0.4.0


408
409
410
411
# File 'lib/hanami/environment.rb', line 408

def require_application_environment
  ::Bundler.setup(*bundler_groups)
  require project_environment_configuration.to_s # if project_environment_configuration.exist?
end

#rootPathname

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.

Returns:

  • (Pathname)

    application's root

Since:

  • 0.2.0


275
276
277
# File 'lib/hanami/environment.rb', line 275

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.

Returns:

  • (Boolean)

Since:

  • 0.6.0


441
442
443
# File 'lib/hanami/environment.rb', line 441

def serve_static_assets?
  SERVE_STATIC_ASSETS_ENABLED == env[SERVE_STATIC_ASSETS]
end

#static_assets_middlewareObject

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:

  • 0.6.0


447
448
449
450
451
452
453
454
455
456
457
# File 'lib/hanami/environment.rb', line 447

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::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

Returns:

  • (::Hash)

Since:

  • 0.1.0


471
472
473
474
475
476
477
478
479
480
# File 'lib/hanami/environment.rb', line 471

def to_options
  @options.merge(
    environment: environment,
    env_config:  env_config,
    apps_path:   apps_path,
    rackup:      rackup,
    host:        host,
    port:        port
  )
end