Sha256: cb40e8cd3494ebf5afa7a9971b05d3df05d8521bfce8a545dbf40e3c45135d05

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require_relative "rails_env_helper/version"

module RailsEnvHelper
  class << self
    def initialize_env_variables(config_file_path)
      begin
        env_vars = load_env_variables(config_file_path)
        set_env_variables(env_vars)
        return "Environment variables initialized successfully."
      rescue Errno::ENOENT
        return "Error: Configuration file not found at #{config_file_path}."
      rescue Psych::SyntaxError
        return "Error: Invalid YAML syntax in the configuration file."
      rescue StandardError => e
        return "Error: #{e.message}"
      end
    end

    def get_env(variable_name)
      if Rails.env.production?
        ENV[variable_name]
      else
        Rails.application.credentials[variable_name]
      end
    end

    private

    def load_env_variables(config_file_path)
      env_vars_file = Rails.root.join(config_file_path)
      YAML.load_file(env_vars_file)
    end

    def set_env_variables(env_vars)
      Rails.application.configure do
        env_vars[Rails.env].each do |key, value|
          ENV[key] ||= value.to_s
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_env_helper-0.1.3 lib/rails_env_helper.rb
rails_env_helper-0.1.2 lib/rails_env_helper.rb