service_bureau 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +76 -1
- data/lib/service_bureau/locator.rb +5 -0
- data/lib/service_bureau/version.rb +1 -1
- data/spec/service_bureau_spec.rb +26 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MzZkOGZmNWI1Y2FkMjU5OGJiODNlZDUzN2MzZWQ2ODNiYzA0ZDgwYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjNiZWYxMTVkMzIwM2U4ZGE4MTUzMDZmMjAzYmIwODkzZTVmMGY1Zg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MWNkOTdiZDFkYzNjMTYwNjBmMGY4MDViOGMyNmY3YTI4YjJiZTg2NGM4Y2Zk
|
10
|
+
NTA5Nzg1OWM3MWM5Y2I3ZTJmNzgyYTEzYjkxZjMxZmE4YTkwZGFmZmRjNDkx
|
11
|
+
MGE4MjBjZjdkNmNhMjA3OGRiM2RlMWVhNDdkYTlmYWEyYjBmMjM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NWIxM2JiNmEwNjVjNjFhMDhhNGI3NTQ2ZmYwYzIyODk4OGViNWNjNzdkZmVm
|
14
|
+
ZDUwMjg5OGI1NzYyZTk4YWRjZGQ2MTMzNzNjN2Q1MTIzMTRkMWM2MDIwMTlh
|
15
|
+
YzU3MTljMTUwNmQ4Njg0OTcyMzkyNzllNzY5NzQ0ZjcxNmIyNTM=
|
data/README.md
CHANGED
@@ -27,6 +27,8 @@ A factory can be any object that responds to `#call` - a Proc, lambda, method ob
|
|
27
27
|
|
28
28
|
If your service requires arguments to initialize it, make sure the proc accepts them.
|
29
29
|
|
30
|
+
If you are using Rails, this would go into an initializer.
|
31
|
+
|
30
32
|
```ruby
|
31
33
|
ServiceBureau::Locations.configure do |config|
|
32
34
|
config.my_service ->{ MyService.instance }
|
@@ -83,7 +85,18 @@ describe MyClass
|
|
83
85
|
end
|
84
86
|
```
|
85
87
|
|
86
|
-
###
|
88
|
+
### An instance method lookup
|
89
|
+
```ruby
|
90
|
+
class MyClass
|
91
|
+
def my_method
|
92
|
+
locator = ServiceBureau::Locator.new
|
93
|
+
service = locator.get_service(:my_service, 'any', 'arguments')
|
94
|
+
result = service.do_something
|
95
|
+
end
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
### A class method lookup
|
87
100
|
```ruby
|
88
101
|
class MyClass
|
89
102
|
def my_method
|
@@ -93,6 +106,8 @@ class MyClass
|
|
93
106
|
end
|
94
107
|
```
|
95
108
|
|
109
|
+
### Notes
|
110
|
+
|
96
111
|
The handle to the instance will memoize it, and only instantiate it the first time it is called.
|
97
112
|
If you need a new instance, you can use the injector to clear out the old one,
|
98
113
|
or you can use the class method on Locator to get a new instance (and you can use the injector
|
@@ -118,6 +133,66 @@ class MyClass
|
|
118
133
|
end
|
119
134
|
```
|
120
135
|
|
136
|
+
### Rails Example with services that need services.
|
137
|
+
|
138
|
+
Yo dawg! I heard you like services, so I put a service in your service.
|
139
|
+
This is helpful if you like keeping you application amde up of small, composable, well-focused pieces of functionality.
|
140
|
+
```ruby
|
141
|
+
# config/initializers/services.rb
|
142
|
+
ServiceBureau::Locations.configure do |config|
|
143
|
+
config.my_service_a MyServiceA.public_method(:new)
|
144
|
+
config.my_service_b MyServiceB.public_method(:new)
|
145
|
+
config.my_service_c MyServiceC.public_method(:new)
|
146
|
+
end
|
147
|
+
|
148
|
+
# app/services/my_service_a.rb
|
149
|
+
class MyServiceA
|
150
|
+
def work
|
151
|
+
puts "service a"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
# app/services/my_service_b.rb
|
156
|
+
class MyServiceB
|
157
|
+
include ServiceBureau::Services
|
158
|
+
|
159
|
+
def work
|
160
|
+
puts "service b"
|
161
|
+
puts my_service_c.work
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# app/services/my_service_c.rb
|
166
|
+
class MyServiceC
|
167
|
+
def work
|
168
|
+
puts "service c"
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# app/models/client.rb
|
173
|
+
class Client
|
174
|
+
include ServiceBureau::Services
|
175
|
+
|
176
|
+
def do_something
|
177
|
+
puts my_service_a.work
|
178
|
+
puts my_service_b.work
|
179
|
+
end
|
180
|
+
end
|
181
|
+
```
|
182
|
+
|
183
|
+
```
|
184
|
+
> client = Client.new
|
185
|
+
# => #<Client:0x007deadbeef000>
|
186
|
+
> client.do_something
|
187
|
+
|
188
|
+
# =>
|
189
|
+
service a
|
190
|
+
|
191
|
+
service b
|
192
|
+
service c
|
193
|
+
```
|
194
|
+
|
195
|
+
|
121
196
|
|
122
197
|
## Contributing
|
123
198
|
|
@@ -4,6 +4,11 @@ module ServiceBureau
|
|
4
4
|
@service_factories = ServiceBureau::Locations.factory_map
|
5
5
|
end
|
6
6
|
|
7
|
+
def self.get_service service_key, *args
|
8
|
+
@instance ||= new
|
9
|
+
@instance.get_service service_key, *args
|
10
|
+
end
|
11
|
+
|
7
12
|
def get_service service_key, *args
|
8
13
|
service_factories.fetch(service_key).call *args
|
9
14
|
|
data/spec/service_bureau_spec.rb
CHANGED
@@ -39,6 +39,32 @@ describe ServiceBureau::Locator do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
describe '.get_service' do
|
44
|
+
it "gets a configured service" do
|
45
|
+
found_svc = ServiceBureau::Locator.get_service :some_service
|
46
|
+
expect(found_svc).to equal a_service_obj
|
47
|
+
end
|
48
|
+
|
49
|
+
it "instantiates a service with args given" do
|
50
|
+
expect(factory).to receive(:call).with('foo')
|
51
|
+
ServiceBureau::Locator.get_service :other_service, 'foo'
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when no service is registered' do
|
55
|
+
it "raises UnknownServiceError" do
|
56
|
+
expect{ ServiceBureau::Locator.get_service :no_such_svc }
|
57
|
+
.to raise_error(ServiceBureau::UnknownServiceError)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when the factory registered is not callable' do
|
62
|
+
it "raises UncallableFactoryError" do
|
63
|
+
expect{ ServiceBureau::Locator.get_service :bones_mccoy }
|
64
|
+
.to raise_error(ServiceBureau::UncallableFactoryError)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
42
68
|
end
|
43
69
|
|
44
70
|
describe ServiceBureau::Locations do
|