dim 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1 +1 @@
1
- rvm use 1.9.2-p136@dim --create
1
+ rvm use 1.9.3
data/Gemfile CHANGED
@@ -1,2 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ..gemspec
2
4
  gemspec
@@ -1,10 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dim (0.9.1)
4
+ dim (1.0.0)
5
5
 
6
6
  GEM
7
- remote: http://rubygems.org/
7
+ remote: https://rubygems.org/
8
8
  specs:
9
9
  diff-lcs (1.1.2)
10
10
  rspec (2.5.0)
@@ -1,4 +1,6 @@
1
- Copyright (c) 2004, 2010 Jim Weirich
1
+ Copyright (c) 2012 Mike Subelsky
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
@@ -17,5 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,6 +1,6 @@
1
1
  # DIM: Dependency Injection - Minimal
2
2
 
3
- DIM is [Jim Weirich's](http://onestepback.org) minimalistic dependency injection framework, maintained in
3
+ DIM is [Jim Weirich's](http://onestepback.org) minimalistic dependency injection framework, maintained in
4
4
  gem form by [Mike Subelsky](http://subelsky.com).
5
5
 
6
6
  Dependency injection lets you organize all of your app's object setup code in one place by creating a
@@ -33,7 +33,7 @@ The following could be in a "lib.init.rb" file or in a Rails app, "config/initia
33
33
  ServerContainer.register(:listening_host) { "0.0.0.0" }
34
34
  ServerContainer.register(:listening_port) { "8080" }
35
35
 
36
- ServerContainer.register(:game) do |c|
36
+ ServerContainer.register(:game) do |c|
37
37
  game = Game.new
38
38
  game.logger = c.logger
39
39
  game.event_handler = c.event_handler
@@ -54,6 +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"], returns "FAKE_PASSWORD" if it can't be found
58
+ ServerContainer.register_env(:api_password,"FAKE_PASSWORD")
59
+
57
60
  Using the above code elsewhere in the app, when you want a reference to the app's logger object:
58
61
 
59
62
  ServerContainer.logger.info("I didn't have to setup my own logger")
@@ -62,11 +65,11 @@ Or if you wanted access to the game instance created during setup (which already
62
65
 
63
66
  current_game = ServerContainer.game
64
67
 
65
- If you don't like creating even the one dependency on the global constant ServerContainer, you could
68
+ If you don't like creating even the one dependency on the global constant ServerContainer, you could
66
69
  inject ServerContainer itself into your objects like so:
67
-
70
+
68
71
  World.new(GameContainer)
69
-
72
+
70
73
  ## More Background
71
74
 
72
75
  Jim wrote a [nice article](http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc) explaining
@@ -74,4 +77,4 @@ the rationale for this code and how it works. Also check out [his slides](http:/
74
77
 
75
78
  # License
76
79
 
77
- DIM is available under the MIT license (see the file MIT-LICENSE for details).
80
+ Dim is available under the MIT license (see the file LICENSE for details).
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
- #!/usr/bin/env ruby"
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
2
3
 
3
4
  require 'rake/clean'
4
5
  require 'rake/testtask'
@@ -1,5 +1,5 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
- require 'lib/dim/version'
2
+ require 'lib/version'
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = %q{dim}
@@ -17,4 +17,5 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files spec`.split("\n")
18
18
  s.add_development_dependency 'rspec'
19
19
  s.add_development_dependency 'rspec-given'
20
+ s.license = "MIT"
20
21
  end
data/lib/dim.rb CHANGED
@@ -29,10 +29,13 @@
29
29
  #
30
30
  module Dim
31
31
  # Thrown when a service cannot be located by name.
32
- class MissingServiceError < StandardError; end
32
+ MissingServiceError = Class.new(StandardError)
33
33
 
34
34
  # Thrown when a duplicate service is registered.
35
- class DuplicateServiceError < StandardError; end
35
+ DuplicateServiceError = Class.new(StandardError)
36
+
37
+ # Thrown by register_env when a suitable ENV variable can't be found
38
+ EnvironmentVariableNotFound = Class.new(StandardError)
36
39
 
37
40
  # Dim::Container is the central data store for registering services
38
41
  # used for dependency injuction. Users register services by
@@ -61,6 +64,13 @@ module Dim
61
64
  @services[name] = block
62
65
  end
63
66
 
67
+ # Lookup a service from ENV variables; fall back to searching the provided hash
68
+ # if the
69
+ def register_env(name,default = nil)
70
+ value = ENV[name.to_s.upcase] || default || raise(EnvironmentVariableNotFound, "Cannot find any ENV variable named #{name}")
71
+ register(name) { value }
72
+ end
73
+
64
74
  # Lookup a service by name. Throw an exception if no service is
65
75
  # found.
66
76
  def [](name)
@@ -0,0 +1,3 @@
1
+ module Dim
2
+ VERSION = "1.1.0"
3
+ end
@@ -162,4 +162,25 @@ describe Dim::Container do
162
162
  end
163
163
  end
164
164
 
165
+ describe "Registering env variables" do
166
+ Scenario "which exist in ENV" do
167
+ Given { ENV["SHAZ"] = "bot" }
168
+ Given { container.register_env(:shaz) }
169
+ Then { container.shaz.should == "bot" }
170
+ end
171
+
172
+ Scenario "which exist in optional hash" do
173
+ Given { ENV["SHAZ"] = nil }
174
+ Given { container.register_env(:shaz,"test-bot") }
175
+ Then { container.shaz.should == "test-bot" }
176
+ end
177
+
178
+ Scenario "which don't exist in optional hash" do
179
+ Then {
180
+ lambda {
181
+ container.register_env(:dont_exist_in_env_or_optional_hash)
182
+ }.should raise_error(Dim::EnvironmentVariableNotFound)
183
+ }
184
+ end
185
+ end
165
186
  end
metadata CHANGED
@@ -1,87 +1,94 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: dim
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
4
5
  prerelease:
5
- version: 1.0.0
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Jim Weirich
9
9
  - Mike Subelsky
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
-
14
- date: 2011-05-18 00:00:00 Z
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
13
+ date: 2012-06-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
17
16
  name: rspec
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
20
18
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: "0"
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
25
23
  type: :development
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
28
- name: rspec-given
29
24
  prerelease: false
30
- requirement: &id002 !ruby/object:Gem::Requirement
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rspec-given
33
+ requirement: !ruby/object:Gem::Requirement
31
34
  none: false
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: "0"
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
36
39
  type: :development
37
- version_requirements: *id002
38
- description: Minimalistic dependency injection framework keeps all of your object setup code in one place.
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Minimalistic dependency injection framework keeps all of your object
48
+ setup code in one place.
39
49
  email: mike@subelsky.com
40
50
  executables: []
41
-
42
51
  extensions: []
43
-
44
- extra_rdoc_files:
52
+ extra_rdoc_files:
45
53
  - README.markdown
46
- files:
54
+ files:
47
55
  - .gitignore
48
56
  - .rspec
49
57
  - .rvmrc
50
58
  - Gemfile
51
59
  - Gemfile.lock
52
- - MIT-LICENSE
60
+ - LICENSE
53
61
  - README.markdown
54
62
  - Rakefile
55
63
  - dim.gemspec
56
64
  - lib/dim.rb
57
- - lib/dim/version.rb
65
+ - lib/version.rb
58
66
  - spec/dim_spec.rb
59
67
  homepage: http://github.com/subelsky/dim
60
- licenses: []
61
-
68
+ licenses:
69
+ - MIT
62
70
  post_install_message:
63
- rdoc_options:
71
+ rdoc_options:
64
72
  - --charset=UTF-8
65
- require_paths:
73
+ require_paths:
66
74
  - lib
67
- required_ruby_version: !ruby/object:Gem::Requirement
75
+ required_ruby_version: !ruby/object:Gem::Requirement
68
76
  none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: "0"
73
- required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
82
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: "0"
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
79
87
  requirements: []
80
-
81
88
  rubyforge_project:
82
- rubygems_version: 1.8.2
89
+ rubygems_version: 1.8.24
83
90
  signing_key:
84
91
  specification_version: 3
85
92
  summary: Minimalistic dependency injection framework
86
- test_files:
93
+ test_files:
87
94
  - spec/dim_spec.rb
@@ -1,3 +0,0 @@
1
- module Dim
2
- VERSION = "1.0.0"
3
- end