module RequireEnv

Constants

VERSION

Public Class Methods

application_environment() click to toggle source
# File lib/require_env.rb, line 55
def self.application_environment
  @application_environment ||= ENV['RAILS_ENV']
end
application_environment=(value) click to toggle source
# File lib/require_env.rb, line 59
def self.application_environment=(value)
  @application_environment = value
end
check(*files) click to toggle source
# File lib/require_env.rb, line 6
def self.check(*files)
  check_multiple_requirements(files.map do |file|
    requirements = requirements_from_file(file)
    env_requirements = requirements[application_environment] || requirements[fallback_environment]
    if env_requirements.nil?
      raise ArgumentError.new("#{file} does not define anything for environment: #{application_environment}")
    end
    [file, env_requirements]
  end)
end
check_multiple_requirements(multiple_requirements) click to toggle source
# File lib/require_env.rb, line 25
def self.check_multiple_requirements(multiple_requirements)
  missing_requirements = multiple_requirements.map do |file, requirements|
    missing = check_requirements(requirements)
    if missing.present?
      [file, missing]
    end
  end.compact

  if missing_requirements.present?
    raise RequireEnv::Missing.new(missing_requirements)
  end
end
check_requirements(requirements) click to toggle source
# File lib/require_env.rb, line 38
def self.check_requirements(requirements)
  requirements.map do |key, value|
    if ENV.has_key?(key) || ENV.has_key?(key.split('_IF_').first)
      next nil
    elsif value
      ENV[key] = value.to_s
      next nil
    elsif key.include?('_IF_')
      if (condition = ENV[key.split('_IF_').last]) && condition != 'false'
        next key.split('_IF_').first
      end
    else
      next key
    end
  end.compact
end
fallback_environment() click to toggle source
# File lib/require_env.rb, line 63
def self.fallback_environment
  @fallback_environment ||= nil
end
fallback_environment=(value) click to toggle source
# File lib/require_env.rb, line 66
def self.fallback_environment=(value)
  @fallback_environment = value
end
requirements_from_file(file) click to toggle source
# File lib/require_env.rb, line 17
def self.requirements_from_file(file)
  if File.exist?(file)
    SafeYAML.load(ERB.new(File.read(file)).result)
  else
    raise ArgumentError.new("File not found: #{file}")
  end
end