Class: Enfig

Inherits:
Object
  • Object
show all
Defined in:
lib/enfig.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_overwriteObject 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

#envObject

Returns the value of attribute env.



5
6
7
# File 'lib/enfig.rb', line 5

def env
  @env
end

#filesObject

Returns the value of attribute files.



5
6
7
# File 'lib/enfig.rb', line 5

def files
  @files
end

#rootObject

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

#configObject



17
18
19
# File 'lib/enfig.rb', line 17

def config
  @config ||= load_config
end

#load_configObject



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