Skip to content

Commit

Permalink
Goliath.env should throw an exception if unset
Browse files Browse the repository at this point in the history
  • Loading branch information
gregwebs committed May 15, 2011
1 parent bb8d049 commit b7e4410
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
26 changes: 19 additions & 7 deletions lib/goliath/goliath.rb
Expand Up @@ -6,19 +6,31 @@
module Goliath
module_function

@env = :development
ENVIRONMENTS = [:development, :production, :test]

# Retrieves the current goliath environment
#
# @return [String] the current environment
def env
@env
@env or fail "environment has not been set"
end

# Sets the current goliath environment
#
# @param [Symbol] env the environment symbol of [development|production|test]
def env= e
es = e.to_sym
if ENVIRONMENTS.include? es
@env = es
else
fail "did not recognize environment: #{e}, expected one of: #{ENVIRONMENTS.join(', ')}"
end
end

# Sets the current goliath environment
#
# @param [String] env the environment string of [dev|prod|test]
def env=(env)
def short_env=(env)
case(env.to_s)
when 'dev' then @env = :development
when 'prod' then @env = :production
Expand All @@ -30,20 +42,20 @@ def env=(env)
#
# @return [Boolean] true if current environemnt is production, false otherwise
def prod?
@env == :production
env == :production
end

# Determines if we are in the development environment
#
# @return [Boolean] true if current environemnt is development, false otherwise
def dev?
@env == :development
env == :development
end

# Determines if we are in the test environment
#
# @return [Boolean] true if current environemnt is test, false otherwise
def test?
@env == :test
env == :test
end
end
end
6 changes: 4 additions & 2 deletions lib/goliath/runner.rb
Expand Up @@ -63,6 +63,7 @@ class Runner
def initialize(argv, api)
api.options_parser(options_parser, options) if api
options_parser.parse!(argv)
Goliath.short_env = options.delete(:env)

@api = api
@address = options.delete(:address)
Expand All @@ -88,7 +89,8 @@ def options_parser

:daemonize => false,
:verbose => false,
:log_stdout => false
:log_stdout => false,
:env => 'dev',
}

@options_parser ||= OptionParser.new do |opts|
Expand All @@ -97,7 +99,7 @@ def options_parser
opts.separator ""
opts.separator "Server options:"

opts.on('-e', '--environment NAME', "Set the execution environment (prod, dev or test) (default: #{Goliath.env})") { |val| Goliath.env = val }
opts.on('-e', '--environment NAME', "Set the execution environment (prod, dev or test) (default: #{@options[:env]})") { |val| @options[:env] = val }

opts.on('-a', '--address HOST', "Bind to HOST address (default: #{@options[:address]})") { |addr| @options[:address] = addr }
opts.on('-p', '--port PORT', "Use PORT (default: #{@options[:port]})") { |port| @options[:port] = port.to_i }
Expand Down
2 changes: 1 addition & 1 deletion lib/goliath/test_helper.rb
Expand Up @@ -25,7 +25,7 @@ module Goliath
#
module TestHelper
def self.included(mod)
Goliath.env = 'test'
Goliath.short_env = 'test'
end

# Launches an instance of a given API server. The server
Expand Down
8 changes: 4 additions & 4 deletions spec/integration/reloader_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

Goliath.env = 'dev'
Goliath.env = :development

class ReloaderDev < Goliath::API
use Goliath::Rack::Params
Expand All @@ -17,8 +17,8 @@ class ReloaderProd < Goliath::API
describe "Reloader" do
let(:err) { Proc.new { fail "API request failed" } }

before(:each) { Goliath.env = "dev" }
after(:each) { Goliath.env = "test" }
before(:each) { Goliath.env = :development }
after(:each) { Goliath.env = :test }

def count(klass)
cnt = 0
Expand All @@ -37,7 +37,7 @@ def count(klass)
end

it "doesn't add in prod mode" do
Goliath.env = "prod"
Goliath.env = :production
count(ReloaderProd).should == 0
end
end
8 changes: 4 additions & 4 deletions spec/unit/server_spec.rb
Expand Up @@ -106,28 +106,28 @@
context 'config parsing' do
context 'environment' do
it 'executes the block if the environment matches the provided string' do
Goliath.env = 'dev'
Goliath.env = :development
block_run = false
@s.environment('development') { block_run = true }
block_run.should be_true
end

it 'does not execute the block if the environment does not match' do
Goliath.env = 'dev'
Goliath.env = :development
block_run = false
@s.environment('test') { block_run = true }
block_run.should be_false
end

it 'accepts an array of environments' do
Goliath.env = 'dev'
Goliath.env = :development
block_run = false
@s.environment(['development', 'test']) { block_run = true }
block_run.should be_true
end

it 'does not run the block if the environment is not in the array' do
Goliath.env = 'prod'
Goliath.env = :production
block_run = false
@s.environment(['development', 'test']) { block_run = true }
block_run.should be_false
Expand Down

0 comments on commit b7e4410

Please sign in to comment.