Skip to content

Commit

Permalink
pre-parse out -e or --environment for use in use statements
Browse files Browse the repository at this point in the history
  • Loading branch information
dj2 committed Jul 13, 2011
1 parent de10ada commit be9e52d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 17 additions & 0 deletions examples/env_use_statements.rb
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
$:<< '../lib' << 'lib'

require 'goliath'
require 'yajl'

class EnvUseStatements < Goliath::API
if Goliath.dev?
use Goliath::Rack::Render, 'json'
elsif Goliath.prod?
use Goliath::Rack::Render, 'xml'
end

def response(env)
[200, {}, {'Test' => 'Response'}]
end
end
17 changes: 15 additions & 2 deletions lib/goliath/application.rb
Expand Up @@ -2,6 +2,19 @@
require 'goliath/runner'
require 'goliath/rack'

# Pre-load the goliath environment so it's available as we try to parse the class.
# This means we can use Goliath.dev? or Goliath.prod? in the use statements.
#
# Note, as implmented, you have to have -e as it's own flag, you can't do -sve dev
# as it won't pickup the e flag.
env = ENV['RACK_ENV']
env ||= begin
if ((i = ARGV.index('-e')) || (i = ARGV.index('--environment')))
ARGV[i + 1]
end
end
Goliath.env = env if env

module Goliath
# The main execution class for Goliath. This will execute in the at_exit
# handler to run the server.
Expand Down Expand Up @@ -44,14 +57,14 @@ def self.app_file
c = $0 if !c || c.empty?
c
end

# Returns the userland class which inherits the Goliath API
#
# @return [String] The app class
def self.app_class
@app_class
end

# Sets the userland class that should use the Goliath API
#
# @param app_class [String|Symbol|Constant] The new app class
Expand Down

0 comments on commit be9e52d

Please sign in to comment.