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

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
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
DEFAULT_DOTENV =

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` file name

Since:

  • 0.2.0

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

Since:

  • 0.2.0

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

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

Since:

  • 0.4.0

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

Since:

  • 0.4.0

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

Since:

  • 0.4.0

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

Since:

  • 0.4.0

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

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

Examples:

Define ENV variables from .env


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

# % cat .env
#   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']        # => "yes"

# Hanami::Environment evaluates `.env` first as master configuration.
# Then it evaluates `.env.development` because the current environment
# is "development". The settings defined in this last file override
# the one defined in the parent (eg `FOO` is overwritten). All the
# other settings (eg `XYZ`) will be left untouched.

Parameters:

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

    override default options for various environment attributes

See Also:

Since:

  • 0.1.0



195
196
197
198
199
200
# File 'lib/hanami/environment.rb', line 195

def initialize(options = {})
  @options = Hanami::Hanamirc.new(root).options
  @options.merge! Utils::Hash.new(options.clone).symbolize!
  @mutex   = Mutex.new
  @mutex.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



418
419
420
421
422
423
424
425
# File 'lib/hanami/environment.rb', line 418

def apps_path
  @options.fetch(:path) {
    case architecture
    when CONTAINER   then CONTAINER_PATH
    when APPLICATION then APPLICATION_PATH
    end
  }
end

#architectureObject

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



397
398
399
400
401
402
# File 'lib/hanami/environment.rb', line 397

def architecture
  @options.fetch(:architecture) {
    puts "Cannot recognize Hanami architecture, please check `.hanamirc'"
    exit 1
  }
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



237
238
239
# File 'lib/hanami/environment.rb', line 237

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



385
386
387
388
389
390
391
392
393
# File 'lib/hanami/environment.rb', line 385

def code_reloading?
  # JRuby doesn't implement fork that's why shotgun cannot be used.
  if Utils.jruby?
    puts "JRuby doesn't support code reloading."
    false
  else
    @options.fetch(:code_reloading) { !!CODE_RELOADING[environment] }
  end
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



272
273
274
# File 'lib/hanami/environment.rb', line 272

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.

Returns:

  • (Boolean)

Since:

  • 0.4.0



406
407
408
# File 'lib/hanami/environment.rb', line 406

def container?
  architecture == CONTAINER
end

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

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



353
354
355
# File 'lib/hanami/environment.rb', line 353

def env_config
  root.join(@options.fetch(:environment) { config.join(DEFAULT_ENVIRONMENT_CONFIG) })
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 defalt one: ‘“development”`.

Rack environment ‘“deployment”` is translated to Hanami `“production”`.

Returns:

  • (String)

    the current environment

See Also:

Since:

  • 0.1.0



218
219
220
# File 'lib/hanami/environment.rb', line 218

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



225
226
227
# File 'lib/hanami/environment.rb', line 225

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



294
295
296
297
298
# File 'lib/hanami/environment.rb', line 294

def host
  @host ||= @options.fetch(:host) {
    ENV[HANAMI_HOST] || default_host
  }
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



314
315
316
# File 'lib/hanami/environment.rb', line 314

def port
  @port ||= @options.fetch(:port) { ENV[HANAMI_PORT] || DEFAULT_PORT }.to_i
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



332
333
334
# File 'lib/hanami/environment.rb', line 332

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

#require_application_environmentObject

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



363
364
365
# File 'lib/hanami/environment.rb', line 363

def require_application_environment
  require env_config.to_s
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



249
250
251
# File 'lib/hanami/environment.rb', line 249

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



412
413
414
# File 'lib/hanami/environment.rb', line 412

def serve_static_assets?
  SERVE_STATIC_ASSETS_ENABLED == ENV[SERVE_STATIC_ASSETS]
end

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

Returns:

  • (Hanami::Utils::Hash)

Since:

  • 0.1.0



433
434
435
436
437
438
439
440
441
442
# File 'lib/hanami/environment.rb', line 433

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