Class: Config::Sources::EnvSource
- Inherits:
- 
      Object
      
        - Object
- Config::Sources::EnvSource
 
- Defined in:
- lib/config/sources/env_source.rb
Overview
Allows settings to be loaded from a “flat” hash with string keys, like ENV.
Instance Attribute Summary collapse
- 
  
    
      #converter  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute converter. 
- 
  
    
      #parse_values  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute parse_values. 
- 
  
    
      #prefix  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute prefix. 
- 
  
    
      #separator  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute separator. 
Instance Method Summary collapse
- 
  
    
      #initialize(env, prefix: Config.env_prefix || Config.const_name, separator: Config.env_separator, converter: Config.env_converter, parse_values: Config.env_parse_values)  ⇒ EnvSource 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of EnvSource. 
- #load ⇒ Object
Constructor Details
#initialize(env, prefix: Config.env_prefix || Config.const_name, separator: Config.env_separator, converter: Config.env_converter, parse_values: Config.env_parse_values) ⇒ EnvSource
Returns a new instance of EnvSource.
| 10 11 12 13 14 15 16 17 18 19 20 | # File 'lib/config/sources/env_source.rb', line 10 def initialize(env, prefix: Config.env_prefix || Config.const_name, separator: Config.env_separator, converter: Config.env_converter, parse_values: Config.env_parse_values) @env = env @prefix = prefix.to_s.split(separator) @separator = separator @converter = converter @parse_values = parse_values end | 
Instance Attribute Details
#converter ⇒ Object (readonly)
Returns the value of attribute converter.
| 7 8 9 | # File 'lib/config/sources/env_source.rb', line 7 def converter @converter end | 
#parse_values ⇒ Object (readonly)
Returns the value of attribute parse_values.
| 8 9 10 | # File 'lib/config/sources/env_source.rb', line 8 def parse_values @parse_values end | 
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
| 5 6 7 | # File 'lib/config/sources/env_source.rb', line 5 def prefix @prefix end | 
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
| 6 7 8 | # File 'lib/config/sources/env_source.rb', line 6 def separator @separator end | 
Instance Method Details
#load ⇒ Object
| 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | # File 'lib/config/sources/env_source.rb', line 22 def load return {} if @env.nil? || @env.empty? hash = Hash.new @env.each do |variable, value| keys = variable.to_s.split(separator) next if keys.shift(prefix.size) != prefix keys.map! { |key| case converter when :downcase then key.downcase when nil then key else raise "Invalid ENV variables name converter: #{converter}" end } leaf = keys[0...-1].inject(hash) { |h, key| h[key] ||= {} } unless leaf.is_a?(Hash) conflicting_key = (prefix + keys[0...-1]).join(separator) raise "Environment variable #{variable} conflicts with variable #{conflicting_key}" end leaf[keys.last] = parse_values ? __value(value) : value end hash end |