module Kamaze::Project::Concern::Env

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.

@see github.com/bkeepers/dotenv @see 12factor.net/config

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

env_loaded() click to toggle source

Loaded environment

@return [Hash]

# File lib/kamaze/project/concern/env.rb, line 41
def env_loaded
  @env_loaded ||= {}
end

Protected Instance Methods

env_load(**options) click to toggle source

Load “.env“ file (and store result)

@return [self]

# 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