require 'yaml' module Envvy class Railtie < Rails::Railtie attr_accessor :env class << self attr_accessor :config_path def config_path @config_path || File.join(Rails.root, "config", "env_vars.yml") end end config.before_initialize do self.get("config") do |config| config.each do |k, v| ENV[k.to_s.upcase] = v end end end config.before_configuration do set_env self.config_path if Rails.env.development? require 'filewatch/watch' conf_watcher = FileWatch::Watch.new conf_watcher.watch(self.config_path) Thread.new do conf_watcher.subscribe(1,5) do |status, path| set_env path end end end end def set_env file self.env = if File.exist?(file) then YAML.load(File.read(file)) else {} end end def get(key, &block) if block_given? yield self.env[key] else return self.env[key] end end end end