Class: Canfig::EnvConfig
- Inherits:
-
OpenConfig
- Object
- Config
- OpenConfig
- Canfig::EnvConfig
- Defined in:
- lib/canfig/env_config.rb
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
- #env(key, default = nil, &block) ⇒ Object
- #get(key, default = nil, &block) ⇒ Object
-
#initialize(namespace = nil, **attributes, &block) ⇒ EnvConfig
constructor
A new instance of EnvConfig.
Methods inherited from OpenConfig
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, **attributes, &block) ⇒ EnvConfig
Returns a new instance of EnvConfig.
5 6 7 8 9 10 11 |
# File 'lib/canfig/env_config.rb', line 5 def initialize(namespace=nil, **attributes, &block) @namespace = namespace ? "#{namespace.to_s.underscore.upcase.gsub(/_$/, '')}_" : namespace super(*attributes.keys, &block) attributes.each do |key,val| env(key, val) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Canfig::Config
Instance Attribute Details
#namespace ⇒ Object (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
17 18 19 20 21 22 23 24 |
# File 'lib/canfig/env_config.rb', line 17 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 && ENV.key?(val.to_s) ? env(val, default, &block) : val end end |
#get(key, default = nil, &block) ⇒ Object
13 14 15 |
# File 'lib/canfig/env_config.rb', line 13 def get(key, default=nil, &block) super || env(key, default, &block) end |