rinject 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.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +40 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/lib/rinject/container.rb +53 -0
- data/lib/rinject/initializer_error.rb +10 -0
- data/lib/rinject/service_exists_error.rb +10 -0
- data/lib/rinject/unknown_service_error.rb +10 -0
- data/lib/rinject.rb +11 -0
- data/rinject.gemspec +57 -0
- data/test/rinject_container_test.rb +77 -0
- data/test/test_helper.rb +10 -0
- metadata +79 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Stefan Nuxoll
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
= rinject
|
2
|
+
|
3
|
+
RInject is a simple dependency injection library for ruby.
|
4
|
+
|
5
|
+
== Sample usage
|
6
|
+
require 'rinject'
|
7
|
+
|
8
|
+
class MyCoolApplication
|
9
|
+
def initialize
|
10
|
+
@container = RInject::Container.new
|
11
|
+
@container.register(:logfile) { 'mycoolapp.log' }
|
12
|
+
@container.register(:db_user) { 'snuxoll' }
|
13
|
+
@container.register(:db_password) { 'dragonslayer' }
|
14
|
+
@container.register(:database) do |c|
|
15
|
+
Database.connect(c.db_user, c.db_password)
|
16
|
+
end
|
17
|
+
@container.register(:error_handler) do |c|
|
18
|
+
handler = ErrorHandler.new
|
19
|
+
handler.logger = c.logger
|
20
|
+
end
|
21
|
+
@container.register(:logger) do |c|
|
22
|
+
Logger.new(c.logfile)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
== Note on Patches/Pull Requests
|
28
|
+
|
29
|
+
* Fork the project.
|
30
|
+
* Make your feature addition or bug fix.
|
31
|
+
* Add tests for it. This is important so I don't break it in a
|
32
|
+
future version unintentionally.
|
33
|
+
* Commit, do not mess with rakefile, version, or history.
|
34
|
+
(if you want to have your own version, that is fine but
|
35
|
+
bump version in a commit by itself I can ignore when I pull)
|
36
|
+
* Send me a pull request. Bonus points for topic branches.
|
37
|
+
|
38
|
+
== Copyright
|
39
|
+
|
40
|
+
Copyright (c) 2009 Stefan Nuxoll. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rinject"
|
8
|
+
gem.summary = %Q{Dependency injection for ruby}
|
9
|
+
gem.description = %Q{Simple dependency injection framework for ruby applications}
|
10
|
+
gem.email = "stefan@nuxoll.eu.org"
|
11
|
+
gem.homepage = "http://github.com/snuxoll/rinject"
|
12
|
+
gem.authors = ["Stefan Nuxoll"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
if File.exist?('VERSION')
|
47
|
+
version = File.read('VERSION')
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "rinject #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# container.rb
|
2
|
+
# Author:: Stefan Nuxoll
|
3
|
+
# Copyright:: (C) 2009 Stefan Nuxoll
|
4
|
+
# License:: MIT License
|
5
|
+
|
6
|
+
module RInject
|
7
|
+
class Container
|
8
|
+
|
9
|
+
# Creates a new RInject container
|
10
|
+
def initialize
|
11
|
+
# Contains the initialization procs for services
|
12
|
+
@initializers = {}
|
13
|
+
# Cached objects returned from initializers
|
14
|
+
@cached_objects = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
# Registers a new service with the container, providing a block to
|
18
|
+
# initialize the service
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# container = RInject::Container.new
|
22
|
+
# container.register(:logger) do
|
23
|
+
# logger = MyCoolLogger.new
|
24
|
+
# logger.set_level(:error)
|
25
|
+
# logger
|
26
|
+
# end
|
27
|
+
def register(service, &initializer)
|
28
|
+
if not initializer
|
29
|
+
raise RInject::InitializerError
|
30
|
+
elsif not @initializers.include?(service)
|
31
|
+
@initializers[service] = initializer
|
32
|
+
else
|
33
|
+
raise RInject::ServiceExistsError
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Dirty hack for the `container.service` convention
|
38
|
+
def method_missing(symbol)
|
39
|
+
if object = @cached_objects[symbol]
|
40
|
+
object
|
41
|
+
else
|
42
|
+
# See if we have an initializer for the service
|
43
|
+
if initializer = @initializers[symbol]
|
44
|
+
@cached_objects[symbol] = initializer.call(self)
|
45
|
+
else
|
46
|
+
# Don't have an initializer for this service, return an error
|
47
|
+
raise RInject::UnknownServiceError
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/lib/rinject.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# rinject.rb
|
2
|
+
# Author:: Stefan Nuxoll
|
3
|
+
# Copyright:: (C) 2009 Stefan Nuxoll
|
4
|
+
# License:: MIT License
|
5
|
+
|
6
|
+
module RInject
|
7
|
+
require 'rinject/service_exists_error'
|
8
|
+
require 'rinject/unknown_service_error'
|
9
|
+
require 'rinject/initializer_error'
|
10
|
+
require 'rinject/container'
|
11
|
+
end
|
data/rinject.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rinject}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Stefan Nuxoll"]
|
12
|
+
s.date = %q{2009-10-06}
|
13
|
+
s.description = %q{Simple dependency injection framework for ruby applications}
|
14
|
+
s.email = %q{stefan@nuxoll.eu.org}
|
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/rinject.rb",
|
27
|
+
"lib/rinject/container.rb",
|
28
|
+
"lib/rinject/initializer_error.rb",
|
29
|
+
"lib/rinject/service_exists_error.rb",
|
30
|
+
"lib/rinject/unknown_service_error.rb",
|
31
|
+
"rinject.gemspec",
|
32
|
+
"test/rinject_container_test.rb",
|
33
|
+
"test/test_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/snuxoll/rinject}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.5}
|
39
|
+
s.summary = %q{Dependency injection for ruby}
|
40
|
+
s.test_files = [
|
41
|
+
"test/rinject_container_test.rb",
|
42
|
+
"test/test_helper.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
end
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'rinject'
|
4
|
+
|
5
|
+
class RInjectContainerTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
should "Raise error on duplicate registrations" do
|
8
|
+
|
9
|
+
# Create our new container
|
10
|
+
container = RInject::Container.new
|
11
|
+
# If we don't change this value than obviously we never caught the exception
|
12
|
+
caught_exception = false
|
13
|
+
container.register(:test) { "Hello, World" }
|
14
|
+
begin
|
15
|
+
container.register(:test) { "Goodbye, World" }
|
16
|
+
rescue RInject::ServiceExistsError => e
|
17
|
+
caught_exception = true
|
18
|
+
end
|
19
|
+
assert caught_exception, "Exception not thrown"
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
context "Service registration" do
|
24
|
+
|
25
|
+
setup do
|
26
|
+
@container = RInject::Container.new
|
27
|
+
end
|
28
|
+
|
29
|
+
should "not register services without an initializer" do
|
30
|
+
caught_exception=false
|
31
|
+
begin
|
32
|
+
@container.register(:test)
|
33
|
+
rescue RInject::InitializerError => e
|
34
|
+
caught_exception = true
|
35
|
+
end
|
36
|
+
assert(caught_exception, "Exception not caught")
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "Initialized container" do
|
42
|
+
|
43
|
+
setup do
|
44
|
+
@container = RInject::Container.new
|
45
|
+
# Register a simple test service
|
46
|
+
@container.register(:hello) do
|
47
|
+
"Hello, World"
|
48
|
+
end
|
49
|
+
# Cached object test
|
50
|
+
@container.register(:cache) do
|
51
|
+
Time.now
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
should "respond to a service lookup" do
|
56
|
+
hello_string = @container.hello
|
57
|
+
assert_equal(hello_string, "Hello, World")
|
58
|
+
end
|
59
|
+
|
60
|
+
should "cache service objects" do
|
61
|
+
time = @container.cache
|
62
|
+
assert_equal(time, @container.cache)
|
63
|
+
end
|
64
|
+
|
65
|
+
should "raise error on unkown service" do
|
66
|
+
exception_raised = false
|
67
|
+
begin
|
68
|
+
@container.service_that_has_not_been_registered
|
69
|
+
rescue RInject::UnknownServiceError => e
|
70
|
+
exception_raised = true
|
71
|
+
end
|
72
|
+
assert(exception_raised, "exception not raised")
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rinject
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefan Nuxoll
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-06 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Simple dependency injection framework for ruby applications
|
26
|
+
email: stefan@nuxoll.eu.org
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/rinject.rb
|
42
|
+
- lib/rinject/container.rb
|
43
|
+
- lib/rinject/initializer_error.rb
|
44
|
+
- lib/rinject/service_exists_error.rb
|
45
|
+
- lib/rinject/unknown_service_error.rb
|
46
|
+
- rinject.gemspec
|
47
|
+
- test/rinject_container_test.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/snuxoll/rinject
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --charset=UTF-8
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Dependency injection for ruby
|
77
|
+
test_files:
|
78
|
+
- test/rinject_container_test.rb
|
79
|
+
- test/test_helper.rb
|