From a1fa99bd4b10f839c4f296fcba06e13c212e6a32 Mon Sep 17 00:00:00 2001 From: Cory ODaniel Date: Fri, 16 May 2008 11:15:44 -0700 Subject: [PATCH] Merb Personalized Environments; Completed Merb#merge_env method, now accepts boolean parameter to optionally use the merged environments database connection --- lib/merb-core.rb | 42 +++++++++++++++++++++++++++++++++++- spec/public/core/merb_core_spec.rb | 11 +++++++++ 2 files changed, 52 insertions(+), 1 deletions(-) diff --git a/lib/merb-core.rb b/lib/merb-core.rb index 3aa4a5c..09075fd 100644 --- a/lib/merb-core.rb +++ b/lib/merb-core.rb @@ -13,6 +13,46 @@ module Merb module GlobalHelpers; end class << self + # Merge environment settings + # Can allow you to have a "localdev" that runs like your "development" + # OR + # A "staging" environment that runs like your "production" + # + # ==== Parameters + # env<~String>:: Environment to run like + # use_db<~Boolean>:: Should Merb use the merged environments DB connection + # Defaults to +false+ + def merge_env(env,use_db=false) + if Merb.environment_info.nil? + Merb.environment_info = { + :real_env => Merb.environment, + :merged_envs => [], + :db_env => Merb.environment + } + end + + #Only load if it hasn't been loaded + unless Merb.environment_info[:merged_envs].member? env + Merb.environment_info[:merged_envs] << env + + env_file = Merb.dir_for(:config) / "environments" / ("#{env}.rb") + if File.exists?(env_file) + load(env_file) + else + Merb.logger.warn! "Environment file does not exist! #{env_file}" + end + + end + + # Mark specific environment to load when ORM loads, + # if multiple environments are loaded, the last one + # with use_db as TRUE will be loaded + if use_db + Merb.environment_info[:db_env] = env + end + end + + # Startup Merb by setting up the Config and starting the server. # This is where Merb application environment and root path are set. # @@ -59,7 +99,7 @@ module Merb start_environment(Merb::Config.to_hash.merge(argv)) end - attr_accessor :environment, :load_paths, :adapter + attr_accessor :environment, :load_paths, :adapter, :environment_info alias :env :environment diff --git a/spec/public/core/merb_core_spec.rb b/spec/public/core/merb_core_spec.rb index d4ac9f4..5a210a4 100644 --- a/spec/public/core/merb_core_spec.rb +++ b/spec/public/core/merb_core_spec.rb @@ -30,5 +30,16 @@ describe "Merb.env helpers" do end end + it "should allow an environment to merge another environments settings" do + %w(development test production staging demo custom).each do |e| + + Merb.environment = e + Merb.start_environment + Merb.merge_env "some_other_env" + Merb.environment_info.nil?.should be_false + Merb.environment_info[:merged_envs].first.should == "some_other_env" + end + end + end \ No newline at end of file -- 1.5.2.4