smart_env 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +25 -0
- data/lib/smart_env/uri_proxy.rb +5 -1
- data/lib/smart_env/version.rb +1 -1
- data/lib/smart_env.rb +5 -2
- data/spec/decoration_spec.rb +3 -3
- data/spec/load_unload_spec.rb +2 -2
- data/spec/registry_spec.rb +38 -14
- data/spec/uri_spec.rb +4 -0
- metadata +3 -2
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2010 Blake Mizerany, Keith Rarick
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/README.md
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
Attach hooks to your ENV vars and wrap them in Proxy Objects.
|
9
9
|
|
10
10
|
## Example with built-in URI Proxy
|
11
|
+
ENV.use(SmartEnv::UriProxy)
|
12
|
+
|
11
13
|
ENV['SERVICE'] = 'http://username:password@example.com:3000/"
|
12
14
|
|
13
15
|
ENV['SERVICE'] #=> 'http://username:password@example.com:3000/"
|
@@ -20,6 +22,8 @@ Attach hooks to your ENV vars and wrap them in Proxy Objects.
|
|
20
22
|
ENV['SERVICE'].port #=> 3000
|
21
23
|
|
22
24
|
## Add your own Proxies
|
25
|
+
|
26
|
+
### With a block
|
23
27
|
class TestProxy
|
24
28
|
def initialize(value)
|
25
29
|
end
|
@@ -29,3 +33,24 @@ Attach hooks to your ENV vars and wrap them in Proxy Objects.
|
|
29
33
|
|
30
34
|
ENV['FOO'] = 'bar'
|
31
35
|
ENV['FOO'].class #=> TestProxy
|
36
|
+
|
37
|
+
### Or by implementing ::when
|
38
|
+
|
39
|
+
class TestProxy
|
40
|
+
def initialize(key, value)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.when(key, value)
|
44
|
+
key == 'FOO'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
ENV.use(TestProxy)
|
48
|
+
|
49
|
+
ENV['FOO'] = 'bar'
|
50
|
+
ENV['FOO'].class #=> TestProxy
|
51
|
+
## License
|
52
|
+
|
53
|
+
SmartENV distributed under the terms of the MIT License. See [LICENSE][] for details.
|
54
|
+
|
55
|
+
[LICENSE]: /csquared/smart_env/blob/master/LICENSE
|
56
|
+
|
data/lib/smart_env/uri_proxy.rb
CHANGED
@@ -8,7 +8,7 @@ module SmartEnv
|
|
8
8
|
extend ::Forwardable
|
9
9
|
def_delegators :@uri, *(::URI::Generic::COMPONENT + [:user, :password])
|
10
10
|
|
11
|
-
def initialize(uri)
|
11
|
+
def initialize(_key, uri)
|
12
12
|
@original = uri
|
13
13
|
@uri = ::URI.parse(uri)
|
14
14
|
@params = ::CGI.parse(@uri.query.to_s)
|
@@ -17,6 +17,10 @@ module SmartEnv
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
+
def self.when(key, value)
|
21
|
+
value.match(/^\w+:\/\//)
|
22
|
+
end
|
23
|
+
|
20
24
|
def base_uri
|
21
25
|
base = "#{@uri.scheme}://#{@uri.host}"
|
22
26
|
(@uri.port ? "#{base}:#{@uri.port}" : base) + '/'
|
data/lib/smart_env/version.rb
CHANGED
data/lib/smart_env.rb
CHANGED
@@ -13,11 +13,14 @@ module SmartEnv
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def default
|
16
|
-
[
|
16
|
+
[]
|
17
17
|
end
|
18
18
|
|
19
19
|
def use(klass)
|
20
20
|
@class = klass
|
21
|
+
if @class.respond_to? :when
|
22
|
+
registry << [@class, lambda { |k,v| @class.when(k,v) }]
|
23
|
+
end
|
21
24
|
self
|
22
25
|
end
|
23
26
|
|
@@ -46,7 +49,7 @@ module SmartEnv
|
|
46
49
|
value = get(key)
|
47
50
|
registry.each do |klass, condition|
|
48
51
|
result = condition.call(key, value) rescue false
|
49
|
-
value = klass.new(value) if value && result
|
52
|
+
value = klass.new(key, value) if value && result
|
50
53
|
end
|
51
54
|
value
|
52
55
|
end
|
data/spec/decoration_spec.rb
CHANGED
@@ -4,15 +4,15 @@ describe SmartEnv, 'decoration' do
|
|
4
4
|
before do
|
5
5
|
class PassOne < String
|
6
6
|
attr_accessor :one
|
7
|
-
def initialize(value)
|
7
|
+
def initialize(key, value)
|
8
8
|
@one = true
|
9
|
-
super
|
9
|
+
super(value)
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
class PassTwo < PassOne
|
14
14
|
attr_accessor :two
|
15
|
-
def initialize(value)
|
15
|
+
def initialize(key, value)
|
16
16
|
@two = true
|
17
17
|
super
|
18
18
|
end
|
data/spec/load_unload_spec.rb
CHANGED
data/spec/registry_spec.rb
CHANGED
@@ -1,25 +1,49 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
shared_examples_for "all proxies" do
|
2
3
|
|
3
|
-
|
4
|
-
before do
|
5
|
-
class TestProxy
|
6
|
-
def initialize(value)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
ENV.use(TestProxy).when { |key, value| key == 'FOO' }
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should use the specified proxy when the block returns true" do
|
4
|
+
it "should use the specified proxy when the block or when returns true" do
|
13
5
|
ENV['FOO'] = 'bar'
|
14
6
|
ENV['FOO'].should be_a(TestProxy)
|
15
7
|
ENV['BAR'] = 'bar'
|
16
8
|
ENV['BAR'].should == 'bar'
|
17
9
|
ENV['BAR'].should be_a(String)
|
18
10
|
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe SmartEnv, 'registering your own Proxies' do
|
14
|
+
context 'with a bloack' do
|
15
|
+
before do
|
16
|
+
class TestProxy
|
17
|
+
def initialize(key, value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
ENV.use(TestProxy).when { |key, value| key == 'FOO' }
|
21
|
+
end
|
19
22
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
23
|
+
it_should_behave_like "all proxies"
|
24
|
+
|
25
|
+
it "raises error if the block doens't have two args" do
|
26
|
+
lambda { ENV.use(TestProxy).when { |k,v,x| name == 'FOO' } }.should raise_error
|
27
|
+
lambda { ENV.use(TestProxy).when { |value| name == 'FOO' } }.should raise_error
|
28
|
+
lambda { ENV.use(TestProxy).when { false } }.should_not raise_error
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with a class that implements ::when' do
|
33
|
+
before do
|
34
|
+
class TestProxy
|
35
|
+
def initialize(key, value)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.when(key, value)
|
39
|
+
key == 'FOO'
|
40
|
+
end
|
41
|
+
end
|
42
|
+
ENV.use(TestProxy)
|
43
|
+
end
|
44
|
+
|
45
|
+
it_should_behave_like "all proxies"
|
24
46
|
end
|
25
47
|
end
|
48
|
+
|
49
|
+
|
data/spec/uri_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-10-06 00:00:00.000000000 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
description: Allows you to register Proxy classes for ENV vars by using blocks to
|
@@ -22,6 +22,7 @@ extra_rdoc_files: []
|
|
22
22
|
files:
|
23
23
|
- .gitignore
|
24
24
|
- Gemfile
|
25
|
+
- LICENSE
|
25
26
|
- README.md
|
26
27
|
- Rakefile
|
27
28
|
- lib/smart_env.rb
|