garcon 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70e6459dde2b9a208c1fd89d5917e7818443d2e2
4
- data.tar.gz: 739061b54a73366276729161eeb18288437d7a38
3
+ metadata.gz: 16b8e6f63d6d342e4670b5423de18da7f4facb14
4
+ data.tar.gz: c32746bb3ddb05071dd9c5ea586c071c95b3e8a3
5
5
  SHA512:
6
- metadata.gz: bf265524c8e31f50f9b59cd4ddae995abe4b0b1f06ef3a8904e33554df4305c86e2253e5567ae8e420241cb8968ff3d51e4d4b0d7230cbefc11703d67bf1428e
7
- data.tar.gz: 881113987fceac165fe1c8998bbadc26e6b5ec4264dcc31c0f81adf8532dd56d8fc021869903b77405fe30c0d9cf356ea6bd83613310764a30f90add097c1d23
6
+ metadata.gz: 3157455c1f221d7b703ecc7f63e52a436f6aacc638427a559edd79c6e002510b275336a6588c3f64164e7ab480e52dde1e34035dbfd9af8d6e14d1a515679e06
7
+ data.tar.gz: 700ce985f2bb2444477461e3151782f00cfb6be87a133d912ec7d88b06615168f80849320c31e718b2051d765c444d1ae48df22208b98f706c91105b686a6279
@@ -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(:email_server) { MyEmailServer.new(configuration_details) }
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 FileStorage < Struct.new(:services)
39
- def store_file file
40
- services[:file_server].store file, path: DEFAULT_PATH
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
 
@@ -7,7 +7,8 @@ module Garcon
7
7
  end
8
8
 
9
9
  def [] service_name
10
- @services[service_name.to_s].call
10
+ service = @services[service_name.to_s]
11
+ service.nil? ? Object.const_get(service_name.to_s) : service.call
11
12
  end
12
13
 
13
14
  def register service_name, &handler
@@ -1,3 +1,3 @@
1
1
  module Garcon
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -30,4 +30,8 @@ describe Garcon::ServiceLocator do
30
30
  subject['dynamic'].must_equal value1
31
31
  subject['dynamic'].must_equal value2
32
32
  end
33
+
34
+ it "retrieves a class dynamically" do
35
+ subject["String"].must_equal String
36
+ end
33
37
  end
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.0
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-02-12 00:00:00.000000000 Z
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.3
108
+ rubygems_version: 2.4.6
108
109
  signing_key:
109
110
  specification_version: 4
110
111
  summary: A simple Service Locator class