smart_env 0.0.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Gemfile +4 -5
- data/LICENSE +22 -0
- data/README.md +41 -3
- data/Rakefile +20 -0
- data/lib/smart_env/auto.rb +3 -0
- data/lib/smart_env/uri_proxy.rb +18 -4
- data/lib/smart_env/version.rb +1 -1
- data/lib/smart_env.rb +24 -32
- data/smart_env.gemspec +2 -0
- data/spec/decoration_spec.rb +30 -0
- data/spec/load_unload_spec.rb +46 -0
- data/spec/registry_spec.rb +38 -14
- data/spec/spec_helper.rb +6 -2
- data/spec/uri_spec.rb +16 -4
- metadata +38 -6
data/.gitignore
CHANGED
data/Gemfile
CHANGED
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
@@ -1,17 +1,55 @@
|
|
1
1
|
# SmartEnv
|
2
2
|
|
3
|
+
gem install smart_env
|
4
|
+
|
5
|
+
require 'smart_env'
|
6
|
+
|
3
7
|
## Purpose
|
4
8
|
Attach hooks to your ENV vars and wrap them in Proxy Objects.
|
5
9
|
|
6
|
-
## Example
|
10
|
+
## Example with built-in URI Proxy
|
11
|
+
ENV.use(SmartEnv::UriProxy)
|
12
|
+
|
7
13
|
ENV['SERVICE'] = 'http://username:password@example.com:3000/"
|
8
14
|
|
9
15
|
ENV['SERVICE'] #=> 'http://username:password@example.com:3000/"
|
10
|
-
ENV['SERVICE'].base_uri #=> 'http://example.com
|
11
|
-
ENV['SERVICE'].url #=> 'http://example.com/"
|
16
|
+
ENV['SERVICE'].base_uri #=> 'http://example.com:3000"
|
12
17
|
ENV['SERVICE'].user #=> 'username'
|
13
18
|
ENV['SERVICE'].password #=> 'password'
|
14
19
|
ENV['SERVICE'].host #=> 'example.com'
|
15
20
|
ENV['SERVICE'].scheme #=> 'http'
|
16
21
|
ENV['SERVICE'].port #=> 3000
|
17
22
|
|
23
|
+
## Add your own Proxies
|
24
|
+
|
25
|
+
### With a block
|
26
|
+
class TestProxy
|
27
|
+
def initialize(key, value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ENV.use(TestProxy).when { |key, value| key == 'FOO' }
|
32
|
+
|
33
|
+
ENV['FOO'] = 'bar'
|
34
|
+
ENV['FOO'].class #=> TestProxy
|
35
|
+
|
36
|
+
### Or by implementing ::when
|
37
|
+
|
38
|
+
class TestProxy
|
39
|
+
def initialize(key, value)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.when(key, value)
|
43
|
+
key == 'FOO'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
ENV.use(TestProxy)
|
47
|
+
|
48
|
+
ENV['FOO'] = 'bar'
|
49
|
+
ENV['FOO'].class #=> TestProxy
|
50
|
+
## License
|
51
|
+
|
52
|
+
SmartENV distributed under the terms of the MIT License. See [LICENSE][] for details.
|
53
|
+
|
54
|
+
[LICENSE]: /csquared/smart_env/blob/master/LICENSE
|
55
|
+
|
data/Rakefile
CHANGED
@@ -1 +1,21 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.require
|
1
3
|
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
desc 'Default: run specs.'
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
desc "Run specs"
|
11
|
+
RSpec::Core::RakeTask.new do |t|
|
12
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
13
|
+
# Put spec opts in a file named .rspec in root
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Generate code coverage"
|
17
|
+
RSpec::Core::RakeTask.new(:coverage) do |t|
|
18
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
19
|
+
t.rcov = true
|
20
|
+
t.rcov_opts = ['--exclude', 'spec']
|
21
|
+
end
|
data/lib/smart_env/uri_proxy.rb
CHANGED
@@ -1,21 +1,35 @@
|
|
1
1
|
require 'uri'
|
2
|
+
require 'cgi'
|
2
3
|
require 'forwardable'
|
3
4
|
|
5
|
+
class BasicObject
|
6
|
+
instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval/ }
|
7
|
+
end unless defined?(BasicObject)
|
8
|
+
|
4
9
|
module SmartEnv
|
5
10
|
class UriProxy < BasicObject
|
11
|
+
attr_reader :params
|
6
12
|
extend ::Forwardable
|
7
13
|
def_delegators :@uri, *(::URI::Generic::COMPONENT + [:user, :password])
|
8
14
|
|
9
|
-
def initialize(uri)
|
15
|
+
def initialize(_key, uri)
|
10
16
|
@original = uri
|
11
17
|
@uri = ::URI.parse(uri)
|
18
|
+
@params = ::CGI.parse(@uri.query.to_s)
|
19
|
+
@params.each do |key, values|
|
20
|
+
@params[key] = values.first if values.size == 1
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
#for SmartEnv
|
25
|
+
def self.when(key, value)
|
26
|
+
value.match(/^\w+:\/\//)
|
12
27
|
end
|
13
28
|
|
14
|
-
def base_uri
|
29
|
+
def base_uri(port = true)
|
15
30
|
base = "#{@uri.scheme}://#{@uri.host}"
|
16
|
-
|
31
|
+
port ? "#{base}:#{@uri.port}" : base
|
17
32
|
end
|
18
|
-
alias url base_uri
|
19
33
|
|
20
34
|
def method_missing(method, *args, &block)
|
21
35
|
@original.send(method, *args, &block)
|
data/lib/smart_env/version.rb
CHANGED
data/lib/smart_env.rb
CHANGED
@@ -2,57 +2,49 @@ require "smart_env/version"
|
|
2
2
|
require "smart_env/uri_proxy"
|
3
3
|
|
4
4
|
module SmartEnv
|
5
|
-
|
5
|
+
attr_accessor :registry
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
def clear_registry
|
8
|
+
@registry = empty_registry
|
9
|
+
end
|
9
10
|
|
10
|
-
def
|
11
|
-
|
12
|
-
[lambda{ |k,v| v.match(/^\w+:\/\//) }, UriProxy]
|
13
|
-
]
|
11
|
+
def empty_registry
|
12
|
+
[]
|
14
13
|
end
|
15
14
|
|
16
15
|
def use(klass)
|
17
16
|
@class = klass
|
17
|
+
if @class.respond_to? :when
|
18
|
+
registry << [@class, lambda { |k,v| @class.when(k,v) }]
|
19
|
+
end
|
18
20
|
self
|
19
21
|
end
|
20
22
|
|
21
23
|
def when(&block)
|
22
|
-
raise "Block must take 0 or 2 arguments" unless (
|
23
|
-
|
24
|
+
raise "Block must take 0 or 2 arguments: key and value" unless (block.arity < 1 || block.arity == 2)
|
25
|
+
registry << [@class, block]
|
24
26
|
self
|
25
27
|
end
|
26
28
|
|
27
|
-
def
|
28
|
-
|
29
|
-
@@registry.each do |condition, klass|
|
30
|
-
result = condition.call(key, value) rescue false
|
31
|
-
return klass.new(value) if result
|
32
|
-
end
|
33
|
-
value
|
29
|
+
def registry
|
30
|
+
@registry ||= empty_registry
|
34
31
|
end
|
35
32
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
alias_method :[]=, :set
|
40
|
-
end
|
41
|
-
@@loaded = false
|
42
|
-
end
|
33
|
+
def self.extended(base)
|
34
|
+
raise RuntimeError.new("#{base.inspect} doesn't respond to #[]") \
|
35
|
+
unless base.respond_to? :[]
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
return if @@loaded
|
47
|
-
class << ENV
|
48
|
-
alias_method :get, :[]
|
37
|
+
class << base
|
38
|
+
alias_method :__get, :[]
|
49
39
|
|
50
40
|
def [](key)
|
51
|
-
|
41
|
+
value = __get(key)
|
42
|
+
registry.each do |klass, condition|
|
43
|
+
result = condition.call(key, value) rescue false
|
44
|
+
value = klass.new(key, value) if value && result
|
45
|
+
end
|
46
|
+
value
|
52
47
|
end
|
53
48
|
end
|
54
|
-
@@loaded = true
|
55
49
|
end
|
56
50
|
end
|
57
|
-
|
58
|
-
SmartEnv.load!
|
data/smart_env.gemspec
CHANGED
@@ -17,4 +17,6 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
|
+
s.add_development_dependency 'rspec', '~> 2.0'
|
21
|
+
s.add_development_dependency 'rcov'
|
20
22
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SmartEnv, 'decoration' do
|
4
|
+
before do
|
5
|
+
class PassOne < String
|
6
|
+
attr_accessor :one
|
7
|
+
def initialize(key, value)
|
8
|
+
@one = true
|
9
|
+
super(value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class PassTwo < PassOne
|
14
|
+
attr_accessor :two
|
15
|
+
def initialize(key, value)
|
16
|
+
@two = true
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ENV.use(PassOne).when{ true }
|
22
|
+
ENV.use(PassTwo).when{ true }
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should pass successive value to all callbacks that match" do
|
26
|
+
ENV['FOO'] = "bar"
|
27
|
+
ENV['FOO'].one.should be_true
|
28
|
+
ENV['FOO'].two.should be_true
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SmartEnv, 'load/unload' do
|
4
|
+
before do
|
5
|
+
class PassOne < String
|
6
|
+
attr_accessor :one
|
7
|
+
def initialize(key, value)
|
8
|
+
@one = true
|
9
|
+
super(value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
@env = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
context "on extend" do
|
17
|
+
before do
|
18
|
+
@env.extend SmartEnv
|
19
|
+
end
|
20
|
+
|
21
|
+
after do
|
22
|
+
@env.clear_registry if @env
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not bleed" do
|
26
|
+
ENV['FOO'] = 'bar'
|
27
|
+
@env['FOO'] = 'bar'
|
28
|
+
@env.use(PassOne).when{ true }
|
29
|
+
|
30
|
+
ENV['FOO'].should_not respond_to :one
|
31
|
+
@env['FOO'].one.should be_true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "::clear_registry" do
|
36
|
+
before do
|
37
|
+
@env.extend SmartEnv
|
38
|
+
@env.use(PassOne).when{ true }
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should clear the hooks" do
|
42
|
+
@env.clear_registry
|
43
|
+
@env['FOO'].should_not respond_to :one
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
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
|
-
SmartEnv.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/spec_helper.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
|
1
|
+
require File.expand_path('../../lib/smart_env', __FILE__)
|
2
|
+
require 'rspec/core'
|
3
|
+
require 'smart_env/auto'
|
2
4
|
|
3
5
|
RSpec.configure do |config|
|
4
|
-
config.after(:each)
|
6
|
+
config.after(:each) do
|
7
|
+
ENV.clear_registry
|
8
|
+
end
|
5
9
|
end
|
data/spec/uri_spec.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe SmartEnv, 'uri support' do
|
4
|
+
before do
|
5
|
+
ENV.use(SmartEnv::UriProxy)
|
6
|
+
end
|
7
|
+
|
4
8
|
context "with a value FOO that is not a URI" do
|
5
9
|
before do
|
6
10
|
ENV['FOO'] = 'bar'
|
@@ -12,7 +16,6 @@ describe SmartEnv, 'uri support' do
|
|
12
16
|
end
|
13
17
|
|
14
18
|
context "with a value FOO that is a URI" do
|
15
|
-
|
16
19
|
before do
|
17
20
|
@url = 'http://username:password@this.domain.example.com:3000/path?var=val'
|
18
21
|
ENV['FOO'] = @url
|
@@ -22,9 +25,14 @@ describe SmartEnv, 'uri support' do
|
|
22
25
|
ENV['FOO'].should == @url
|
23
26
|
end
|
24
27
|
|
25
|
-
it "should return scheme://host for #base_uri
|
26
|
-
ENV['FOO'].base_uri.should == 'http://this.domain.example.com:3000
|
27
|
-
ENV['FOO']
|
28
|
+
it "should return scheme://host:port for #base_uri" do
|
29
|
+
ENV['FOO'].base_uri.should == 'http://this.domain.example.com:3000'
|
30
|
+
ENV['FOO'] = @url.gsub(':3000','')
|
31
|
+
ENV['FOO'].base_uri.should == 'http://this.domain.example.com:80'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return scheme://host for #base_uri(false)" do
|
35
|
+
ENV['FOO'].base_uri(false).should == 'http://this.domain.example.com'
|
28
36
|
end
|
29
37
|
|
30
38
|
it "should respond to #scheme with the scheme" do
|
@@ -46,5 +54,9 @@ describe SmartEnv, 'uri support' do
|
|
46
54
|
it "should respond to #port with the port" do
|
47
55
|
ENV['FOO'].port.should == 3000
|
48
56
|
end
|
57
|
+
|
58
|
+
it "should expose params via the #params method" do
|
59
|
+
ENV['FOO'].params['var'].should == 'val'
|
60
|
+
end
|
49
61
|
end
|
50
62
|
end
|
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: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,30 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
|
14
|
-
|
12
|
+
date: 2012-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70268106271400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70268106271400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rcov
|
27
|
+
requirement: &70268106270940 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70268106270940
|
15
36
|
description: Allows you to register Proxy classes for ENV vars by using blocks to
|
16
37
|
match keys and values
|
17
38
|
email:
|
@@ -22,16 +43,19 @@ extra_rdoc_files: []
|
|
22
43
|
files:
|
23
44
|
- .gitignore
|
24
45
|
- Gemfile
|
46
|
+
- LICENSE
|
25
47
|
- README.md
|
26
48
|
- Rakefile
|
27
49
|
- lib/smart_env.rb
|
50
|
+
- lib/smart_env/auto.rb
|
28
51
|
- lib/smart_env/uri_proxy.rb
|
29
52
|
- lib/smart_env/version.rb
|
30
53
|
- smart_env.gemspec
|
54
|
+
- spec/decoration_spec.rb
|
55
|
+
- spec/load_unload_spec.rb
|
31
56
|
- spec/registry_spec.rb
|
32
57
|
- spec/spec_helper.rb
|
33
58
|
- spec/uri_spec.rb
|
34
|
-
has_rdoc: true
|
35
59
|
homepage: ''
|
36
60
|
licenses: []
|
37
61
|
post_install_message:
|
@@ -44,19 +68,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
68
|
- - ! '>='
|
45
69
|
- !ruby/object:Gem::Version
|
46
70
|
version: '0'
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
hash: 3361827544882758592
|
47
74
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
75
|
none: false
|
49
76
|
requirements:
|
50
77
|
- - ! '>='
|
51
78
|
- !ruby/object:Gem::Version
|
52
79
|
version: '0'
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
hash: 3361827544882758592
|
53
83
|
requirements: []
|
54
84
|
rubyforge_project: smart_env
|
55
|
-
rubygems_version: 1.
|
85
|
+
rubygems_version: 1.8.10
|
56
86
|
signing_key:
|
57
87
|
specification_version: 3
|
58
88
|
summary: Lazily proxy ENV values
|
59
89
|
test_files:
|
90
|
+
- spec/decoration_spec.rb
|
91
|
+
- spec/load_unload_spec.rb
|
60
92
|
- spec/registry_spec.rb
|
61
93
|
- spec/spec_helper.rb
|
62
94
|
- spec/uri_spec.rb
|