garcon 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 70e6459dde2b9a208c1fd89d5917e7818443d2e2
4
+ data.tar.gz: 739061b54a73366276729161eeb18288437d7a38
5
+ SHA512:
6
+ metadata.gz: bf265524c8e31f50f9b59cd4ddae995abe4b0b1f06ef3a8904e33554df4305c86e2253e5567ae8e420241cb8968ff3d51e4d4b0d7230cbefc11703d67bf1428e
7
+ data.tar.gz: 881113987fceac165fe1c8998bbadc26e6b5ec4264dcc31c0f81adf8532dd56d8fc021869903b77405fe30c0d9cf356ea6bd83613310764a30f90add097c1d23
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in garcon.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Rahoul Baruah
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Garcon
2
+
3
+ Garcon is a simple Service Locator object.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'garcon'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install garcon
20
+
21
+ ## Usage
22
+
23
+ Create an instance of your service locator, and then register your
24
+ services with it:
25
+
26
+ ```ruby
27
+ services = Garcon::ServiceLocator.new
28
+
29
+ services.register(:file_server) { MyFileServer.new }
30
+ services.register(:email_server) { MyEmailServer.new(configuration_details) }
31
+ services.register(:hostname) { "mysite.example.com" }
32
+ ```
33
+
34
+ Then use this Service Locator to find your related objects, instead of
35
+ having hard-coded constants.
36
+
37
+ ```ruby
38
+ class FileStorage < Struct.new(:services)
39
+ def store_file file
40
+ services[:file_server].store file, path: DEFAULT_PATH
41
+ end
42
+ DEFAULT_PATH = '/home/someone/somewhere';
43
+ end
44
+
45
+ file_storage = FileStorage.new(services)
46
+ file_storage.store_file my_file
47
+ ```
48
+
49
+ If you're writing a Rails app I like to configure it like so - create
50
+ your services object in config/initializers/services.rb. Then store it
51
+ in your application's configuration:
52
+
53
+ ```ruby
54
+ services = Garcon::ServiceLocator.new
55
+ services.register(:thing_server) { ThingServer.new }
56
+ MyStuff::Application.config.services = services
57
+ ```
58
+
59
+ Then, in your application controller, add a protected method to access
60
+ it:
61
+
62
+ ```ruby
63
+ class ApplicationController < ActionController::Base
64
+
65
+ protected
66
+
67
+ def services
68
+ MyStuff::Application.config.services
69
+ end
70
+ end
71
+ ```
72
+
73
+ And finally you can then use it in your controllers as you wish (and
74
+ then pass it on to everything else):
75
+
76
+ ```ruby
77
+ class MyThingsController < ApplicationController
78
+ def index
79
+ @things = services[:thing_server].find_all
80
+ end
81
+ end
82
+ ```
83
+
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it ( https://github.com/rahoulb/garcon/fork )
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ end
7
+
data/garcon.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'garcon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "garcon"
8
+ spec.version = Garcon::VERSION
9
+ spec.authors = ["Rahoul Baruah"]
10
+ spec.email = ["rahoulb@madeofstone.net"]
11
+ spec.summary = %q{A simple Service Locator class}
12
+ spec.description = %q{}
13
+ spec.homepage = "http://github.com/rahoulb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.5.0"
24
+ spec.add_development_dependency "mocha", "~> 1.1.0"
25
+ end
@@ -0,0 +1,17 @@
1
+ module Garcon
2
+ class ServiceLocator
3
+
4
+ def initialize
5
+ super
6
+ @services = {}
7
+ end
8
+
9
+ def [] service_name
10
+ @services[service_name.to_s].call
11
+ end
12
+
13
+ def register service_name, &handler
14
+ @services[service_name.to_s] = handler
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Garcon
2
+ VERSION = "0.1.0"
3
+ end
data/lib/garcon.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "garcon/version"
2
+ require "garcon/service_locator"
3
+
4
+ module Garcon
5
+
6
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../test_helper'
2
+ require_relative '../../lib/garcon/service_locator'
3
+
4
+ describe Garcon::ServiceLocator do
5
+ subject { Garcon::ServiceLocator.new }
6
+
7
+ CONSTANT = "HELLO".freeze
8
+ let(:factory) { stub 'Factory' }
9
+ let(:value1) { stub 'Value1' }
10
+ let(:value2) { stub 'Value2' }
11
+
12
+ it "registers constants" do
13
+ subject.register "constant" do
14
+ CONSTANT
15
+ end
16
+
17
+ subject['constant'].must_equal CONSTANT
18
+ end
19
+
20
+ it "registers dynamic objects" do
21
+ factory.expects(:new).with(1).returns(value1)
22
+ factory.expects(:new).with(2).returns(value2)
23
+
24
+ count = 0
25
+ subject.register 'dynamic' do
26
+ count += 1
27
+ factory.new(count)
28
+ end
29
+
30
+ subject['dynamic'].must_equal value1
31
+ subject['dynamic'].must_equal value2
32
+ end
33
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/setup'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: garcon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rahoul Baruah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.1.0
69
+ description: ''
70
+ email:
71
+ - rahoulb@madeofstone.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - garcon.gemspec
82
+ - lib/garcon.rb
83
+ - lib/garcon/service_locator.rb
84
+ - lib/garcon/version.rb
85
+ - test/garcon/service_locator_test.rb
86
+ - test/test_helper.rb
87
+ homepage: http://github.com/rahoulb
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: A simple Service Locator class
111
+ test_files:
112
+ - test/garcon/service_locator_test.rb
113
+ - test/test_helper.rb