smart_env 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -1
- data/lib/smart_env/version.rb +1 -1
- data/lib/smart_env.rb +24 -25
- data/spec/decoration_spec.rb +30 -0
- data/spec/load_unload_spec.rb +27 -0
- data/spec/registry_spec.rb +4 -4
- data/spec/spec_helper.rb +4 -1
- metadata +6 -2
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
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
|
7
11
|
ENV['SERVICE'] = 'http://username:password@example.com:3000/"
|
8
12
|
|
9
13
|
ENV['SERVICE'] #=> 'http://username:password@example.com:3000/"
|
@@ -15,3 +19,13 @@ Attach hooks to your ENV vars and wrap them in Proxy Objects.
|
|
15
19
|
ENV['SERVICE'].scheme #=> 'http'
|
16
20
|
ENV['SERVICE'].port #=> 3000
|
17
21
|
|
22
|
+
## Add your own Proxies
|
23
|
+
class TestProxy
|
24
|
+
def initialize(value)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
ENV.use(TestProxy).when { |key, value| key == 'FOO' }
|
29
|
+
|
30
|
+
ENV['FOO'] = 'bar'
|
31
|
+
ENV['FOO'].class #=> TestProxy
|
data/lib/smart_env/version.rb
CHANGED
data/lib/smart_env.rb
CHANGED
@@ -2,15 +2,18 @@ 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 = []
|
9
|
+
end
|
9
10
|
|
10
11
|
def reset_registry
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
@registry = default
|
13
|
+
end
|
14
|
+
|
15
|
+
def default
|
16
|
+
[[UriProxy, lambda{ |k,v| v.match(/^\w+:\/\//) }]]
|
14
17
|
end
|
15
18
|
|
16
19
|
def use(klass)
|
@@ -19,40 +22,36 @@ module SmartEnv
|
|
19
22
|
end
|
20
23
|
|
21
24
|
def when(&block)
|
22
|
-
raise "Block must take 0 or 2 arguments" unless (
|
23
|
-
|
25
|
+
raise "Block must take 0 or 2 arguments: key and value" unless (block.arity == 0 || block.arity == 2)
|
26
|
+
registry << [@class, block]
|
24
27
|
self
|
25
28
|
end
|
26
29
|
|
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
|
30
|
+
def registry
|
31
|
+
@registry ||= default
|
34
32
|
end
|
35
33
|
|
36
34
|
def unload!
|
37
|
-
class <<
|
35
|
+
class << self
|
38
36
|
alias_method :[], :get
|
39
|
-
alias_method :[]=, :set
|
37
|
+
# alias_method :[]=, :set
|
40
38
|
end
|
41
|
-
@@loaded = false
|
42
39
|
end
|
43
40
|
|
44
|
-
def
|
45
|
-
|
46
|
-
return if @@loaded
|
47
|
-
class << ENV
|
41
|
+
def self.extended(base)
|
42
|
+
class << base
|
48
43
|
alias_method :get, :[]
|
49
44
|
|
50
45
|
def [](key)
|
51
|
-
|
46
|
+
value = get(key)
|
47
|
+
registry.each do |klass, condition|
|
48
|
+
result = condition.call(key, value) rescue false
|
49
|
+
value = klass.new(value) if value && result
|
50
|
+
end
|
51
|
+
value
|
52
52
|
end
|
53
53
|
end
|
54
|
-
@@loaded = true
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
|
-
SmartEnv
|
57
|
+
ENV.extend SmartEnv
|
@@ -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(value)
|
8
|
+
@one = true
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class PassTwo < PassOne
|
14
|
+
attr_accessor :two
|
15
|
+
def initialize(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,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SmartEnv, 'load/unload' do
|
4
|
+
context "on extend" do
|
5
|
+
before do
|
6
|
+
@env = {}
|
7
|
+
@env.extend SmartEnv
|
8
|
+
|
9
|
+
class PassOne < String
|
10
|
+
attr_accessor :one
|
11
|
+
def initialize(value)
|
12
|
+
@one = true
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not bleed" do
|
19
|
+
ENV['FOO'] = 'bar'
|
20
|
+
@env['FOO'] = 'bar'
|
21
|
+
@env.use(PassOne).when{ true }
|
22
|
+
|
23
|
+
ENV['FOO'].should_not respond_to :one
|
24
|
+
@env['FOO'].one.should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/registry_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe SmartEnv, 'registering your own Proxies' do
|
|
6
6
|
def initialize(value)
|
7
7
|
end
|
8
8
|
end
|
9
|
-
|
9
|
+
ENV.use(TestProxy).when { |key, value| key == 'FOO' }
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should use the specified proxy when the block returns true" do
|
@@ -18,8 +18,8 @@ describe SmartEnv, 'registering your own Proxies' do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it "raises error if the block doens't have two args" do
|
21
|
-
lambda {
|
22
|
-
lambda {
|
23
|
-
lambda {
|
21
|
+
lambda { ENV.use(TestProxy).when { |k,v,x| name == 'FOO' } }.should raise_error
|
22
|
+
lambda { ENV.use(TestProxy).when { |value| name == 'FOO' } }.should raise_error
|
23
|
+
lambda { ENV.use(TestProxy).when { false } }.should_not raise_error
|
24
24
|
end
|
25
25
|
end
|
data/spec/spec_helper.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.0.2
|
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-08-
|
12
|
+
date: 2011-08-24 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
|
@@ -28,6 +28,8 @@ files:
|
|
28
28
|
- lib/smart_env/uri_proxy.rb
|
29
29
|
- lib/smart_env/version.rb
|
30
30
|
- smart_env.gemspec
|
31
|
+
- spec/decoration_spec.rb
|
32
|
+
- spec/load_unload_spec.rb
|
31
33
|
- spec/registry_spec.rb
|
32
34
|
- spec/spec_helper.rb
|
33
35
|
- spec/uri_spec.rb
|
@@ -57,6 +59,8 @@ signing_key:
|
|
57
59
|
specification_version: 3
|
58
60
|
summary: Lazily proxy ENV values
|
59
61
|
test_files:
|
62
|
+
- spec/decoration_spec.rb
|
63
|
+
- spec/load_unload_spec.rb
|
60
64
|
- spec/registry_spec.rb
|
61
65
|
- spec/spec_helper.rb
|
62
66
|
- spec/uri_spec.rb
|