dependency_injector 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/Rakefile +14 -0
- data/dependency_injector.gemspec +17 -0
- data/lib/dependency_injector.rb +20 -0
- data/lib/dependency_injector/version.rb +3 -0
- data/test/dependency_injector_test.rb +36 -0
- data/test/test_helper.rb +3 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jeremy Friesen
|
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,48 @@
|
|
1
|
+
# DependencyInjector
|
2
|
+
|
3
|
+
A simple macro, akin to def_delegator for plain and simple dependency
|
4
|
+
injection.
|
5
|
+
|
6
|
+
I'm of the opinion that if you are going to have a convention for dependency
|
7
|
+
injection, then why not make a macro method. Then, it's easy to components
|
8
|
+
involved in the dependency injection straightforward.
|
9
|
+
|
10
|
+
This is plain simple dependency injection and frankly, if you don't like it
|
11
|
+
at least you have a macro to search on instead of scattered :attr_writer
|
12
|
+
declarations with their corresponding private :attr_reader methods.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'dependency_injector'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install dependency_injector
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
class Blog
|
31
|
+
extend DependencyInjector
|
32
|
+
def_injector(:request_locator) { Location.public_method(:locate) }
|
33
|
+
end
|
34
|
+
|
35
|
+
$ Blog.new.respond_to?(:request_locator)
|
36
|
+
=> true
|
37
|
+
$ Blog.new.respond_to?(:request_locator)
|
38
|
+
=> NoMethodError: private method `request_locator' called for #<Blog:0x00000103408c20>
|
39
|
+
$ Blog.new.request_locator = -> { MiniTest::Mock.new }
|
40
|
+
=> #<Proc:0x00000101b0bba0@(irb):3 (lambda)>
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
|
6
|
+
desc "Run tests"
|
7
|
+
Rake::TestTask.new('test') do |t|
|
8
|
+
t.libs.push "lib"
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
t.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
task :default => :test
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/dependency_injector/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jeremy Friesen"]
|
6
|
+
gem.email = ["jeremy.n.friesen@gmail.com"]
|
7
|
+
gem.description = %q{A thus far stupidly simple dependency injector}
|
8
|
+
gem.summary = %q{Simple dependency injector}
|
9
|
+
gem.homepage = ""
|
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 = "dependency_injector"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = DependencyInjector::VERSION
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "dependency_injector/version"
|
2
|
+
|
3
|
+
module DependencyInjector
|
4
|
+
# A simple macro for creating an easy dependency injector.
|
5
|
+
def def_injector(method_name, &default)
|
6
|
+
attr_writer method_name
|
7
|
+
|
8
|
+
define_method(method_name) do
|
9
|
+
if returning_value = instance_variable_get("@#{method_name}")
|
10
|
+
returning_value
|
11
|
+
else
|
12
|
+
|
13
|
+
returning_value = instance_eval(&default)
|
14
|
+
instance_variable_set("@#{method_name}", returning_value)
|
15
|
+
returning_value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
private method_name
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'dependency_injector'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class DependencyInjectorTest < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@klass = Class.new {
|
9
|
+
extend DependencyInjector
|
10
|
+
def_injector(:foo) { :default_foo }
|
11
|
+
|
12
|
+
def get_foo; foo; end
|
13
|
+
}
|
14
|
+
@object = @klass.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_creates_public_writer
|
18
|
+
@object.foo = :baz
|
19
|
+
assert_equal :baz, @object.get_foo
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_creates_private_method
|
23
|
+
assert_raises NoMethodError, "private method `foo' called" do
|
24
|
+
@object.foo
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_default_injected_value
|
29
|
+
assert_equal :default_foo, @object.get_foo
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_overridden_injected_value
|
33
|
+
@object.foo = :override
|
34
|
+
assert_equal :override, @object.get_foo
|
35
|
+
end
|
36
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dependency_injector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Friesen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A thus far stupidly simple dependency injector
|
15
|
+
email:
|
16
|
+
- jeremy.n.friesen@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- dependency_injector.gemspec
|
27
|
+
- lib/dependency_injector.rb
|
28
|
+
- lib/dependency_injector/version.rb
|
29
|
+
- test/dependency_injector_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
homepage: ''
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.17
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Simple dependency injector
|
55
|
+
test_files:
|
56
|
+
- test/dependency_injector_test.rb
|
57
|
+
- test/test_helper.rb
|