# lib/frecon/database.rb
#
# Copyright (C) 2014 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
#
# This file is part of FReCon, an API for scouting at FRC Competitions, which is
# licensed under the MIT license.  You should have received a copy of the MIT
# license with this program.  If not, please see
# <http://opensource.org/licenses/MIT>.

require "logger"
require "mongoid"

require "tempfile"
require "yaml"

require "frecon/models"

module FReCon
	class Database
		def self.setup(environment, mongoid_hash = nil)
			if mongoid_hash.is_a?(Hash)
				mongoid_tempfile = Tempfile.new("FReCon")
				mongoid_tempfile.write(mongoid_hash.to_yaml)
				mongoid_tempfile.rewind

				Mongoid.load!(mongoid_tempfile.path, environment)
			else
				Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), environment)
			end
			
			Mongoid.logger.level = Logger::DEBUG
			Mongoid.logger = Logger.new($stdout)

			Moped.logger.level = Logger::DEBUG
			Moped.logger = Logger.new($stdout)
		end
	end
end

# lib/frecon/database.rb
#
# Copyright (C) 2014 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
#
# This file is part of FReCon, an API for scouting at FRC Competitions, which is
# licensed under the MIT license.  You should have received a copy of the MIT
# license with this program.  If not, please see
# <http://opensource.org/licenses/MIT>.

require "logger"
require "mongoid"

require "frecon/models"

module FReCon
	class Database
		def self.setup(environment)
			Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), environment)
			
			Mongoid.logger.level = Logger::DEBUG
			Mongoid.logger = Logger.new($stdout)

			Moped.logger.level = Logger::DEBUG
			Moped.logger = Logger.new($stdout)
		end
	end
end

--- 
+++ 
@@ -10,12 +10,23 @@
 require "logger"
 require "mongoid"
 
+require "tempfile"
+require "yaml"
+
 require "frecon/models"
 
 module FReCon
 	class Database
-		def self.setup(environment)
-			Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), environment)
+		def self.setup(environment, mongoid_hash = nil)
+			if mongoid_hash.is_a?(Hash)
+				mongoid_tempfile = Tempfile.new("FReCon")
+				mongoid_tempfile.write(mongoid_hash.to_yaml)
+				mongoid_tempfile.rewind
+
+				Mongoid.load!(mongoid_tempfile.path, environment)
+			else
+				Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), environment)
+			end
 			
 			Mongoid.logger.level = Logger::DEBUG
 			Mongoid.logger = Logger.new($stdout)
FReCon::Database.setup: Enable custom Hash for configuration.

If a Hash is given, it is converted to yaml (via to_yaml) and written to a Tempfile. This Tempfile's path is passed to the Mongoid.load! method instead of the path to the "mongoid.yml" file.  By default (If no `mongoid_hash' is given), FReCon::Database.setup will give Mongoid.load! the "mongoid.yml" file in the "lib/frecon/" directory's path.
