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_args, #configure_with_block, #configure_with_file, #disable_state_saves!, #enable_state_saves!, #method_missing, #save_state!, #save_state?, #set, #to_h

Constructor Details

#initialize(namespace = nil, **options, &block) ⇒ EnvConfig

Returns a new instance of EnvConfig.



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

def initialize(namespace=nil, **options, &block)
  @namespace = namespace ? "#{namespace.to_s.underscore.upcase.gsub(/_$/, '')}_" : namespace
  super(**options, &block)
end

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

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



14
15
16
17
18
# File 'lib/canfig/env_config.rb', line 14

def env(key, default=nil, &block)
  key = key.to_s.underscore.upcase
  key = key.gsub(/^#{namespace}/, '') if namespace
  super("#{namespace}#{key}", default, &block)
end

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



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

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