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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjdhMDFkZTZlNTMxMWMyMzA1YTRlMDQ5ODU1Yzg1ZjQyMDE3YTFlZg==
4
+ MzZkOGZmNWI1Y2FkMjU5OGJiODNlZDUzN2MzZWQ2ODNiYzA0ZDgwYg==
5
5
  data.tar.gz: !binary |-
6
- OGE4OTI2NDI3MTE2MWZiODM0MGE1YTZjZWU1M2RjYzY0MzcyMzM5ZQ==
6
+ ZjNiZWYxMTVkMzIwM2U4ZGE4MTUzMDZmMjAzYmIwODkzZTVmMGY1Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NWUzY2IyMDQ5MjVjOTNlNDYxYWM2Nzk4NjkwOWIwNTMyYWFjYjE5NDJiZjVi
10
- ZjkyNjBhYjhlMzU4Y2U5MDVmNWRiYmYyMWZjNDRmZTdlNTNjNWRiZDkzZjU4
11
- MGY1ZTc5NzQ1MzA4Njg5MjRlMDYxMWNlZWZkOWRmYzZjYzEzMTQ=
9
+ MWNkOTdiZDFkYzNjMTYwNjBmMGY4MDViOGMyNmY3YTI4YjJiZTg2NGM4Y2Zk
10
+ NTA5Nzg1OWM3MWM5Y2I3ZTJmNzgyYTEzYjkxZjMxZmE4YTkwZGFmZmRjNDkx
11
+ MGE4MjBjZjdkNmNhMjA3OGRiM2RlMWVhNDdkYTlmYWEyYjBmMjM=
12
12
  data.tar.gz: !binary |-
13
- NWM0YzgwZDc5NmEwMDU4ODIyMzg1ZGE4YmQ2NjczOThmZDg2M2Y2ZDhjNmE1
14
- ZDRiOWQ1Y2Y5MDNmNzdlZDIzM2UwMzg1NWI1MDJhODA1NDQ5OGMxMGIyYmVm
15
- MWJkNmM2ZGI5NDhjNjJjODVmODhkNzhkYjY3NzM5YzA3YjI2ZmE=
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
- ### As a regular class method lookup
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
 
@@ -1,3 +1,3 @@
1
1
  module ServiceBureau
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_bureau
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Van Horn