Module: Inquisitive::Environment::Parser

Defined in:
lib/inquisitive/environment.rb

Class Method Summary collapse

Class Method Details

.[](var_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/inquisitive/environment.rb', line 58

def [](var_name)
  result = if ENV.has_key? var_name

    env_var = ENV[var_name]
    if env_var.include? ','
      env_var.split(',').map(&:strip)
    else
      env_var
    end

  elsif env_vars = can_find_env_keys_from(var_name)

    Hash[].tap do |hash|
      env_vars.each do |key|
        set_hash_value_of hash, key
      end
    end

  end

  replace_empty result
end

.can_find_env_keys_from(var_name) ⇒ Object



81
82
83
84
# File 'lib/inquisitive/environment.rb', line 81

def can_find_env_keys_from(var_name)
  found = env_keys_from(var_name)
  found.empty? ? nil : found
end

.env_keys_from(var_name) ⇒ Object



86
87
88
89
90
# File 'lib/inquisitive/environment.rb', line 86

def env_keys_from(var_name)
  ENV.keys.select do |key|
    key =~ /^#{var_name}__/
  end
end

.replace_empty(value) ⇒ Object



110
111
112
# File 'lib/inquisitive/environment.rb', line 110

def replace_empty(value)
  value == "" or value.nil? ? NilClass.new(nil) : value
end

.set_hash_value_of(hash, var) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/inquisitive/environment.rb', line 92

def set_hash_value_of(hash, var)
  keypath = var.split('__').map(&:downcase)
  keypath.shift
  hash.tap do |hash|
    keypath.reduce(hash) do |namespace, key|
      namespace[key] = if key == keypath.last
        replace_empty Inquisitive[Parser[var]]
      else
        if namespace[key].respond_to? :store
          namespace[key]
        else
          Hash.new
        end
      end
    end
  end
end