dim 1.2.7 → 1.2.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ODMwNTEzMjE5MTNkYTFkMzA2MDkxZDQ3NDcxYWUyZTVhMjkzZGU3Yg==
5
- data.tar.gz: !binary |-
6
- NmNkYmZiYzZjYzU1OTU0ZDk3ZmQ5OTZmOTE4MGQzNGFlZjk3NzA2ZQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- MzQ4ZmU3ZGY0NDFkZGU3YTI4MGJlNGQ2MDBjZDIxOGIxNzZmNDBjOWE3MmE0
10
- MmJlNjZiNTRjNmI5ZGVmODQ2ZGQ0ZDBlZmJiNGM5ODViMGJjOGJjM2U2MmNl
11
- YTJlYWYyOWY0ZGRjOTQ0Zjg3YThhNDhkYzZhODgwMDA3NTJmYWY=
12
- data.tar.gz: !binary |-
13
- NTUzMWJhNmJlYzczNTAwNDQzZjhhMmI5MmJmMDI3MjAxMjEzODhmMmZiMjJm
14
- MmQyNGI0ZTdiYWFlZDM4ZjI5YzJhMWQ5ZDkxODU1NTQzZmQ5OTc4Yzk5ZjM0
15
- OTY5MTFlNjc5OTY0Y2MxZmYxYWUwYzg4MjEyNTA4MGY0MzY3M2E=
2
+ SHA1:
3
+ metadata.gz: c4782eddd2362acb00039030ff666c5adf30e3fa
4
+ data.tar.gz: 11cc13c319a4de4aede04ee05baadabee3c8021b
5
+ SHA512:
6
+ metadata.gz: ba10c081a2d720b3a076cf6ff8e89f2d16e888cff4857fc74ef90ed8b920593a8265bc9940fb87e848b737e143060473b19e5471fd0a4bcf3e30bcca041d5409
7
+ data.tar.gz: 8357f2778456f69a4d357a736eeff7072719543ad759803ad6d87623ca1e00f95304f382c727e8fe5d7a820e9eaa9080186c70636f82805e8d3c1f28a3664600
data/Gemfile CHANGED
@@ -2,4 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in ..gemspec
4
4
  gemspec
5
- gem "rspec-given", "< 3"
@@ -13,6 +13,7 @@ When testing your code, you can either stub out services on the container, or yo
13
13
 
14
14
  The following could be in a "lib.init.rb" file or in a Rails app, "config/initializers/container.rb":
15
15
 
16
+ ```ruby
16
17
  require "dim"
17
18
  require "logger"
18
19
  require 'game'
@@ -57,19 +58,26 @@ The following could be in a "lib.init.rb" file or in a Rails app, "config/initia
57
58
  # attempts to read ENV["API_PASSWORD"], otherwise makes sure that the parent container has
58
59
  # a service named api_password registered
59
60
  ServerContainer.register_env(:api_password)
60
-
61
+ ```
62
+
61
63
  Using the above code elsewhere in the app, when you want a reference to the app's logger object:
62
64
 
65
+ ```ruby
63
66
  ServerContainer.logger.info("I didn't have to setup my own logger")
67
+ ```
64
68
 
65
69
  Or if you wanted access to the game instance created during setup (which already is configured with everything it needs):
66
70
 
71
+ ```ruby
67
72
  current_game = ServerContainer.game
73
+ ```
68
74
 
69
75
  If you don't like creating even the one dependency on the global constant ServerContainer, you could
70
76
  inject ServerContainer itself into your objects like so:
71
77
 
78
+ ```ruby
72
79
  World.new(GameContainer)
80
+ ```
73
81
 
74
82
  ## More Background
75
83
 
data/Rakefile CHANGED
@@ -9,3 +9,6 @@ task :default => :spec
9
9
  task :spec do
10
10
  sh "rspec spec"
11
11
  end
12
+
13
+ desc "Build the gem, create a git tag, and push to git. If the build passes, CircleCI will publish to packagecloud"
14
+ task :release, [:remote] => %w(build release:guard_clean release:source_control_push)
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.summary = %q{Minimalistic dependency injection framework}
16
16
  s.description = %q{Minimalistic dependency injection framework keeps all of your object setup code in one place.}
17
17
  s.test_files = `git ls-files spec`.split("\n")
18
- s.add_development_dependency 'rspec'
19
- s.add_development_dependency 'rspec-given'
18
+ s.add_development_dependency "rspec"
19
+ s.add_development_dependency "rake"
20
20
  s.license = "MIT"
21
21
  end
data/lib/dim.rb CHANGED
@@ -58,9 +58,14 @@ module Dim
58
58
  # create the service on demand. It is recommended that symbols be
59
59
  # used as the name of a service.
60
60
  def register(name,raise_error_on_duplicate = true,&block)
61
- if @services[name] && raise_error_on_duplicate
62
- fail DuplicateServiceError, "Duplicate Service Name '#{name}'"
61
+ if @services[name]
62
+ if raise_error_on_duplicate
63
+ fail DuplicateServiceError, "Duplicate Service Name '#{name}'"
64
+ else # delete the service from the cache
65
+ @cache.delete(name)
66
+ end
63
67
  end
68
+
64
69
  @services[name] = block
65
70
 
66
71
  self.class.send(:define_method, name) do
@@ -72,15 +77,6 @@ module Dim
72
77
  register(name,false,&block)
73
78
  end
74
79
 
75
- # Given a list of services, check to see if they are available, returning true or false.
76
- def verify_dependencies(*names)
77
- names.all? do |name|
78
- respond_to?(name) || service_block(name)
79
- end
80
- rescue Dim::MissingServiceError
81
- false
82
- end
83
-
84
80
  # Lookup a service from ENV variables, or use a default if given; fall back to searching the container and its parents for a default value
85
81
  def register_env(name,default = nil)
86
82
  if value = ENV[name.to_s.upcase]
@@ -128,5 +124,26 @@ module Dim
128
124
  def self.service_block(name)
129
125
  fail(MissingServiceError, "Unknown Service '#{name}'")
130
126
  end
127
+
128
+ # Check to see if a custom method or service has been registered, returning true or false.
129
+ def service_exists?(name)
130
+ respond_to?(name) || service_block(name)
131
+ rescue Dim::MissingServiceError
132
+ false
133
+ end
134
+
135
+ # Given a list of services, check to see if they are available, returning true or false.
136
+ def verify_dependencies(*names)
137
+ names.all? { |name| service_exists?(name) }
138
+ end
139
+
140
+ # Given a list of services, check to see if they are available or raise an exception.
141
+ def verify_dependencies!(*names)
142
+ missing_dependencies = names.reject { |name| service_exists?(name) }
143
+
144
+ unless missing_dependencies.empty?
145
+ fail Dim::MissingServiceError, "Missing dependencies #{missing_dependencies.join(", ")}"
146
+ end
147
+ end
131
148
  end
132
149
  end
@@ -1,3 +1,3 @@
1
1
  module Dim
2
- VERSION = "1.2.7"
2
+ VERSION = "1.2.8".freeze
3
3
  end
@@ -1,5 +1,4 @@
1
- require 'dim'
2
- require 'rspec/given'
1
+ require "dim"
3
2
 
4
3
  class ConsoleAppender
5
4
  end
@@ -28,192 +27,233 @@ end
28
27
  describe Dim::Container do
29
28
  let(:container) { Dim::Container.new }
30
29
 
31
- Scenario "creating objects" do
32
- Given { container.register(:app) { App.new } }
33
- Then { container.app.should be_a(App) }
30
+ context "creating objects" do
31
+ before { container.register(:app) { App.new } }
32
+ specify { expect(container.app).to be_a(App) }
34
33
  end
35
34
 
36
- Scenario "returning the same object every time" do
37
- Given { container.register(:app) { App.new } }
38
- Given(:app) { container.app }
39
- Then { container.app.should be(app) }
35
+ context "returning the same object every time" do
36
+ let(:app) { container.app }
37
+ before { container.register(:app) { App.new } }
38
+ specify { expect(container.app).to be(app) }
40
39
  end
41
40
 
42
- Scenario "overriding previously-registered objects" do
43
- Given { container.register(:some_value) { "A" } }
44
- Given { container.override(:some_value) { "B" } }
45
- Then { container.some_value.should == "B" }
41
+ context "overriding previously-registered objects" do
42
+ before do
43
+ container.register(:some_value) { "A" }
44
+ container.override(:some_value) { "B" }
45
+ end
46
+
47
+ specify { expect(container.some_value).to eq("B") }
46
48
  end
47
49
 
48
- it "clears cache explicitly" do
49
- container.register(:app) { App.new }
50
- app_before = container.app
51
- container.clear_cache!
52
- app_after = container.app
53
- app_before.should_not == app_after
50
+ context "explicitly clearing cache" do
51
+ before do
52
+ container.register(:app) { App.new }
53
+ end
54
+
55
+ specify "returns new object" do
56
+ expect {
57
+ container.clear_cache! }.
58
+ to change { container.app }
59
+ end
54
60
  end
55
61
 
56
- Scenario "contructing dependent objects" do
57
- Given { container.register(:app) { |c| App.new(c.logger) } }
58
- Given { container.register(:logger) { Logger.new } }
59
- Given(:app) { container.app }
60
- Then { app.logger.should be(container.logger) }
62
+ context "contructing dependent objects" do
63
+ let(:app) { container.app }
64
+
65
+ before do
66
+ container.register(:app) { |c| App.new(c.logger) }
67
+ container.register(:logger) { Logger.new }
68
+ end
69
+
70
+ specify { expect(app.logger).to be(container.logger) }
61
71
  end
62
72
 
63
- Scenario "constructing dependent objects with setters" do
64
- Given {
65
- container.register(:app) { |c|
66
- App.new.tap { |obj|
67
- obj.db = c.database
68
- }
69
- }
70
- }
71
- Given { container.register(:database) { MockDB.new } }
72
- Given(:app) { container.app }
73
+ context "constructing dependent objects with setters" do
74
+ let(:app) { container.app }
75
+
76
+ before do
77
+ container.register(:app) do |c|
78
+ App.new.tap { |obj| obj.db = c.database }
79
+ end
80
+ container.register(:database) { MockDB.new }
81
+ end
73
82
 
74
- Then { app.db.should be(container.database) }
83
+ specify { expect(app.db).to be(container.database) }
75
84
  end
76
85
 
77
- Scenario "constructing multiple dependent objects" do
78
- Given {
79
- container.register(:app) { |c|
80
- App.new(c.logger).tap { |obj|
81
- obj.db = c.database
82
- }
83
- }
84
- }
85
- Given { container.register(:logger) { Logger.new } }
86
- Given { container.register(:database) { MockDB.new } }
87
- Given(:app) { container.app }
88
- Then { app.logger.should be(container.logger) }
89
- Then { app.db.should be(container.database) }
86
+ context "constructing multiple dependent objects" do
87
+ let(:app) { container.app }
88
+
89
+ before do
90
+ container.register(:app) do |c|
91
+ App.new(c.logger).tap { |obj| obj.db = c.database }
92
+ end
93
+ container.register(:logger) { Logger.new }
94
+ container.register(:database) { MockDB.new }
95
+ end
96
+
97
+ specify { expect(app.logger).to be(container.logger) }
98
+ specify { expect(app.db).to be(container.database) }
90
99
  end
91
100
 
92
- Scenario "constructing chains of dependencies" do
93
- Given { container.register(:app) { |c| App.new(c.logger) } }
94
- Given {
95
- container.register(:logger) { |c|
96
- Logger.new.tap { |obj|
97
- obj.appender = c.logger_appender
98
- }
99
- }
100
- }
101
- Given { container.register(:logger_appender) { ConsoleAppender.new } }
102
- Given { container.register(:database) { MockDB.new } }
103
- Given(:logger) { container.app.logger }
101
+ context "constructing chains of dependencies" do
102
+ let(:logger) { container.app.logger }
104
103
 
105
- Then { logger.appender.should be(container.logger_appender) }
104
+ before do
105
+ container.register(:app) { |c| App.new(c.logger) }
106
+ container.register(:logger) do |c|
107
+ Logger.new.tap { |obj| obj.appender = c.logger_appender }
108
+ end
109
+ container.register(:logger_appender) { ConsoleAppender.new }
110
+ container.register(:database) { MockDB.new }
111
+ end
112
+
113
+ specify { expect(logger.appender).to be(container.logger_appender) }
106
114
  end
107
115
 
108
- Scenario "constructing literals" do
109
- Given { container.register(:database) { |c| RealDB.new(c.username, c.userpassword) } }
110
- Given { container.register(:username) { "user_name_value" } }
111
- Given { container.register(:userpassword) { "password_value" } }
112
- Given(:db) { container.database }
116
+ context "constructing literals" do
117
+ let(:db) { container.database }
118
+
119
+ before do
120
+ container.register(:database) { |c| RealDB.new(c.username, c.userpassword) }
121
+ container.register(:username) { "user_name_value" }
122
+ container.register(:userpassword) { "password_value" }
123
+ end
113
124
 
114
- Then { db.username.should == "user_name_value" }
115
- Then { db.password.should == "password_value" }
125
+ specify { expect(db.username).to eq("user_name_value") }
126
+ specify { expect(db.password).to eq("password_value") }
116
127
  end
117
128
 
118
129
  describe "Errors" do
119
- Scenario "missing services" do
120
- Then {
121
- lambda {
122
- container.undefined_service_name
123
- }.should raise_error(Dim::MissingServiceError, /undefined_service_name/)
124
- }
125
- end
126
-
127
- Scenario "duplicate service names" do
128
- Given { container.register(:duplicate_name) { 0 } }
129
- Then {
130
- lambda {
130
+ specify "raise missing service error" do
131
+ expect {
132
+ container.undefined_service_name
133
+ }.to raise_error(Dim::MissingServiceError, /undefined_service_name/)
134
+ end
135
+
136
+ context "duplicate service names" do
137
+ before { container.register(:duplicate_name) { 0 } }
138
+
139
+ specify do
140
+ expect {
131
141
  container.register(:duplicate_name) { 0 }
132
- }.should raise_error(Dim::DuplicateServiceError, /duplicate_name/)
133
- }
142
+ }.to raise_error(Dim::DuplicateServiceError, /duplicate_name/)
143
+ end
134
144
  end
135
145
  end
136
146
 
137
147
  describe "Parent/Child Container Interaction" do
138
- Given(:parent) { container }
139
- Given(:child) { Dim::Container.new(parent) }
148
+ let(:parent) { container }
149
+ let(:child) { Dim::Container.new(parent) }
140
150
 
141
- Given { parent.register(:cell) { :parent_cell } }
142
- Given { parent.register(:gene) { :parent_gene } }
143
- Given { child.register(:gene) { :child_gene } }
151
+ before do
152
+ parent.register(:cell) { :parent_cell }
153
+ parent.register(:gene) { :parent_gene }
154
+ child.register(:gene) { :child_gene }
155
+ end
144
156
 
145
- Scenario "reusing a service from the parent" do
146
- Then { child.cell.should == :parent_cell }
157
+ context "reusing a service from the parent" do
158
+ specify { expect(child.cell).to eq(:parent_cell) }
147
159
  end
148
160
 
149
- Scenario "overiding a service from the parent" do
150
- Then "the child service overrides the parent" do
151
- child.gene.should == :child_gene
161
+ context "overiding a service from the parent" do
162
+ specify "the child service overrides the parent" do
163
+ expect(child.gene).to eq(:child_gene)
152
164
  end
153
165
  end
154
166
 
155
- Scenario "wrapping a service from a parent" do
156
- Given { child.register(:cell) { |c| [c.parent.cell] } }
157
- Then { child.cell.should == [:parent_cell] }
167
+ context "wrapping a service from a parent" do
168
+ before { child.register(:cell) { |c| [c.parent.cell] } }
169
+ specify { expect(child.cell).to eq([:parent_cell]) }
158
170
  end
159
171
 
160
- Scenario "overriding an indirect dependency" do
161
- Given { parent.register(:wrapped_cell) { |c| [c.cell] } }
162
- Given { child.register(:cell) { :child_cell } }
163
- Then { child.wrapped_cell.should == [:child_cell] }
172
+ context "overriding an indirect dependency" do
173
+ before do
174
+ parent.register(:wrapped_cell) { |c| [c.cell] }
175
+ child.register(:cell) { :child_cell }
176
+ end
177
+
178
+ specify { expect(child.wrapped_cell).to eq([:child_cell]) }
164
179
  end
165
180
 
166
- Scenario "parent / child service conflicts from parents view" do
167
- Then { parent.gene.should == :parent_gene }
181
+ context "parent / child service conflicts from parents view" do
182
+ specify { expect(parent.gene).to eq(:parent_gene) }
168
183
  end
169
184
 
170
- Scenario "child / child service name conflicts" do
171
- Given(:other_child) { Dim::Container.new(parent) }
172
- Given { other_child.register(:gene) { :other_child_gene } }
185
+ context "child / child service name conflicts" do
186
+ let(:other_child) { Dim::Container.new(parent) }
187
+
188
+ before { other_child.register(:gene) { :other_child_gene } }
173
189
 
174
- Then { child.gene.should == :child_gene }
175
- Then { other_child.gene.should == :other_child_gene }
190
+ specify { expect(child.gene).to eq(:child_gene) }
191
+ specify { expect(other_child.gene).to eq(:other_child_gene) }
176
192
  end
177
193
  end
178
194
 
179
195
  describe "Registering env variables" do
180
- Scenario "which exist in ENV" do
181
- Given { ENV["SHAZ"] = "bot" }
182
- Given { container.register_env(:shaz) }
183
- Then { container.shaz.should == "bot" }
196
+ context "which exist in ENV" do
197
+ before do
198
+ ENV["SHAZ"] = "bot"
199
+ container.register_env(:shaz)
200
+ end
201
+
202
+ specify { expect(container.shaz).to eq("bot") }
184
203
  end
185
204
 
186
- Scenario "which only exist in parent" do
187
- Given(:parent) { container }
188
- Given { parent.register(:foo) { "bar" } }
189
- Given(:child) { Dim::Container.new(parent) }
205
+ context "which only exist in parent" do
206
+ let(:parent) { container }
207
+ let(:child) { Dim::Container.new(parent) }
190
208
 
191
- Given { ENV["FOO"] = nil }
192
- Given { child.register_env(:foo) }
193
- Then { container.foo.should == "bar" }
209
+ before do
210
+ parent.register(:foo) { "bar" }
211
+ ENV["FOO"] = nil
212
+ child.register_env(:foo)
213
+ end
214
+
215
+ specify { expect(container.foo).to eq("bar") }
194
216
  end
195
217
 
196
- Scenario "which don't exist in ENV but have a default" do
197
- Given { container.register_env(:abc,"123") }
198
- Then { container.abc.should == "123" }
218
+ context "which don't exist in ENV but have a default" do
219
+ before { container.register_env(:abc,"123") }
220
+ specify { expect(container.abc).to eq("123") }
199
221
  end
200
222
 
201
- Scenario "which don't exist in optional hash" do
202
- Then {
203
- lambda {
223
+ context "which don't exist in optional hash" do
224
+ specify do
225
+ expect {
204
226
  container.register_env(:dont_exist_in_env_or_optional_hash)
205
- }.should raise_error(Dim::EnvironmentVariableNotFound)
206
- }
227
+ }.to raise_error(Dim::EnvironmentVariableNotFound)
228
+ end
207
229
  end
208
230
  end
209
231
 
210
- Scenario "verifying dependencies" do
211
- Given { container.register(:app) { :app } }
212
- Given {
213
- def container.custom_method
214
- end
215
- }
216
- Then { container.verify_dependencies(:app,:custom_method).should == true }
217
- Then { container.verify_dependencies(:app,:custom_method,:frobosh).should == false }
232
+ context "verifying dependencies" do
233
+ before { container.register(:app) { :app } }
234
+
235
+ specify { expect(container.verify_dependencies(:app)).to be_true }
236
+ specify { expect(container.verify_dependencies(:app,:frobosh)).to be_false }
237
+ end
238
+
239
+ context "check if service exists" do
240
+ before do
241
+ container.register(:app) { :app }
242
+ def container.custom_method; end
243
+ end
244
+
245
+ specify { expect(container.service_exists?(:app)).to be_true }
246
+ specify { expect(container.service_exists?(:custom_method)).to be_true }
247
+ specify { expect(container.service_exists?(:missing_app)).to be_false }
248
+ end
249
+
250
+ context "dangerously verifying dependencies" do
251
+ before { container.register(:app) { :app } }
252
+
253
+ specify { expect(container.verify_dependencies!(:app)).to be_nil }
254
+
255
+ specify "raise error with list of missing services" do
256
+ expect{ container.verify_dependencies!(:app,:missing_app) }.to raise_error(Dim::MissingServiceError,/missing_app/)
257
+ end
218
258
  end
219
259
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dim
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.7
4
+ version: 1.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Weirich
@@ -9,34 +9,34 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-06 00:00:00.000000000 Z
12
+ date: 2017-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ! '>='
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ! '>='
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: rspec-given
29
+ name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ! '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ! '>='
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  description: Minimalistic dependency injection framework keeps all of your object
@@ -47,9 +47,8 @@ extensions: []
47
47
  extra_rdoc_files:
48
48
  - README.markdown
49
49
  files:
50
- - .gitignore
51
- - .rspec
52
- - .ruby-version
50
+ - ".gitignore"
51
+ - ".rspec"
53
52
  - Gemfile
54
53
  - LICENSE
55
54
  - README.markdown
@@ -64,25 +63,24 @@ licenses:
64
63
  metadata: {}
65
64
  post_install_message:
66
65
  rdoc_options:
67
- - --charset=UTF-8
66
+ - "--charset=UTF-8"
68
67
  require_paths:
69
68
  - lib
70
69
  required_ruby_version: !ruby/object:Gem::Requirement
71
70
  requirements:
72
- - - ! '>='
71
+ - - ">="
73
72
  - !ruby/object:Gem::Version
74
73
  version: '0'
75
74
  required_rubygems_version: !ruby/object:Gem::Requirement
76
75
  requirements:
77
- - - ! '>='
76
+ - - ">="
78
77
  - !ruby/object:Gem::Version
79
78
  version: '0'
80
79
  requirements: []
81
80
  rubyforge_project:
82
- rubygems_version: 2.0.6
81
+ rubygems_version: 2.6.12
83
82
  signing_key:
84
83
  specification_version: 4
85
84
  summary: Minimalistic dependency injection framework
86
85
  test_files:
87
86
  - spec/dim_spec.rb
88
- has_rdoc:
@@ -1 +0,0 @@
1
- ruby-1.9.3