Class: Lotus::Environment Private

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/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 Lotus 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
LOTUS_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 Lotus ENV key

Since:

  • 0.1.0

'LOTUS_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 Lotus environment

Since:

  • 0.1.0

'development'.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
LOTUS_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 Lotus host ENV key

Since:

  • 0.1.0

'LOTUS_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
LOTUS_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 Lotus port ENV key

Since:

  • 0.1.0

'LOTUS_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 Lotus 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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Lotus::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 Lotus 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 Lotus, such as `RACK_ENV` and `LOTUS_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 config/
#   config
#   ├── .env
#   ├── .env.development
#   └── environment.rb

# % cat config/.env
#   FOO="bar"
#   XYZ="yes"

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

require 'lotus/environment'

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

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

ENV['LOTUS_HOST'] # => "localhost"
ENV['LOTUS_PORT'] # => "2300"

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

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



157
158
159
160
161
# File 'lib/lotus/environment.rb', line 157

def initialize(options = {})
  @options = Utils::Hash.new(options).symbolize!.freeze
  @mutex   = Mutex.new
  @mutex.synchronize { set_env_vars! }
end

Instance Method Details

#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



189
190
191
# File 'lib/lotus/environment.rb', line 189

def bundler_groups
  [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



327
328
329
# File 'lib/lotus/environment.rb', line 327

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



224
225
226
# File 'lib/lotus/environment.rb', line 224

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



305
306
307
# File 'lib/lotus/environment.rb', line 305

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:

* LOTUS_ENV
* RACK_ENV

If those are missing it falls back to the defalt one: ‘“development”`.

Returns:

  • (String)

    the current environment

See Also:

Since:

  • 0.1.0



177
178
179
# File 'lib/lotus/environment.rb', line 177

def environment
  @environment ||= ENV[LOTUS_ENV] || ENV[RACK_ENV] || DEFAULT_ENV
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`
* LOTUS_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



246
247
248
249
250
# File 'lib/lotus/environment.rb', line 246

def host
  @host ||= @options.fetch(:host) {
    ENV[LOTUS_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`
* LOTUS_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



266
267
268
# File 'lib/lotus/environment.rb', line 266

def port
  @port ||= @options.fetch(:port) { ENV[LOTUS_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



284
285
286
# File 'lib/lotus/environment.rb', line 284

def rackup
  root.join(@options.fetch(:rackup) { DEFAULT_RACKUP })
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. Lotus assumes that all the commands are executed from there.

Returns:

  • (Pathname)

    application’s root

Since:

  • 0.2.0



201
202
203
# File 'lib/lotus/environment.rb', line 201

def root
  @root ||= Pathname.new(Dir.pwd)
end

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

  • (Lotus::Utils::Hash)

Since:

  • 0.1.0



337
338
339
340
341
342
343
344
345
# File 'lib/lotus/environment.rb', line 337

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