Sha256: 412f9b6fe9cac173ff40fb63c30558fd6775d546f9753292c50d42b607fdd343

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
envvy-1.0.3 lib/envvy/railtie.rb