ghaki-env 2011.11.29.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README +23 -0
- data/lib/ghaki/env/app.rb +13 -0
- data/lib/ghaki/env/engine.rb +90 -0
- data/lib/ghaki/env/mixin.rb +18 -0
- data/spec/ghaki/env/app_spec.rb +23 -0
- data/spec/ghaki/env/engine_spec.rb +101 -0
- data/spec/ghaki/env/mixin_spec.rb +34 -0
- data/spec/spec_helper.rb +6 -0
- metadata +102 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Gerald Kalafut
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
= Ghaki Env - Run mode discovery helpers
|
2
|
+
|
3
|
+
Ghaki Env is a collection of application helpers for dealing with the run time environment.
|
4
|
+
|
5
|
+
== Download
|
6
|
+
|
7
|
+
The latest version of Ghaki Env can be found at
|
8
|
+
|
9
|
+
* git@github.com:ghaki/ghaki-env.git
|
10
|
+
|
11
|
+
== Installation
|
12
|
+
|
13
|
+
The preferred method of installing Ghaki Env is through its GEM file.
|
14
|
+
|
15
|
+
% [sudo] gem install ghaki-env-0.0.1.gem
|
16
|
+
|
17
|
+
== License
|
18
|
+
|
19
|
+
Ghaki Env is released under the MIT license.
|
20
|
+
|
21
|
+
== Support
|
22
|
+
|
23
|
+
Contact mailto:gerald@kalafut.org
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Ghaki #:nodoc:
|
4
|
+
module Env #:nodoc:
|
5
|
+
|
6
|
+
# Run time environment discovery singleton.
|
7
|
+
#
|
8
|
+
# - Detects GHAKI_RUN_ENV, RAILS_ENV, RACK_ENV in environment.
|
9
|
+
# - Detects the RAILS_ENV global constant.
|
10
|
+
# - Detects Sinatra and Merb classes in the Ruby namespace.
|
11
|
+
#
|
12
|
+
# ==== Examples
|
13
|
+
#
|
14
|
+
# Ghaki::Env.instance.clear_environment
|
15
|
+
# RACK_ENV = 'testing'
|
16
|
+
# Ghaki::Env.instance.environment #=> 'testing'
|
17
|
+
|
18
|
+
class Engine
|
19
|
+
include Singleton
|
20
|
+
|
21
|
+
# Environment read attribute, detects current environment if not already set.
|
22
|
+
|
23
|
+
def environment
|
24
|
+
@environment ||= self.detect_environment
|
25
|
+
end
|
26
|
+
|
27
|
+
# Environment write attribute. Asserts on acceptable values.
|
28
|
+
|
29
|
+
def environment= val
|
30
|
+
@environment = _assert_environment( val )
|
31
|
+
end
|
32
|
+
|
33
|
+
# Clears current set environment.
|
34
|
+
|
35
|
+
def clear_environment
|
36
|
+
@environment = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
# Throw an exception if the environment is not valid.
|
40
|
+
|
41
|
+
def assert_environment
|
42
|
+
_assert_environment( @environment )
|
43
|
+
end
|
44
|
+
|
45
|
+
# Check environment for current run state.
|
46
|
+
|
47
|
+
def detect_environment
|
48
|
+
_assert_environment( self._detect_environment )
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def _assert_environment val #:nodoc:
|
54
|
+
case val
|
55
|
+
when nil
|
56
|
+
raise ArgumentError, 'Missing Environment'
|
57
|
+
when :production, :prod, 'production', 'prod'
|
58
|
+
:production
|
59
|
+
when :devel, :development, :dev, 'devel', 'development', 'dev'
|
60
|
+
:development
|
61
|
+
when :test, :testing, 'test', 'testing'
|
62
|
+
:test
|
63
|
+
else
|
64
|
+
raise ArgumentError, "Invalid Environment Name: #{val}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def _detect_environment #:nodoc:
|
69
|
+
if not @environment.nil?
|
70
|
+
@environment
|
71
|
+
elsif ENV.has_key?('GHAKI_RUN_ENV')
|
72
|
+
ENV['GHAKI_RUN_ENV']
|
73
|
+
elsif ENV.has_key?('RACK_ENV')
|
74
|
+
ENV['RACK_ENV']
|
75
|
+
elsif defined?(RAILS_ENV)
|
76
|
+
RAILS_ENV
|
77
|
+
elsif ENV.has_key?('RAILS_ENV') # deprecated
|
78
|
+
ENV['RAILS_ENV']
|
79
|
+
elsif defined?(Sinatra)
|
80
|
+
Sinatra.environment
|
81
|
+
elsif defined?(Merb)
|
82
|
+
Merb.environment
|
83
|
+
else
|
84
|
+
nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'ghaki/env/engine'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module Ghaki #:nodoc:
|
5
|
+
module Env #:nodoc:
|
6
|
+
|
7
|
+
# Extends class with environment methods delegated to the environment singleton.
|
8
|
+
|
9
|
+
module Mixin
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
def_delegators 'Ghaki::Env::Engine.instance',
|
13
|
+
:environment,:environment=,:clear_environment,
|
14
|
+
:assert_environment,:detect_environment
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ghaki/env/app'
|
2
|
+
|
3
|
+
ENV['GHAKI_RUN_ENV'] = 'test'
|
4
|
+
|
5
|
+
describe Ghaki::App::Engine do
|
6
|
+
|
7
|
+
context 'object' do
|
8
|
+
subject { Ghaki::App::Engine.instance }
|
9
|
+
it { should respond_to :environment }
|
10
|
+
it { should respond_to :environment= }
|
11
|
+
it { should respond_to :assert_environment }
|
12
|
+
it { should respond_to :clear_environment }
|
13
|
+
it { should respond_to :detect_environment }
|
14
|
+
|
15
|
+
describe '#environment' do
|
16
|
+
it 'should actually see ENV[GHAKI_RUN_ENV]' do
|
17
|
+
subject.environment.should == :test
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
############################################################################
|
2
|
+
require 'ghaki/env/engine'
|
3
|
+
|
4
|
+
############################################################################
|
5
|
+
module Ghaki module Env module EngineTesting
|
6
|
+
describe Ghaki::Env::Engine do
|
7
|
+
|
8
|
+
########################################################################
|
9
|
+
subject { Ghaki::Env::Engine.instance }
|
10
|
+
context 'singleton instance' do
|
11
|
+
it { should respond_to :environment }
|
12
|
+
it { should respond_to :environment= }
|
13
|
+
it { should respond_to :assert_environment }
|
14
|
+
it { should respond_to :clear_environment }
|
15
|
+
it { should respond_to :detect_environment }
|
16
|
+
end
|
17
|
+
|
18
|
+
########################################################################
|
19
|
+
def clear_all
|
20
|
+
ENV.delete('GHAKI_RUN_ENV')
|
21
|
+
ENV.delete('RAILS_ENV')
|
22
|
+
ENV.delete('RACK_ENV')
|
23
|
+
Object.send( :remove_const, :Sinatra ) if defined? ::Sinatra
|
24
|
+
Object.send( :remove_const, :Merb ) if defined? ::Merb
|
25
|
+
Object.send( :remove_const, :RAILS_ENV ) if defined? ::RAILS_ENV
|
26
|
+
end
|
27
|
+
|
28
|
+
before(:all) do clear_all end
|
29
|
+
after(:each) do clear_all end
|
30
|
+
|
31
|
+
########################################################################
|
32
|
+
describe '#assert_environment' do
|
33
|
+
it 'complains when missing' do
|
34
|
+
lambda {
|
35
|
+
subject.assert_environment
|
36
|
+
}.should raise_error( ArgumentError, 'Missing Environment' )
|
37
|
+
end
|
38
|
+
it 'does not complain when present' do
|
39
|
+
subject.environment = 'production'
|
40
|
+
lambda {
|
41
|
+
subject.assert_environment
|
42
|
+
}.should_not raise_error( ArgumentError, 'Missing Environment' )
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
########################################################################
|
47
|
+
describe '#clear_environment' do
|
48
|
+
it 'cleans environment' do
|
49
|
+
subject.environment = 'test'
|
50
|
+
subject.clear_environment
|
51
|
+
lambda {
|
52
|
+
subject.assert_environment
|
53
|
+
}.should raise_error( ArgumentError, 'Missing Environment' )
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
before(:each) do subject.clear_environment end
|
58
|
+
|
59
|
+
########################################################################
|
60
|
+
USING_SETUP = {
|
61
|
+
'explicit assignment' => [ :production, lambda {
|
62
|
+
Ghaki::Env::Engine.instance.environment = 'production'
|
63
|
+
}],
|
64
|
+
'ENV[GHAKI_RUN_ENV]' => [ :development, lambda { ENV['GHAKI_RUN_ENV'] = 'development' }],
|
65
|
+
'ENV[RAILS_ENV]' => [ :development, lambda { ENV['RAILS_ENV'] = 'development' }],
|
66
|
+
'ENV[RACK_ENV]' => [ :test, lambda { ENV['RACK_ENV'] = 'test' }],
|
67
|
+
'RAILS_ENV' => [ :development, lambda { ::RAILS_ENV = 'development' }],
|
68
|
+
'Merb' => [ :production, lambda {
|
69
|
+
Object::const_set( :Merb, Class::new )
|
70
|
+
::Merb.expects(:environment).returns('production')
|
71
|
+
}],
|
72
|
+
'Sinatra' => [ :test, lambda {
|
73
|
+
Object::const_set( :Sinatra, Class::new )
|
74
|
+
::Sinatra.expects(:environment).returns('test')
|
75
|
+
}],
|
76
|
+
}
|
77
|
+
|
78
|
+
########################################################################
|
79
|
+
describe '#detect_environment' do
|
80
|
+
USING_SETUP.each_key do |desc|
|
81
|
+
want,func = USING_SETUP[desc]
|
82
|
+
it "detects #{desc}" do
|
83
|
+
func.call
|
84
|
+
subject.detect_environment.should == want
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
########################################################################
|
90
|
+
describe '#environment' do
|
91
|
+
USING_SETUP.each_key do |desc|
|
92
|
+
want,func = USING_SETUP[desc]
|
93
|
+
it "accepts #{desc}" do
|
94
|
+
func.call
|
95
|
+
subject.environment.should == want
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end end end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
############################################################################
|
2
|
+
require 'ghaki/env/mixin'
|
3
|
+
|
4
|
+
############################################################################
|
5
|
+
module Ghaki module Env module MixinTesting
|
6
|
+
describe Ghaki::Env::Mixin do
|
7
|
+
|
8
|
+
########################################################################
|
9
|
+
class TestEnv
|
10
|
+
include Ghaki::Env::Mixin
|
11
|
+
end
|
12
|
+
|
13
|
+
########################################################################
|
14
|
+
context 'including objects' do
|
15
|
+
|
16
|
+
subject { TestEnv.new }
|
17
|
+
it { should respond_to :environment }
|
18
|
+
it { should respond_to :environment= }
|
19
|
+
it { should respond_to :assert_environment }
|
20
|
+
it { should respond_to :clear_environment }
|
21
|
+
it { should respond_to :detect_environment }
|
22
|
+
|
23
|
+
describe '#environment' do
|
24
|
+
it 'should actually see ENV[GHAKI_RUN_ENV]' do
|
25
|
+
subject.environment.should == :test
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end end end
|
33
|
+
############################################################################
|
34
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ghaki-env
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2011.11.29.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gerald Kalafut
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-30 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: ghaki-app
|
16
|
+
requirement: &78229850 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *78229850
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &78229590 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.4.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *78229590
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: mocha
|
38
|
+
requirement: &78229330 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.9.12
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *78229330
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rdoc
|
49
|
+
requirement: &78229100 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.9.4
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *78229100
|
58
|
+
description: Collection of application helpers for dealing with the run time environment.
|
59
|
+
email: gerald@kalafut.org
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files:
|
63
|
+
- README
|
64
|
+
files:
|
65
|
+
- lib/ghaki/env/engine.rb
|
66
|
+
- lib/ghaki/env/mixin.rb
|
67
|
+
- lib/ghaki/env/app.rb
|
68
|
+
- README
|
69
|
+
- LICENSE
|
70
|
+
- spec/ghaki/env/app_spec.rb
|
71
|
+
- spec/ghaki/env/mixin_spec.rb
|
72
|
+
- spec/ghaki/env/engine_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: http://github.com/ghaki
|
75
|
+
licenses: []
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 1.3.6
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project: ghaki-env
|
94
|
+
rubygems_version: 1.8.10
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: Run mode discovery helpers
|
98
|
+
test_files:
|
99
|
+
- spec/ghaki/env/app_spec.rb
|
100
|
+
- spec/ghaki/env/mixin_spec.rb
|
101
|
+
- spec/ghaki/env/engine_spec.rb
|
102
|
+
- spec/spec_helper.rb
|