smithy 1.0

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.

Potentially problematic release.


This version of smithy might be problematic. Click here for more details.

data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p374@smithy
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smithy.gemspec
4
+ gemspec
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,58 @@
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"
30
+ require "bundler/setup"
31
+
32
+ require "logger"
33
+ require "smithy"
34
+
35
+ class LoggingErrorReporter
36
+ def initialize(logger)
37
+ @logger = logger
38
+ end
39
+
40
+ def report(error)
41
+ @logger.error("badness: #{error}")
42
+ end
43
+ end
44
+
45
+ container = Smithy::Container.new
46
+ container.register(:logger, Logger.new($stdout)) # you can register literal objects
47
+ container.register(:error_reporter, LoggingErrorReporter, :logger) # you can also register classes
48
+
49
+ container.instance(:error_reporter).report("no more coffee")
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
58
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ desc "Default: run specs."
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,42 @@
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
+ if dependency.any?
17
+ @definitions[name] = [component, dependency]
18
+ else
19
+ @definitions[name] = [component, infer_dependency(component)]
20
+ end
21
+ else
22
+ @instances[name] = component
23
+ end
24
+ end
25
+
26
+ def instance(name)
27
+ return @instances[name] if @instances[name]
28
+ component, dependency = @definitions[name]
29
+ raise(UnsatisfiedDependencyError, name) unless component
30
+ args = dependency.map {|service| self.instance(service) }
31
+ @instances[name] = component.new(*args)
32
+ end
33
+
34
+ private
35
+
36
+ def infer_dependency(component)
37
+ component.instance_method(:initialize).parameters.map do |_, name|
38
+ name
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Smithy
2
+ VERSION = "1.0"
3
+ end
data/lib/smithy.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "smithy/version"
2
+ require "smithy/container"
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,82 @@
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 inferred dependencies" do
19
+ let(:dependency) { Object.new }
20
+ let(:component_class) do
21
+ Class.new do
22
+ attr_reader :dependency
23
+
24
+ def initialize(dependency)
25
+ @dependency = dependency
26
+ end
27
+ end
28
+ end
29
+
30
+ before do
31
+ subject.register(:component, component_class)
32
+ subject.register(:dependency, dependency)
33
+ end
34
+
35
+ it "infers the dependency by parameter name" do
36
+ subject.instance(:component).dependency.should == dependency
37
+ end
38
+ end
39
+
40
+ context "name is registered to a class with explicit dependencies" do
41
+ let(:dependency) { Object.new }
42
+
43
+ let(:component_class) do
44
+ Class.new do
45
+ attr_reader :dependency
46
+
47
+ def initialize(dependency)
48
+ @dependency = dependency
49
+ end
50
+ end
51
+ end
52
+
53
+ before do
54
+ subject.register(:component, component_class, :explicit_dependency)
55
+ subject.register(:explicit_dependency, dependency)
56
+ end
57
+
58
+ it "injects the dependent object into the constructor" do
59
+ subject.instance(:component).dependency.should == dependency
60
+ end
61
+ end
62
+
63
+ context "name is registered to an object" do
64
+ let(:component) { Object.new }
65
+
66
+ before do
67
+ subject.register(:component, component)
68
+ end
69
+
70
+ it "returns the instance of the object" do
71
+ subject.instance(:component).should == component
72
+ end
73
+ end
74
+
75
+ context "name is not registered" do
76
+ it "raises" do
77
+ expect { subject.instance(:not_registered) }.to raise_error(UnsatisfiedDependencyError)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smithy
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris O'Meara
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-07 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