Skip to content

Commit be9e52d

Browse files
committed
pre-parse out -e or --environment for use in use statements
1 parent de10ada commit be9e52d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

‎examples/env_use_statements.rb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
$:<< '../lib' << 'lib'
3+
4+
require'goliath'
5+
require'yajl'
6+
7+
classEnvUseStatements < Goliath::API
8+
ifGoliath.dev?
9+
useGoliath::Rack::Render,'json'
10+
elsifGoliath.prod?
11+
useGoliath::Rack::Render,'xml'
12+
end
13+
14+
defresponse(env)
15+
[200,{},{'Test'=>'Response'}]
16+
end
17+
end

‎lib/goliath/application.rb‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
require'goliath/runner'
33
require'goliath/rack'
44

5+
# Pre-load the goliath environment so it's available as we try to parse the class.
6+
# This means we can use Goliath.dev? or Goliath.prod? in the use statements.
7+
#
8+
# Note, as implmented, you have to have -e as it's own flag, you can't do -sve dev
9+
# as it won't pickup the e flag.
10+
env=ENV['RACK_ENV']
11+
env ||= begin
12+
if((i=ARGV.index('-e')) || (i=ARGV.index('--environment')))
13+
ARGV[i + 1]
14+
end
15+
end
16+
Goliath.env=envifenv
17+
518
moduleGoliath
619
# The main execution class for Goliath. This will execute in the at_exit
720
# handler to run the server.
@@ -44,14 +57,14 @@ def self.app_file
4457
c= $0 if !c || c.empty?
4558
c
4659
end
47-
60+
4861
# Returns the userland class which inherits the Goliath API
4962
#
5063
# @return [String] The app class
5164
defself.app_class
5265
@app_class
5366
end
54-
67+
5568
# Sets the userland class that should use the Goliath API
5669
#
5770
# @param app_class [String|Symbol|Constant] The new app class

0 commit comments

Comments
(0)