Module: Configatron::Integrations::Rails

Defined in:
lib/configatron/integrations/rails.rb

Overview

Helpful for making using configatron with Rails even easier!

To get started you can use the generator to generate the necessary stub files.

$ rails g configatron:install

Class Method Summary collapse

Class Method Details

.init(root = nil, env = nil) ⇒ Object

Loads configatron files in the following order:

Example:

<Rails.root>/config/configatron/defaults.rb
<Rails.root>/config/configatron/<Rails.env>.rb
# optional:
<Rails.root>/config/configatron/<Rails.env>/defaults.rb
<Rails.root>/config/configatron/<Rails.env>/bar.rb
<Rails.root>/config/configatron/<Rails.env>/foo.rb

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/configatron/integrations/rails.rb', line 19

def self.init(root = nil, env = nil)
  base_dir = root
  if root.nil?
    root = defined?(Rails) ? ::Rails.root : FileUtils.pwd
    base_dir = File.expand_path(File.join(root, 'config', 'configatron'))
  end

  if env.nil?
    env = defined?(Rails) ? ::Rails.env : 'development'
  end

  config_files = []

  config_files << File.join(base_dir, 'defaults.rb')
  config_files << File.join(base_dir, "#{env}.rb")

  env_dir = File.join(base_dir, env)
  config_files << File.join(env_dir, 'defaults.rb')

  Dir.glob(File.join(env_dir, '*.rb')).sort.each do |f|
    config_files << f
  end

  config_files.collect! {|config| File.expand_path(config)}.uniq!

  config_files.each do |config|
    if File.exists?(config)
      # puts "Configuration: #{config}"
      require config
    end
  end
end