Class: AutoConfig::Base

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

Class Method Summary collapse

Class Method Details

.autoloadObject Also known as: reload



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/autoconfig.rb', line 49

def autoload
  wipe
  files = Dir.glob(File.join(path,pattern))
  begin
    old_verbose, $VERBOSE = $VERBOSE, nil

    files.each do |file|
      name = File.basename(file, pattern.gsub('*',''))
      next if name.match(ignored_filenames) || name.match(/-overrides/)

      constant_name = "#{name}Config".gsub('-','_').camelize.intern
      begin
        Object::const_set(constant_name, load(name))
        @autoset_constants ||= Set.new
        @autoset_constants << constant_name
      rescue StandardError => e
        @errors << <<-ERROR
          Error reading file #{file} into #{constant_name}.
          #{$!.inspect}
          #{$@}
          You can skip it by adding it to AUTOCONFIG_IGNORE.
        ERROR
      end
    end

    raise @errors.to_a.join( "\n" ) unless @errors.empty?

  ensure
    $VERBOSE = old_verbose
  end
end

.environmentObject



31
32
33
# File 'lib/autoconfig.rb', line 31

def environment
  ENV['AUTOCONFIG_ENV'] || ENV['APP_ENV'] || (rails? && Rails.env) || 'development'
end

.ignored_filenamesObject



39
40
41
42
# File 'lib/autoconfig.rb', line 39

def ignored_filenames
  names = ENV['AUTOCONFIG_IGNORE'] ? "database|" + ENV['AUTOCONFIG_IGNORE'].gsub(/\s/,'|') : 'database'
  Regexp.new(names)
end

.load(name) ⇒ Object



44
45
46
# File 'lib/autoconfig.rb', line 44

def load(name)
  HierarchicalConfig.load_config( name, path, environment )
end

.pathObject



27
28
29
# File 'lib/autoconfig.rb', line 27

def path
  ENV['AUTOCONFIG_PATH'] || File.expand_path('config',root)
end

.patternObject



23
24
25
# File 'lib/autoconfig.rb', line 23

def pattern
  ENV['AUTOCONFIG_PATTERN'] || '*.yml'
end

.rails?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/autoconfig.rb', line 35

def rails?
  Object::const_defined? "Rails"
end

.rootObject



18
19
20
21
# File 'lib/autoconfig.rb', line 18

def root
  rails_root = (rails? && (Rails.root || File.expand_path('../', ENV['BUNDLE_GEMFILE'])))
  ENV['AUTOCONFIG_ROOT'] || ENV['APP_ROOT'] || rails_root || Dir.pwd
end