garcon 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/README.md +40 -9
- data/lib/garcon/service_locator.rb +2 -1
- data/lib/garcon/version.rb +1 -1
- data/test/garcon/service_locator_test.rb +4 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16b8e6f63d6d342e4670b5423de18da7f4facb14
|
4
|
+
data.tar.gz: c32746bb3ddb05071dd9c5ea586c071c95b3e8a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3157455c1f221d7b703ecc7f63e52a436f6aacc638427a559edd79c6e002510b275336a6588c3f64164e7ab480e52dde1e34035dbfd9af8d6e14d1a515679e06
|
7
|
+
data.tar.gz: 700ce985f2bb2444477461e3151782f00cfb6be87a133d912ec7d88b06615168f80849320c31e718b2051d765c444d1ae48df22208b98f706c91105b686a6279
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.0
|
data/README.md
CHANGED
@@ -2,6 +2,31 @@
|
|
2
2
|
|
3
3
|
Garcon is a simple Service Locator object.
|
4
4
|
|
5
|
+
Why?
|
6
|
+
|
7
|
+
Dependencies, that's why.
|
8
|
+
|
9
|
+
Each object in your application has dependencies and related objects.
|
10
|
+
|
11
|
+
For a good design, you need to know those dependencies and keep track of
|
12
|
+
them.
|
13
|
+
|
14
|
+
For a flexible design it's useful to be able to switch those
|
15
|
+
dependencies in and out.
|
16
|
+
|
17
|
+
So instead of hard-coding your dependencies or injecting them as a long
|
18
|
+
trail of constructor parameters, use a service locator;
|
19
|
+
register your services in the locator and tell it to fetch one for you
|
20
|
+
when you need it.
|
21
|
+
|
22
|
+
Your dependency map becomes a list of services - :file_server,
|
23
|
+
:email_server etc.
|
24
|
+
|
25
|
+
Your implementation can be switched out at a moment's notice without
|
26
|
+
affecting anything else.
|
27
|
+
|
28
|
+
You can go home relaxed and have a nice cup of tea.
|
29
|
+
|
5
30
|
## Installation
|
6
31
|
|
7
32
|
Add this line to your application's Gemfile:
|
@@ -26,20 +51,19 @@ services with it:
|
|
26
51
|
```ruby
|
27
52
|
services = Garcon::ServiceLocator.new
|
28
53
|
|
29
|
-
services.register(:file_server) { MyFileServer.new }
|
30
|
-
services.register(:
|
31
|
-
services.register(:hostname) { "mysite.example.com" }
|
54
|
+
services.register(:file_server) { MyFileServer.new(services) }
|
55
|
+
services.register(:path_finder) { MyPathFinder.new(services) }
|
32
56
|
```
|
33
57
|
|
34
58
|
Then use this Service Locator to find your related objects, instead of
|
35
|
-
having hard-coded constants.
|
59
|
+
having hard-coded constants. For example, MyFileServer never needs to
|
60
|
+
know how the Path Finder is implemented; it just needs to respond to #default.
|
36
61
|
|
37
62
|
```ruby
|
38
|
-
class
|
39
|
-
def
|
40
|
-
|
63
|
+
class MyFileServer < Struct.new(:services)
|
64
|
+
def store file
|
65
|
+
write file: file, path: services[:path_finder].default
|
41
66
|
end
|
42
|
-
DEFAULT_PATH = '/home/someone/somewhere';
|
43
67
|
end
|
44
68
|
|
45
69
|
file_storage = FileStorage.new(services)
|
@@ -52,7 +76,7 @@ in your application's configuration:
|
|
52
76
|
|
53
77
|
```ruby
|
54
78
|
services = Garcon::ServiceLocator.new
|
55
|
-
services.register(:thing_server) { ThingServer.new }
|
79
|
+
services.register(:thing_server) { ThingServer.new(services) }
|
56
80
|
MyStuff::Application.config.services = services
|
57
81
|
```
|
58
82
|
|
@@ -81,6 +105,13 @@ class MyThingsController < ApplicationController
|
|
81
105
|
end
|
82
106
|
```
|
83
107
|
|
108
|
+
Alternatively, the default behaviour is to try to load a class, based
|
109
|
+
upon the service name you have passed to it:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
services['String'].class #=> String
|
113
|
+
```
|
114
|
+
|
84
115
|
|
85
116
|
## Contributing
|
86
117
|
|
data/lib/garcon/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: garcon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rahoul Baruah
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,6 +74,7 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- ".gitignore"
|
77
|
+
- ".ruby-version"
|
77
78
|
- Gemfile
|
78
79
|
- LICENSE.txt
|
79
80
|
- README.md
|
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
105
|
version: '0'
|
105
106
|
requirements: []
|
106
107
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.4.
|
108
|
+
rubygems_version: 2.4.6
|
108
109
|
signing_key:
|
109
110
|
specification_version: 4
|
110
111
|
summary: A simple Service Locator class
|