Class: Ace::Core::Organisms::EnvironmentManager
- Inherits:
-
Object
- Object
- Ace::Core::Organisms::EnvironmentManager
- Defined in:
- lib/ace/core/organisms/environment_manager.rb
Overview
Complete environment variable management
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
Instance Method Summary collapse
-
#get(key, default = nil) ⇒ String, Object
Get specific environment variable with fallback.
-
#initialize(root_path: Dir.pwd, config: nil) ⇒ EnvironmentManager
constructor
Initialize environment manager.
-
#key?(key) ⇒ Boolean
Check if variable exists.
-
#list_dotenv_files ⇒ Array<String>
List all .env files that would be loaded.
-
#load ⇒ Hash
Load environment variables based on configuration.
-
#save(filepath = ".env", keys: nil) ⇒ Hash
Save current environment to .env file.
-
#set(key, value) ⇒ String
Set environment variable.
Constructor Details
#initialize(root_path: Dir.pwd, config: nil) ⇒ EnvironmentManager
Initialize environment manager
17 18 19 20 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 17 def initialize(root_path: Dir.pwd, config: nil) @root_path = Ace::Support::Fs::Atoms::PathExpander.(root_path) @config = config || load_config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 12 def config @config end |
#root_path ⇒ Object (readonly)
Returns the value of attribute root_path.
12 13 14 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 12 def root_path @root_path end |
Instance Method Details
#get(key, default = nil) ⇒ String, Object
Get specific environment variable with fallback
64 65 66 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 64 def get(key, default = nil) ENV.fetch(key, default) end |
#key?(key) ⇒ Boolean
Check if variable exists
79 80 81 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 79 def key?(key) ENV.key?(key) end |
#list_dotenv_files ⇒ Array<String>
List all .env files that would be loaded
85 86 87 88 89 90 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 85 def list_dotenv_files return [] unless should_load_dotenv? dotenv_files = get_dotenv_files dotenv_files.map { |file| resolve_dotenv_path(file) }.compact end |
#load ⇒ Hash
Load environment variables based on configuration
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 25 def load return {} unless should_load_dotenv? dotenv_files = get_dotenv_files loaded_vars = {} dotenv_files.each do |file| filepath = resolve_dotenv_path(file) next unless filepath vars = Molecules::EnvLoader.load_file(filepath) loaded_vars.merge!(vars) if vars && !vars.empty? end # Set all loaded variables Molecules::EnvLoader.set_environment(loaded_vars, overwrite: false) end |
#save(filepath = ".env", keys: nil) ⇒ Hash
Save current environment to .env file
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 47 def save(filepath = ".env", keys: nil) filepath = File.join(root_path, filepath) unless Ace::Support::Fs::Atoms::PathExpander.absolute?(filepath) vars_to_save = if keys ENV.to_h.select { |k, _| keys.include?(k) } else ENV.to_h end Molecules::EnvLoader.save_file(vars_to_save, filepath) vars_to_save end |
#set(key, value) ⇒ String
Set environment variable
72 73 74 |
# File 'lib/ace/core/organisms/environment_manager.rb', line 72 def set(key, value) ENV[key] = value.to_s end |