syringe 0.0.1.pre

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Leandro Silva (CodeZone)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ = syringe
2
+
3
+ A very lightweight dependency injection container for Ruby.
4
+
5
+ It born from a real need in one of my current projects (at Locaweb). I'm only scratching our itch. If you have the same itch, join us!
6
+
7
+ ==== Special Thanks
8
+
9
+ Hey Jim Weirich, thanks for initial code and inspiration. You are the man!
10
+
11
+ http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc
12
+
13
+ == Install
14
+
15
+ gem install syringe --pre -s http://gemcutter.org
16
+
17
+ == Examples
18
+
19
+ ==== First example with taste of service locator pattern
20
+
21
+ # on application bootstrap
22
+ container = Syringe::Container.new
23
+ container.register(:service_uri) { |container| 'http://services.syringe.org/api' }
24
+ container.register(:service_consumer) { |container| ServiceConsumer.new(container[:service_uri]) }
25
+
26
+ ...
27
+
28
+ # anywhere in the code
29
+ puts container[:service_uri] # http://services.syringe.org/api
30
+ puts container.service_uri # http://services.syringe.org/api
31
+
32
+ ==== Second example with the best taste of dependency injection
33
+
34
+ # on application bootstrap
35
+ default_container = Syringe::Container.default
36
+ default_container.register(:service_uri) { |container| 'http://services.syringe.org/api' }
37
+
38
+ ...
39
+
40
+ # in some class
41
+ class ServiceConsumer
42
+ inject :service_uri # it will create a new method and instance variable with that name
43
+ end
44
+
45
+ ...
46
+
47
+ # anywhere in the code
48
+ service_consumer = ServiceConsumer.new
49
+ puts service_consumer.service_uri # 'http://services.syringe.org/api'
50
+
51
+ == See more on
52
+
53
+ http://github.com/leandrosilva/syringe/tree/master/spec
54
+
55
+ == Copyright
56
+
57
+ Copyright (c) 2010 Leandro Silva (CodeZone) <leandrodoze@gmail.com>. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "syringe"
8
+ gem.summary = %Q{A very lightweight dependency injection container for Ruby}
9
+ gem.description = %Q{If you needs a dependency injection container for Ruby, try use syringe.}
10
+ gem.email = "leandrodoze@gmail.com"
11
+ gem.homepage = "http://github.com/leandrosilva/syringe"
12
+ gem.authors = ["Leandro Silva"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "syringe #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1.pre
@@ -0,0 +1,2 @@
1
+ require 'syringe/syringe'
2
+ require 'syringe/extension'
@@ -0,0 +1,13 @@
1
+ #
2
+ # Extension for Object to provide dependency injection capability.
3
+ #
4
+ class Object
5
+ # Define a new method and instance variable named as +object_name+ parameter.
6
+ def self.inject(object_name)
7
+ instance_eval do
8
+ send(:define_method, object_name) do
9
+ instance_variable_set("@#{object_name}", Syringe::Container.default[object_name])
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,71 @@
1
+ #
2
+ # Syringe Dependency Injection Container
3
+ #
4
+ # This code is strongly based (almost entirely a copy) on Jim Weirich's sample code at:
5
+ # http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc
6
+ #
7
+ # One more time: thanks Jim!
8
+ #
9
+ module Syringe
10
+ #
11
+ # Thrown when a service cannot be located by name.
12
+ #
13
+ class MissingServiceError < StandardError; end
14
+
15
+ #
16
+ # Thrown when a duplicate service is registered.
17
+ #
18
+ class DuplicateServiceError < StandardError; end
19
+
20
+ #
21
+ # Syringe::Container is the central data store for registering services used for dependency injuction.
22
+ # Users register services by providing a name and a block used to create the service. Services may be
23
+ # retrieved by asking for them by name (via the [] operator) or by selector (via the method_missing
24
+ # technique).
25
+ #
26
+ class Container
27
+ @@default_container = nil
28
+
29
+ #
30
+ # Get default container. It's fundamentaly useful for "inject" class method added to Object class.
31
+ #
32
+ def self.default
33
+ @@default_container ||= Container.new
34
+ end
35
+
36
+ # Create a dependency injection container.
37
+ def initialize
38
+ @services = {}
39
+ @cache = {}
40
+ end
41
+
42
+ # Register a service named +name+. The +block+ will be used to create the service on demand. It is
43
+ # recommended that symbols be used as the name of a service.
44
+ def register(name, &block)
45
+ raise DuplicateServiceError, "Duplicate Service Name '#{name}'" if @services.include? name
46
+
47
+ @services[name] = block
48
+ end
49
+
50
+ # Lookup a service by name. Throw an exception if no service is found.
51
+ def [](name)
52
+ @cache[name] ||= service_block(name).call(self)
53
+ end
54
+
55
+ # Lookup a service by message selector. A service with the same name as +sym+ will be returned, or
56
+ # an exception is thrown if no matching service is found.
57
+ def method_missing(sym, *args, &block)
58
+ self[sym]
59
+ end
60
+
61
+ private
62
+
63
+ # Return the block that creates the named service. Throw an exception if no service creation block of
64
+ # the given name can be found in the container or its parents.
65
+ def service_block(name)
66
+ raise MissingServiceError, "Unknown Service '#{name}'" unless @services.include? name
67
+
68
+ @services[name]
69
+ end
70
+ end
71
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'syringe'
5
+ require 'spec'
6
+
7
+ module SyringeHelperClasses
8
+ class ServiceConsumer
9
+ inject :service_uri
10
+ end
11
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Syringe::Container do
4
+ context 'when instantiated' do
5
+ subject do
6
+ Syringe::Container.new
7
+ end
8
+
9
+ it 'should allow register and retrieve objects that can to be injected' do
10
+ subject.register(:some_string) { |container| 'Some string' }
11
+ subject[:some_string].should == 'Some string'
12
+ subject.some_string.should == 'Some string'
13
+
14
+ subject.register(:some_number) { |container| 1212 }
15
+ subject[:some_number].should == 1212
16
+ subject.some_number.should == 1212
17
+ end
18
+
19
+ it 'should raise error if a object already is registered' do
20
+ subject.register(:some_string) { |container| 'Some string' }
21
+ subject[:some_string].should == 'Some string'
22
+
23
+ lambda {
24
+ subject.register(:some_string) { |container| 'Some string' }
25
+ }.should raise_error Syringe::DuplicateServiceError
26
+ end
27
+
28
+ it 'should raise error if a object was not registered' do
29
+ lambda {
30
+ subject[:some_string]
31
+ }.should raise_error Syringe::MissingServiceError
32
+ end
33
+ end
34
+
35
+ context 'when used as default container instance' do
36
+ context 'and configured with registered objects' do
37
+ it 'should be able to do dependency injection on classes' do
38
+ default_container = Syringe::Container.default
39
+ default_container.register(:service_uri) { |container| 'http://services.syringe.org/api' }
40
+
41
+ service_consumer = SyringeHelperClasses::ServiceConsumer.new
42
+
43
+ service_consumer.respond_to?(:service_uri).should == true
44
+ service_consumer.service_uri.should == 'http://services.syringe.org/api'
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{syringe}
8
+ s.version = "0.0.1.pre"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Leandro Silva"]
12
+ s.date = %q{2010-03-16}
13
+ s.description = %q{If you needs a dependency injection container for Ruby, try use syringe.}
14
+ s.email = %q{leandrodoze@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/syringe.rb",
27
+ "lib/syringe/extension.rb",
28
+ "lib/syringe/syringe.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb",
31
+ "spec/syringe_spec.rb",
32
+ "syringe.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/leandrosilva/syringe}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.6}
38
+ s.summary = %q{A very lightweight dependency injection container for Ruby}
39
+ s.test_files = [
40
+ "spec/spec_helper.rb",
41
+ "spec/syringe_spec.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ end
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syringe
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ - pre
10
+ version: 0.0.1.pre
11
+ platform: ruby
12
+ authors:
13
+ - Leandro Silva
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-16 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: If you needs a dependency injection container for Ruby, try use syringe.
36
+ email: leandrodoze@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/syringe.rb
52
+ - lib/syringe/extension.rb
53
+ - lib/syringe/syringe.rb
54
+ - spec/spec.opts
55
+ - spec/spec_helper.rb
56
+ - spec/syringe_spec.rb
57
+ - syringe.gemspec
58
+ has_rdoc: true
59
+ homepage: http://github.com/leandrosilva/syringe
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">"
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 1
80
+ - 3
81
+ - 1
82
+ version: 1.3.1
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.6
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: A very lightweight dependency injection container for Ruby
90
+ test_files:
91
+ - spec/spec_helper.rb
92
+ - spec/syringe_spec.rb