Class: Ace::Core::Organisms::EnvironmentManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/core/organisms/environment_manager.rb

Overview

Complete environment variable management

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path: Dir.pwd, config: nil) ⇒ EnvironmentManager

Initialize environment manager

Parameters:

  • root_path (String) (defaults to: Dir.pwd)

    Project root path

  • config (Models::Config, nil) (defaults to: nil)

    Configuration to use



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.expand(root_path)
  @config = config || load_config
end

Instance Attribute Details

#configObject (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_pathObject (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

Parameters:

  • key (String)

    Variable name

  • default (Object) (defaults to: nil)

    Default value if not found

Returns:

  • (String, Object)

    Variable value or default



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

Parameters:

  • key (String)

    Variable name

Returns:

  • (Boolean)

    true 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_filesArray<String>

List all .env files that would be loaded

Returns:

  • (Array<String>)

    Paths to .env files



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

#loadHash

Load environment variables based on configuration

Parameters:

  • overwrite (Boolean)

    Whether to overwrite existing vars

Returns:

  • (Hash)

    Variables that were loaded



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

Parameters:

  • filepath (String) (defaults to: ".env")

    Path to save to

  • keys (Array<String>) (defaults to: nil)

    Specific keys to save (nil = all)

Returns:

  • (Hash)

    Variables that were saved



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

Parameters:

  • key (String)

    Variable name

  • value (String)

    Variable value

Returns:

  • (String)

    The value that was set



72
73
74
# File 'lib/ace/core/organisms/environment_manager.rb', line 72

def set(key, value)
  ENV[key] = value.to_s
end