Class: Enfig
- Inherits:
-
Object
- Object
- Enfig
- Defined in:
- lib/enfig.rb
Instance Attribute Summary collapse
-
#enable_overwrite ⇒ Object
(also: #overwrite?)
Returns the value of attribute enable_overwrite.
-
#env ⇒ Object
Returns the value of attribute env.
-
#files ⇒ Object
Returns the value of attribute files.
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #config ⇒ Object
-
#initialize(args = {}) ⇒ Enfig
constructor
A new instance of Enfig.
- #load_config ⇒ Object
- #update_env! ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Enfig
Returns a new instance of Enfig.
9 10 11 12 13 14 15 |
# File 'lib/enfig.rb', line 9 def initialize(args = {}) @config = nil @env = args[:env] || 'development' @root = args[:root] || '' @files = args[:files] || [args[:file]].compact @enable_overwrite = args[:overwrite] == false ? false : true end |
Instance Attribute Details
#enable_overwrite ⇒ Object Also known as: overwrite?
Returns the value of attribute enable_overwrite.
5 6 7 |
# File 'lib/enfig.rb', line 5 def enable_overwrite @enable_overwrite end |
#env ⇒ Object
Returns the value of attribute env.
5 6 7 |
# File 'lib/enfig.rb', line 5 def env @env end |
#files ⇒ Object
Returns the value of attribute files.
5 6 7 |
# File 'lib/enfig.rb', line 5 def files @files end |
#root ⇒ Object
Returns the value of attribute root.
5 6 7 |
# File 'lib/enfig.rb', line 5 def root @root end |
Class Method Details
.update!(args = {}) ⇒ Object
42 43 44 45 46 |
# File 'lib/enfig.rb', line 42 def self.update!(args = {}) enfig = Enfig.new(args) enfig.update_env! enfig end |
Instance Method Details
#[](key) ⇒ Object
21 22 23 |
# File 'lib/enfig.rb', line 21 def [](key) config[key] end |
#config ⇒ Object
17 18 19 |
# File 'lib/enfig.rb', line 17 def config @config ||= load_config end |
#load_config ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/enfig.rb', line 25 def load_config conf = {} files.each do |file| name = File.basename(file, '.yml').to_s.downcase.gsub(/[^a-z0-9_]/i, '_') conf[name.to_sym] = load_yaml(file) end conf end |
#update_env! ⇒ Object
36 37 38 39 40 |
# File 'lib/enfig.rb', line 36 def update_env! load_env_hash(config).each do |k, v| ENV[k] = v.to_s if ENV[k].nil? || ENV[k] == '' || overwrite? end end |