smart_env 0.5.1 → 1.0.0rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/smart_env/version.rb +1 -1
- data/lib/smart_env.rb +34 -21
- data/spec/load_unload_spec.rb +7 -0
- data/spec/memoize_spec.rb +14 -0
- metadata +21 -15
data/lib/smart_env/version.rb
CHANGED
data/lib/smart_env.rb
CHANGED
@@ -4,12 +4,29 @@ require "smart_env/uri_proxy"
|
|
4
4
|
module SmartEnv
|
5
5
|
attr_accessor :registry
|
6
6
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
7
|
+
def self.extended(base)
|
8
|
+
raise RuntimeError.new("#{base.inspect} doesn't respond to #[]") \
|
9
|
+
unless base.respond_to? :[]
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
class << base
|
12
|
+
alias_method :__get, :[]
|
13
|
+
alias_method :__set, :[]=
|
14
|
+
|
15
|
+
def []=(key, value)
|
16
|
+
memoized[key] = nil
|
17
|
+
__set(key, value)
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
return memoized[key] if memoized[key]
|
22
|
+
value = __get(key)
|
23
|
+
registry.each do |klass, condition|
|
24
|
+
result = condition.call(key, value) rescue false
|
25
|
+
value = klass.new(key, value) if value && result
|
26
|
+
end
|
27
|
+
memoized[key] = value
|
28
|
+
end
|
29
|
+
end
|
13
30
|
end
|
14
31
|
|
15
32
|
def use(klass)
|
@@ -26,26 +43,22 @@ module SmartEnv
|
|
26
43
|
self
|
27
44
|
end
|
28
45
|
|
29
|
-
def
|
30
|
-
@registry
|
46
|
+
def clear_registry
|
47
|
+
@registry = empty_registry
|
48
|
+
@memoized = {}
|
31
49
|
end
|
32
50
|
|
33
|
-
|
34
|
-
|
35
|
-
|
51
|
+
private
|
52
|
+
def empty_registry
|
53
|
+
[]
|
54
|
+
end
|
36
55
|
|
37
|
-
|
38
|
-
|
56
|
+
def registry
|
57
|
+
@registry ||= empty_registry
|
58
|
+
end
|
39
59
|
|
40
|
-
|
41
|
-
|
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
|
47
|
-
end
|
48
|
-
end
|
60
|
+
def memoized
|
61
|
+
@memoized ||= {}
|
49
62
|
end
|
50
63
|
end
|
51
64
|
|
data/spec/load_unload_spec.rb
CHANGED
@@ -42,5 +42,12 @@ describe SmartEnv, 'load/unload' do
|
|
42
42
|
@env.clear_registry
|
43
43
|
@env['FOO'].should_not respond_to :one
|
44
44
|
end
|
45
|
+
|
46
|
+
it "should clear memoized values" do
|
47
|
+
@env['FOO'] = 'test'
|
48
|
+
memoized = @env['FOO']
|
49
|
+
@env.clear_registry
|
50
|
+
@env['FOO'].object_id.should_not == memoized.object_id
|
51
|
+
end
|
45
52
|
end
|
46
53
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SmartEnv, 'memoizing values' do
|
4
|
+
before do
|
5
|
+
@env = {}
|
6
|
+
@env.extend SmartEnv
|
7
|
+
@env.use(SmartEnv::UriProxy)
|
8
|
+
@env['TEST'] = 'http://service.com'
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return the same object after calling" do
|
12
|
+
ENV['TEST'].object_id.should == ENV['TEST'].object_id
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_env
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 15424055
|
5
|
+
prerelease: 5
|
6
6
|
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
7
9
|
- 0
|
8
|
-
-
|
10
|
+
- rc
|
9
11
|
- 1
|
10
|
-
version: 0.
|
12
|
+
version: 1.0.0rc1
|
11
13
|
platform: ruby
|
12
14
|
authors:
|
13
15
|
- Chris Continanza
|
@@ -15,12 +17,11 @@ autorequire:
|
|
15
17
|
bindir: bin
|
16
18
|
cert_chain: []
|
17
19
|
|
18
|
-
date: 2011-10-
|
20
|
+
date: 2011-10-12 00:00:00 -07:00
|
19
21
|
default_executable:
|
20
22
|
dependencies:
|
21
23
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
23
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
25
|
none: false
|
25
26
|
requirements:
|
26
27
|
- - ~>
|
@@ -30,12 +31,12 @@ dependencies:
|
|
30
31
|
- 2
|
31
32
|
- 0
|
32
33
|
version: "2.0"
|
34
|
+
version_requirements: *id001
|
35
|
+
name: rspec
|
33
36
|
prerelease: false
|
34
37
|
type: :development
|
35
|
-
requirement: *id001
|
36
38
|
- !ruby/object:Gem::Dependency
|
37
|
-
|
38
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
40
|
none: false
|
40
41
|
requirements:
|
41
42
|
- - ">="
|
@@ -44,9 +45,10 @@ dependencies:
|
|
44
45
|
segments:
|
45
46
|
- 0
|
46
47
|
version: "0"
|
48
|
+
version_requirements: *id002
|
49
|
+
name: rcov
|
47
50
|
prerelease: false
|
48
51
|
type: :development
|
49
|
-
requirement: *id002
|
50
52
|
description: Allows you to register Proxy classes for ENV vars by using blocks to match keys and values
|
51
53
|
email:
|
52
54
|
- christopher.continanza@gmail.com
|
@@ -68,6 +70,7 @@ files:
|
|
68
70
|
- smart_env.gemspec
|
69
71
|
- spec/decoration_spec.rb
|
70
72
|
- spec/load_unload_spec.rb
|
73
|
+
- spec/memoize_spec.rb
|
71
74
|
- spec/registry_spec.rb
|
72
75
|
- spec/spec_helper.rb
|
73
76
|
- spec/uri_spec.rb
|
@@ -92,12 +95,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
96
|
none: false
|
94
97
|
requirements:
|
95
|
-
- - "
|
98
|
+
- - ">"
|
96
99
|
- !ruby/object:Gem::Version
|
97
|
-
hash:
|
100
|
+
hash: 25
|
98
101
|
segments:
|
99
|
-
-
|
100
|
-
|
102
|
+
- 1
|
103
|
+
- 3
|
104
|
+
- 1
|
105
|
+
version: 1.3.1
|
101
106
|
requirements: []
|
102
107
|
|
103
108
|
rubyforge_project: smart_env
|
@@ -108,6 +113,7 @@ summary: Lazily proxy ENV values
|
|
108
113
|
test_files:
|
109
114
|
- spec/decoration_spec.rb
|
110
115
|
- spec/load_unload_spec.rb
|
116
|
+
- spec/memoize_spec.rb
|
111
117
|
- spec/registry_spec.rb
|
112
118
|
- spec/spec_helper.rb
|
113
119
|
- spec/uri_spec.rb
|