dim 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.markdown +3 -2
- data/lib/dim.rb +15 -9
- data/lib/version.rb +1 -1
- data/spec/dim_spec.rb +7 -3
- metadata +1 -1
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -54,8 +54,9 @@ The following could be in a "lib.init.rb" file or in a Rails app, "config/initia
|
|
54
54
|
Logger.new(c.log_file_path)
|
55
55
|
end
|
56
56
|
|
57
|
-
# attempts to read ENV["API_PASSWORD"],
|
58
|
-
|
57
|
+
# attempts to read ENV["API_PASSWORD"], otherwise makes sure that the parent container has
|
58
|
+
# a service named api_password registered
|
59
|
+
ServerContainer.register_env(:api_password)
|
59
60
|
|
60
61
|
Using the above code elsewhere in the app, when you want a reference to the app's logger object:
|
61
62
|
|
data/lib/dim.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
#--
|
3
|
-
# Copyright 2004, 2005, 2010 by Jim Weirich (jim.weirich@gmail.com)
|
2
|
+
# Copyright 2004, 2005, 2010, 2012 by Jim Weirich (jim.weirich@gmail.com)
|
3
|
+
# and Mike Subelsky (mike@subelsky.com)
|
4
|
+
#
|
4
5
|
# All rights reserved.
|
5
6
|
#
|
6
|
-
# This software is available under the MIT license. See the
|
7
|
-
# MIT-LICENSE file for details.
|
7
|
+
# This software is available under the MIT license. See the LICENSE file for details.
|
8
8
|
#++
|
9
9
|
#
|
10
10
|
# = Dependency Injection - Minimal (DIM)
|
@@ -64,11 +64,17 @@ module Dim
|
|
64
64
|
@services[name] = block
|
65
65
|
end
|
66
66
|
|
67
|
-
# Lookup a service from ENV variables; fall back to searching the
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
67
|
+
# Lookup a service from ENV variables; fall back to searching the container and its parents for a default value
|
68
|
+
def register_env(name)
|
69
|
+
if value = ENV[name.to_s.upcase]
|
70
|
+
register(name) { value }
|
71
|
+
else
|
72
|
+
begin
|
73
|
+
@parent.service_block(name)
|
74
|
+
rescue MissingServiceError
|
75
|
+
raise EnvironmentVariableNotFound, "Could not find an ENV variable named #{name.to_s.upcase} nor could we find a service named #{name} in the parent container"
|
76
|
+
end
|
77
|
+
end
|
72
78
|
end
|
73
79
|
|
74
80
|
# Lookup a service by name. Throw an exception if no service is
|
data/lib/version.rb
CHANGED
data/spec/dim_spec.rb
CHANGED
@@ -170,9 +170,13 @@ describe Dim::Container do
|
|
170
170
|
end
|
171
171
|
|
172
172
|
Scenario "which exist in optional hash" do
|
173
|
-
Given {
|
174
|
-
Given {
|
175
|
-
|
173
|
+
Given(:parent) { container }
|
174
|
+
Given { parent.register(:foo) { "bar" } }
|
175
|
+
Given(:child) { Dim::Container.new(parent) }
|
176
|
+
|
177
|
+
Given { ENV["FOO"] = nil }
|
178
|
+
Given { child.register_env(:foo) }
|
179
|
+
Then { container.foo.should == "bar" }
|
176
180
|
end
|
177
181
|
|
178
182
|
Scenario "which don't exist in optional hash" do
|