Class: Canfig::EnvConfig

Inherits:
OpenConfig show all
Defined in:
lib/canfig/env_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from OpenConfig

#allowed?, #set

Methods inherited from Config

#[], #[]=, #allowed?, #changed, #changed?, #clear, #configure, #configure_with_block, #configure_with_file, #disable_state_saves!, #enable_state_saves!, #method_missing, #save_state!, #save_state?, #set, #state, #thread_configs, #to_h

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Canfig::Config

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



3
4
5
# File 'lib/canfig/env_config.rb', line 3

def namespace
  @namespace
end

Instance Method Details

#configure_with_args(argh) ⇒ Object



5
6
7
8
9
# File 'lib/canfig/env_config.rb', line 5

def configure_with_args(argh)
  save_state! do
    argh.symbolize_keys.each { |key,val| env(key, val) }
  end
end

#env(key, default = nil, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/canfig/env_config.rb', line 15

def env(key, default=nil, &block)
  @state[key.to_sym] ||= begin
    key = key.to_s.underscore.upcase
    key = key.gsub(/^#{namespace}/, '') if namespace
    val = ENV.fetch("#{namespace}#{key}", default, &block)
    val = default if val.blank?
    val = env(val, default, &block) if val && ENV.key?(val.to_s)
    val = eval(val) if val == 'true' || val == 'false'
    val
  end
end

#get(key, default = nil, &block) ⇒ Object



11
12
13
# File 'lib/canfig/env_config.rb', line 11

def get(key, default=nil, &block)
  super || env(key, default, &block)
end