Skip to content

Commit

Permalink
Read config settings from ENV in preference to config.yml.
Browse files Browse the repository at this point in the history
  • Loading branch information
gma committed Jun 16, 2010
1 parent 7d9239a commit 4ffa508
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 61 deletions.
63 changes: 25 additions & 38 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,40 @@
module Nesta
class Config
@yaml = nil


@per_environment_settings = %w[cache content google_analytics_code]
@simple_settings = %w[
title subtitle description keywords theme disqus_short_name]
@all_settings = @per_environment_settings + @simple_settings

class << self
attr_accessor :all_settings, :per_environment_settings, :simple_settings
attr_accessor :yaml
end

def self.cache
if Sinatra::Application.environment == :test
false
else
get(environment)["cache"] || false

Nesta::Config.per_environment_settings.each do |setting|
define_method(setting) do
ENV["NESTA_#{setting.upcase}"] || get(environment)[setting]
end
end

Nesta::Config.simple_settings.each do |setting|
define_method(setting) do
ENV["NESTA_#{setting.upcase}"] || configuration[setting]
end
end
end

def self.title
configuration["title"]
end

def self.subtitle
configuration["subtitle"]
end

def self.description
configuration["description"]
end

def self.keywords
configuration["keywords"]
end

def self.author
configuration["author"]
end

def self.theme
configuration["theme"]
end

def self.google_analytics_code
get(environment)["google_analytics_code"]
end

def self.disqus_short_name
configuration["disqus_short_name"]
environment_config = {}
%w[name uri email].each do |setting|
variable = "NESTA_AUTHOR__#{setting.upcase}"
ENV[variable] && environment_config[setting] = ENV[variable]
end
environment_config.empty? ? configuration["author"] : environment_config
end

def self.content_path(basename = nil)
get_path(get(environment)["content"], basename)
get_path(content, basename)
end

def self.page_path(basename = nil)
Expand Down
63 changes: 63 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require File.join(File.dirname(__FILE__), "config_spec_helpers")
require File.join(File.dirname(__FILE__), "spec_helper")

describe "Config" do
include ConfigSpecHelper

after(:each) do
ENV.keys.each { |variable| ENV.delete(variable) if variable =~ /NESTA_/ }
end

describe "when only defined in config.yml" do
it "should read simple configuration from config.yml" do
Nesta::Config.simple_settings.each do |setting|
stub_config_key(setting, "#{setting} in config.yml")
Nesta::Config.send(setting).should == "#{setting} in config.yml"
end
end

it "should read author config from config.yml" do
@author_name = "Barry Chuckle"
stub_config_key("author", { "name" => @author_name })
Nesta::Config.author["name"].should == @author_name
Nesta::Config.author["email"].should be_nil
end

it "should read environment specific config from config.yml" do
stub_env_config_key("cache", true)
Nesta::Config.cache.should be_true
end
end

describe "when simple settings defined in ENV" do
before(:each) do
settings = Nesta::Config.simple_settings + \
Nesta::Config.per_environment_settings
settings.each do |setting|
variable = "NESTA_#{setting.upcase}"
ENV[variable] = "#{setting} from ENV"
end
end

it "should override config.yml" do
assert ENV.keys.grep(/^NESTA_/).size > 0
ENV.keys.grep(/^NESTA_/).each do |variable|
method = variable.sub("NESTA_", "").downcase
Nesta::Config.send(method).should == ENV[variable]
end
end
end

describe "when nested author settings defined in ENV" do
before(:each) do
@author_name = "Paul Chuckle"
ENV["NESTA_AUTHOR__NAME"] = @author_name
end

it "should override author in config.yml" do
Nesta::Config.author["name"].should == @author_name
Nesta::Config.author["email"].should be_nil
Nesta::Config.author["uri"].should be_nil
end
end
end
28 changes: 28 additions & 0 deletions spec/config_spec_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module ConfigSpecHelper

FIXTURE_DIR = File.join(File.dirname(__FILE__), "fixtures")

def initialise_config
@config = {}
Nesta::Config.stub!(:configuration).and_return(@config)
end

def stub_config_key(key, value)
initialise_config if @config.nil?
@config[key] = value
end

def stub_env_config_key(key, value)
initialise_config if @config.nil?
@config["test"] ||= {}
@config["test"][key] = value
end

def stub_configuration
stub_config_key("title", "My blog")
stub_config_key("subtitle", "about stuff")
stub_config_key("description", "great web site")
stub_config_key("keywords", "home, page")
stub_env_config_key("content", ConfigSpecHelper::FIXTURE_DIR)
end
end
27 changes: 4 additions & 23 deletions spec/model_factory.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
module ModelFactory

FIXTURE_DIR = File.join(File.dirname(__FILE__), "fixtures")

def stub_config_key(key, value)
@config ||= {}
@config[key] = value
end

def stub_env_config_key(key, value)
@config ||= {}
@config["test"] ||= {}
@config["test"][key] = value
end
require File.join(File.dirname(__FILE__), "config_spec_helpers")

def stub_configuration
stub_config_key("title", "My blog")
stub_config_key("subtitle", "about stuff")
stub_config_key("description", "great web site")
stub_config_key("keywords", "home, page")
stub_env_config_key("content", FIXTURE_DIR)
Nesta::Config.stub!(:configuration).and_return(@config)
end
module ModelFactory
include ConfigSpecHelper

def create_page(options)
extension = options[:ext] || :mdown
Expand Down Expand Up @@ -64,7 +45,7 @@ def delete_page(type, permalink, extension)
end

def remove_fixtures
FileUtils.rm_r(FIXTURE_DIR, :force => true)
FileUtils.rm_r(ConfigSpecHelper::FIXTURE_DIR, :force => true)
end

def create_content_directories
Expand Down

0 comments on commit 4ffa508

Please sign in to comment.