smart_env 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +7 -0
- data/README.md +17 -0
- data/Rakefile +1 -0
- data/lib/smart_env/uri_proxy.rb +24 -0
- data/lib/smart_env/version.rb +3 -0
- data/lib/smart_env.rb +58 -0
- data/smart_env.gemspec +20 -0
- data/spec/registry_spec.rb +25 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/uri_spec.rb +50 -0
- metadata +62 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# SmartEnv
|
2
|
+
|
3
|
+
## Purpose
|
4
|
+
Attach hooks to your ENV vars and wrap them in Proxy Objects.
|
5
|
+
|
6
|
+
## Example
|
7
|
+
ENV['SERVICE'] = 'http://username:password@example.com:3000/"
|
8
|
+
|
9
|
+
ENV['SERVICE'] #=> 'http://username:password@example.com:3000/"
|
10
|
+
ENV['SERVICE'].base_uri #=> 'http://example.com/"
|
11
|
+
ENV['SERVICE'].url #=> 'http://example.com/"
|
12
|
+
ENV['SERVICE'].user #=> 'username'
|
13
|
+
ENV['SERVICE'].password #=> 'password'
|
14
|
+
ENV['SERVICE'].host #=> 'example.com'
|
15
|
+
ENV['SERVICE'].scheme #=> 'http'
|
16
|
+
ENV['SERVICE'].port #=> 3000
|
17
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'forwardable'
|
3
|
+
|
4
|
+
module SmartEnv
|
5
|
+
class UriProxy < BasicObject
|
6
|
+
extend ::Forwardable
|
7
|
+
def_delegators :@uri, *(::URI::Generic::COMPONENT + [:user, :password])
|
8
|
+
|
9
|
+
def initialize(uri)
|
10
|
+
@original = uri
|
11
|
+
@uri = ::URI.parse(uri)
|
12
|
+
end
|
13
|
+
|
14
|
+
def base_uri
|
15
|
+
base = "#{@uri.scheme}://#{@uri.host}"
|
16
|
+
(@uri.port ? "#{base}:#{@uri.port}" : base) + '/'
|
17
|
+
end
|
18
|
+
alias url base_uri
|
19
|
+
|
20
|
+
def method_missing(method, *args, &block)
|
21
|
+
@original.send(method, *args, &block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/smart_env.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "smart_env/version"
|
2
|
+
require "smart_env/uri_proxy"
|
3
|
+
|
4
|
+
module SmartEnv
|
5
|
+
extend self
|
6
|
+
|
7
|
+
@@loaded = false
|
8
|
+
@@registry = []
|
9
|
+
|
10
|
+
def reset_registry
|
11
|
+
@@registry = [
|
12
|
+
[lambda{ |k,v| v.match(/^\w+:\/\//) }, UriProxy]
|
13
|
+
]
|
14
|
+
end
|
15
|
+
|
16
|
+
def use(klass)
|
17
|
+
@class = klass
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def when(&block)
|
22
|
+
raise "Block must take 0 or 2 arguments" unless ( block.arity == 0 || block.arity == 2)
|
23
|
+
@@registry << [block, @class]
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def [](key)
|
28
|
+
value = ENV.get(key)
|
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
|
34
|
+
end
|
35
|
+
|
36
|
+
def unload!
|
37
|
+
class << ENV
|
38
|
+
alias_method :[], :get
|
39
|
+
alias_method :[]=, :set
|
40
|
+
end
|
41
|
+
@@loaded = false
|
42
|
+
end
|
43
|
+
|
44
|
+
def load!
|
45
|
+
reset_registry
|
46
|
+
return if @@loaded
|
47
|
+
class << ENV
|
48
|
+
alias_method :get, :[]
|
49
|
+
|
50
|
+
def [](key)
|
51
|
+
SmartEnv[key]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
@@loaded = true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
SmartEnv.load!
|
data/smart_env.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "smart_env/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "smart_env"
|
7
|
+
s.version = SmartEnv::VERSION
|
8
|
+
s.authors = ["Chris Continanza"]
|
9
|
+
s.email = ["christopher.continanza@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Lazily proxy ENV values}
|
12
|
+
s.description = %q{Allows you to register Proxy classes for ENV vars by using blocks to match keys and values}
|
13
|
+
|
14
|
+
s.rubyforge_project = "smart_env"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SmartEnv, 'registering your own Proxies' do
|
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
|
13
|
+
ENV['FOO'] = 'bar'
|
14
|
+
ENV['FOO'].should be_a(TestProxy)
|
15
|
+
ENV['BAR'] = 'bar'
|
16
|
+
ENV['BAR'].should == 'bar'
|
17
|
+
ENV['BAR'].should be_a(String)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "raises error if the block doens't have two args" do
|
21
|
+
lambda { SmartEnv.use(TestProxy).when { |k,v,x| name == 'FOO' } }.should raise_error
|
22
|
+
lambda { SmartEnv.use(TestProxy).when { |value| name == 'FOO' } }.should raise_error
|
23
|
+
lambda { SmartEnv.use(TestProxy).when { false } }.should_not raise_error
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/uri_spec.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SmartEnv, 'uri support' do
|
4
|
+
context "with a value FOO that is not a URI" do
|
5
|
+
before do
|
6
|
+
ENV['FOO'] = 'bar'
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not wrap it" do
|
10
|
+
lambda { ENV['FOO'].scheme }.should raise_error(::NoMethodError)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with a value FOO that is a URI" do
|
15
|
+
|
16
|
+
before do
|
17
|
+
@url = 'http://username:password@this.domain.example.com:3000/path?var=val'
|
18
|
+
ENV['FOO'] = @url
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should leave the original value unchanged" do
|
22
|
+
ENV['FOO'].should == @url
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should return scheme://host for #base_uri and #url" do
|
26
|
+
ENV['FOO'].base_uri.should == 'http://this.domain.example.com:3000/'
|
27
|
+
ENV['FOO'].url.should == 'http://this.domain.example.com:3000/'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should respond to #scheme with the scheme" do
|
31
|
+
ENV['FOO'].scheme.should == 'http'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should respond to #host with the host" do
|
35
|
+
ENV['FOO'].host.should == 'this.domain.example.com'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should respond to #password with the password" do
|
39
|
+
ENV['FOO'].password.should == 'password'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should respond to #user with the user" do
|
43
|
+
ENV['FOO'].user.should == 'username'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should respond to #port with the port" do
|
47
|
+
ENV['FOO'].port.should == 3000
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_env
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Continanza
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-19 00:00:00.000000000 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
description: Allows you to register Proxy classes for ENV vars by using blocks to
|
16
|
+
match keys and values
|
17
|
+
email:
|
18
|
+
- christopher.continanza@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/smart_env.rb
|
28
|
+
- lib/smart_env/uri_proxy.rb
|
29
|
+
- lib/smart_env/version.rb
|
30
|
+
- smart_env.gemspec
|
31
|
+
- spec/registry_spec.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
- spec/uri_spec.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: ''
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project: smart_env
|
55
|
+
rubygems_version: 1.6.2
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Lazily proxy ENV values
|
59
|
+
test_files:
|
60
|
+
- spec/registry_spec.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/uri_spec.rb
|