Class: Dip::Environment
- Inherits:
- 
      Object
      
        - Object
- Dip::Environment
 
- Defined in:
- lib/dip/environment.rb
Constant Summary collapse
- VAR_REGEX =
- /\$\{?(?<var_name>[a-zA-Z_][a-zA-Z0-9_]*)\}?/.freeze 
- SPECIAL_VARS =
- %i[os work_dir_rel_path current_user].freeze 
Instance Attribute Summary collapse
- 
  
    
      #vars  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute vars. 
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(key, value) ⇒ Object
- #fetch(name, &block) ⇒ Object
- 
  
    
      #initialize(default_vars)  ⇒ Environment 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Environment. 
- #interpolate(value) ⇒ Object (also: #replace)
- #merge(new_vars) ⇒ Object
Constructor Details
#initialize(default_vars) ⇒ Environment
Returns a new instance of Environment.
| 12 13 14 15 16 | # File 'lib/dip/environment.rb', line 12 def initialize(default_vars) @vars = {} merge(default_vars || {}) end | 
Instance Attribute Details
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
| 10 11 12 | # File 'lib/dip/environment.rb', line 10 def vars @vars end | 
Instance Method Details
#[](name) ⇒ Object
| 25 26 27 | # File 'lib/dip/environment.rb', line 25 def [](name) vars.fetch(name) { ENV[name] } end | 
#[]=(key, value) ⇒ Object
| 33 34 35 | # File 'lib/dip/environment.rb', line 33 def []=(key, value) @vars[key] = value end | 
#fetch(name, &block) ⇒ Object
| 29 30 31 | # File 'lib/dip/environment.rb', line 29 def fetch(name, &block) vars.fetch(name) { ENV.fetch(name, &block) } end | 
#interpolate(value) ⇒ Object Also known as: replace
| 37 38 39 40 41 42 43 44 45 46 47 | # File 'lib/dip/environment.rb', line 37 def interpolate(value) value.gsub(VAR_REGEX) do |match| var_name = Regexp.last_match[:var_name] if special_vars.key?(var_name) fetch(var_name) { send(special_vars[var_name]) } else fetch(var_name) { match } end end end | 
#merge(new_vars) ⇒ Object
| 18 19 20 21 22 23 | # File 'lib/dip/environment.rb', line 18 def merge(new_vars) new_vars.each do |key, value| key = key.to_s @vars[key] = ENV.fetch(key) { interpolate(value.to_s) } end end |