Module: Kamaze::Project::Concern::Env

Included in:
Kamaze::Project
Defined in:
lib/kamaze/project/concern/env.rb

Overview

Load dotenv file

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments, such as resource handles for databases or credentials for external services should be extracted from the code into environment variables.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/kamaze/project/concern/env.rb', line 29

def included(base)
  base.class_eval <<-"ACCESSORS", __FILE__, __LINE__ + 1
    protected

    attr_writer :env_loaded
  ACCESSORS
end

Instance Method Details

#env_load(**options) ⇒ self (protected)

Load .env file (and store result)

Returns:

  • (self)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kamaze/project/concern/env.rb', line 50

def env_load(**options)
  options = {
    # @formatter:off
    pwd: Pathname.new(options[:pwd] || Dir.pwd).realpath,
    file: '.env',
    # @formatter:on
  }.merge(options)

  # @todo load different (or additionnal) files depending on env/mode
  [Pathname.new(options.fetch(:pwd)).join(options.fetch(:file))].each do |file|
    env_loaded.merge!(Dotenv.load(file))
  end

  self
end

#env_loadedHash

Loaded environment

Returns:

  • (Hash)


41
42
43
# File 'lib/kamaze/project/concern/env.rb', line 41

def env_loaded
  @env_loaded ||= {}
end