Class: Foobara::LoadDotenv
- Inherits:
-
Object
- Object
- Foobara::LoadDotenv
- Defined in:
- foobara-dotenv-loader-0.0.3/src/load_dotenv.rb
Defined Under Namespace
Classes: EnvFile
Constant Summary collapse
- FILE_REGEX =
/\A\.env((?:\.\w+)*?)(.local)?\z/
Instance Attribute Summary collapse
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#env ⇒ Object
Returns the value of attribute env.
-
#env_files ⇒ Object
Returns the value of attribute env_files.
-
#env_files_to_apply ⇒ Object
Returns the value of attribute env_files_to_apply.
Class Method Summary collapse
Instance Method Summary collapse
- #apply_env_files ⇒ Object
- #determine_env_files_to_apply ⇒ Object
- #execute ⇒ Object
-
#initialize(env: ENV["DOTENV_ENV"] || ENV["FOOBARA_ENV"] || "development", dir: Dir.pwd) ⇒ LoadDotenv
constructor
Might be nice if we could grab the directory of the calling code instead of using Dir.pwd but not sure.
- #parse_env_file_names ⇒ Object
- #sort_env_files ⇒ Object
Constructor Details
#initialize(env: ENV["DOTENV_ENV"] || ENV["FOOBARA_ENV"] || "development", dir: Dir.pwd) ⇒ LoadDotenv
Might be nice if we could grab the directory of the calling code instead of using Dir.pwd but not sure
14 15 16 17 18 19 20 21 22 23 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 14 def initialize(env: ENV["DOTENV_ENV"] || ENV["FOOBARA_ENV"] || "development", dir: Dir.pwd) unless env # :nocov: raise ArgumentError, "env must be provided" # :nocov: end self.env = env.to_s self.dir = dir end |
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
11 12 13 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 11 def dir @dir end |
#env ⇒ Object
Returns the value of attribute env.
11 12 13 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 11 def env @env end |
#env_files ⇒ Object
Returns the value of attribute env_files.
11 12 13 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 11 def env_files @env_files end |
#env_files_to_apply ⇒ Object
Returns the value of attribute env_files_to_apply.
11 12 13 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 11 def env_files_to_apply @env_files_to_apply end |
Class Method Details
.run! ⇒ Object
4 5 6 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 4 def run!(...) new(...).execute end |
Instance Method Details
#apply_env_files ⇒ Object
73 74 75 76 77 78 79 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 73 def apply_env_files unless env_files_to_apply.empty? Dir.chdir(dir) do Dotenv.load!(*env_files_to_apply.map(&:file_name)) end end end |
#determine_env_files_to_apply ⇒ Object
51 52 53 54 55 56 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 51 def determine_env_files_to_apply self.env_files_to_apply = env_files.select do |env_file| envs = env_file.envs envs.nil? || envs.include?(env) end end |
#execute ⇒ Object
25 26 27 28 29 30 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 25 def execute parse_env_file_names determine_env_files_to_apply sort_env_files apply_env_files end |
#parse_env_file_names ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 34 def parse_env_file_names self.env_files = [] Dir.entries(dir).each do |file_name| match = file_name.match(FILE_REGEX) next unless match envs = match[1] envs = envs[1..]&.split(".") if envs is_local = match[2] env_files << EnvFile.new(file_name, envs, is_local) end end |
#sort_env_files ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'foobara-dotenv-loader-0.0.3/src/load_dotenv.rb', line 58 def sort_env_files max_envs = env_files_to_apply.map { |env_file| env_file.envs&.length }.compact.max || 0 env_files_to_apply.sort_by! do |env_file| envs = env_file.envs || [] envs_length = envs.length [ env_file.is_local ? 0 : 1, max_envs - envs_length + 1, envs.sort ] end end |