smithy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +57 -0
- data/Rakefile +9 -0
- data/lib/smithy.rb +2 -0
- data/lib/smithy/container.rb +30 -0
- data/lib/smithy/version.rb +3 -0
- data/smithy.gemspec +18 -0
- data/spec/smithy/container_spec.rb +60 -0
- metadata +74 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.3-p194@smithy
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 StreamSend
|
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,57 @@
|
|
1
|
+
# Smithy
|
2
|
+
|
3
|
+
Smith is an Inversion of Control (IoC) container for Ruby. It's based on an
|
4
|
+
example given in Jim Weirich's
|
5
|
+
[Dependency Injection: Vitally Important or Totally Irrelevant][ditalk]
|
6
|
+
talk at O'REILLY OSCON 2005. He called the example
|
7
|
+
[matzdi\_constructor][difile]
|
8
|
+
so, presumably, Matz was involved as well.
|
9
|
+
|
10
|
+
[ditalk]:http://onestepback.org/articles/depinj/
|
11
|
+
[difile]:http://onestepback.org/articles/depinj/matz/matzdi_constructor_rb.html
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'smithy'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install smithy
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
require "rubygems/setup"
|
30
|
+
|
31
|
+
require "logger"
|
32
|
+
require "smithy"
|
33
|
+
|
34
|
+
class LoggingErrorReporter
|
35
|
+
def initialize(logger)
|
36
|
+
@logger = logger
|
37
|
+
end
|
38
|
+
|
39
|
+
def report(error)
|
40
|
+
@logger.error("badness: #{error}")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
container = Smithy::Container.new
|
45
|
+
container.register(:logger, Logger.new($stdout)) # you can register literal objects
|
46
|
+
container.register(:error_reporter, LoggingErrorReporter, :logger) # you can also register classes
|
47
|
+
|
48
|
+
container.instance(:error_reporter).report("no more coffee")
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
57
|
+
|
data/Rakefile
ADDED
data/lib/smithy.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module Smithy
|
2
|
+
class UnsatisfiedDependencyError < StandardError
|
3
|
+
def initialize(dependency_name)
|
4
|
+
super "Unmet dependency named #{dependency_name}"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class Container
|
9
|
+
def initialize
|
10
|
+
@definitions = {}
|
11
|
+
@instances = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def register(name, component, *dependency)
|
15
|
+
if component.respond_to?(:new)
|
16
|
+
@definitions[name] = [component, dependency]
|
17
|
+
else
|
18
|
+
@instances[name] = component
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def instance(name)
|
23
|
+
return @instances[name] if @instances[name]
|
24
|
+
component, dependency = @definitions[name]
|
25
|
+
raise(UnsatisfiedDependencyError, name) unless component
|
26
|
+
args = dependency.map {|service| self.instance(service) }
|
27
|
+
@instances[name] = component.new(*args)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/smithy.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/smithy/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Chris O'Meara"]
|
6
|
+
gem.email = ["comeara@streamsend.com"]
|
7
|
+
gem.description = %q{A simple Dependency Injection container for Ruby.}
|
8
|
+
gem.summary = %q{Smithy implements the Dependency Injection pattern using a constructor injection strategy.}
|
9
|
+
gem.homepage = "https://github.com/streamsend/smithy"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "smithy"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Smithy::VERSION
|
17
|
+
gem.add_development_dependency "rspec", "2.10.0"
|
18
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "smithy/container"
|
2
|
+
|
3
|
+
module Smithy
|
4
|
+
describe Container do
|
5
|
+
describe "#instance" do
|
6
|
+
context "name is registered to a class without dependencies" do
|
7
|
+
let(:component_class) { Class.new }
|
8
|
+
|
9
|
+
before do
|
10
|
+
subject.register(:component, component_class)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns a new instance of the class" do
|
14
|
+
subject.instance(:component).should be_kind_of(component_class)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "name is registered to a class with dependencies" do
|
19
|
+
let(:dependency) { Object.new }
|
20
|
+
|
21
|
+
let(:component_class) do
|
22
|
+
Class.new do
|
23
|
+
attr_reader :dependency
|
24
|
+
|
25
|
+
def initialize(dependency)
|
26
|
+
@dependency = dependency
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
before do
|
32
|
+
subject.register(:dependency, dependency)
|
33
|
+
subject.register(:component, component_class, :dependency)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "injects the dependent object into the constructor" do
|
37
|
+
subject.instance(:component).dependency.should == dependency
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "name is registered to an object" do
|
42
|
+
let(:component) { Object.new }
|
43
|
+
|
44
|
+
before do
|
45
|
+
subject.register(:component, component)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns the instance of the object" do
|
49
|
+
subject.instance(:component).should == component
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "name is not registered" do
|
54
|
+
it "raises" do
|
55
|
+
expect { subject.instance(:not_registered) }.to raise_error(UnsatisfiedDependencyError)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smithy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris O'Meara
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.10.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.10.0
|
30
|
+
description: A simple Dependency Injection container for Ruby.
|
31
|
+
email:
|
32
|
+
- comeara@streamsend.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- .rvmrc
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/smithy.rb
|
44
|
+
- lib/smithy/container.rb
|
45
|
+
- lib/smithy/version.rb
|
46
|
+
- smithy.gemspec
|
47
|
+
- spec/smithy/container_spec.rb
|
48
|
+
homepage: https://github.com/streamsend/smithy
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Smithy implements the Dependency Injection pattern using a constructor injection
|
72
|
+
strategy.
|
73
|
+
test_files:
|
74
|
+
- spec/smithy/container_spec.rb
|