attr_inject 1.0.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.
- data/.document +5 -0
- data/.rspec +2 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +57 -0
- data/Guardfile +24 -0
- data/LICENSE.txt +20 -0
- data/README.md +103 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/attr_inject.gemspec +79 -0
- data/lib/attr_inject.rb +4 -0
- data/lib/attr_inject/core_ext.rb +17 -0
- data/lib/attr_inject/exceptions.rb +4 -0
- data/lib/attr_inject/injector.rb +23 -0
- data/lib/attr_inject/target.rb +71 -0
- data/spec/attr_inject/core_ext_spec.rb +42 -0
- data/spec/attr_inject/injector_spec.rb +51 -0
- data/spec/attr_inject/target_spec.rb +63 -0
- data/spec/spec_helper.rb +17 -0
- metadata +197 -0
data/.document
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rdoc", "~> 3.12"
|
10
|
+
gem "bundler"
|
11
|
+
gem "jeweler", "~> 1.8.4"
|
12
|
+
gem "rspec"
|
13
|
+
gem "guard"
|
14
|
+
gem "guard-rspec"
|
15
|
+
gem "rb-fsevent"
|
16
|
+
gem "growl"
|
17
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
coderay (1.0.9)
|
5
|
+
diff-lcs (1.1.3)
|
6
|
+
git (1.2.5)
|
7
|
+
growl (1.0.3)
|
8
|
+
guard (1.6.2)
|
9
|
+
listen (>= 0.6.0)
|
10
|
+
lumberjack (>= 1.0.2)
|
11
|
+
pry (>= 0.9.10)
|
12
|
+
terminal-table (>= 1.4.3)
|
13
|
+
thor (>= 0.14.6)
|
14
|
+
guard-rspec (2.5.1)
|
15
|
+
guard (>= 1.1)
|
16
|
+
rspec (~> 2.11)
|
17
|
+
jeweler (1.8.4)
|
18
|
+
bundler (~> 1.0)
|
19
|
+
git (>= 1.2.5)
|
20
|
+
rake
|
21
|
+
rdoc
|
22
|
+
json (1.7.7)
|
23
|
+
listen (0.7.3)
|
24
|
+
lumberjack (1.0.3)
|
25
|
+
method_source (0.8.1)
|
26
|
+
pry (0.9.12)
|
27
|
+
coderay (~> 1.0.5)
|
28
|
+
method_source (~> 0.8)
|
29
|
+
slop (~> 3.4)
|
30
|
+
rake (10.0.4)
|
31
|
+
rb-fsevent (0.9.2)
|
32
|
+
rdoc (3.12.2)
|
33
|
+
json (~> 1.4)
|
34
|
+
rspec (2.12.0)
|
35
|
+
rspec-core (~> 2.12.0)
|
36
|
+
rspec-expectations (~> 2.12.0)
|
37
|
+
rspec-mocks (~> 2.12.0)
|
38
|
+
rspec-core (2.12.2)
|
39
|
+
rspec-expectations (2.12.1)
|
40
|
+
diff-lcs (~> 1.1.3)
|
41
|
+
rspec-mocks (2.12.2)
|
42
|
+
slop (3.4.4)
|
43
|
+
terminal-table (1.4.5)
|
44
|
+
thor (0.17.0)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
bundler
|
51
|
+
growl
|
52
|
+
guard
|
53
|
+
guard-rspec
|
54
|
+
jeweler (~> 1.8.4)
|
55
|
+
rb-fsevent
|
56
|
+
rdoc (~> 3.12)
|
57
|
+
rspec
|
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec' do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
|
9
|
+
# Rails example
|
10
|
+
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
11
|
+
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
12
|
+
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
|
13
|
+
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
|
14
|
+
watch('config/routes.rb') { "spec/routing" }
|
15
|
+
watch('app/controllers/application_controller.rb') { "spec/controllers" }
|
16
|
+
|
17
|
+
# Capybara features specs
|
18
|
+
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
|
19
|
+
|
20
|
+
# Turnip features and steps
|
21
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
22
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
|
23
|
+
end
|
24
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Jeremy Saenz
|
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.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# attr\_inject
|
2
|
+
|
3
|
+
attr\_inject is an small and elegant dependency injection solution for Ruby.
|
4
|
+
|
5
|
+
#Installation
|
6
|
+
`gem install attr_inject`
|
7
|
+
|
8
|
+
# Usage
|
9
|
+
attr\_inject can be used many ways scaling from the least inrtusive to more inrusive options.
|
10
|
+
|
11
|
+
## Simple Example
|
12
|
+
Dependencies are injected via a Hash through the Object's constructor.
|
13
|
+
|
14
|
+
~~~ ruby
|
15
|
+
class Application
|
16
|
+
|
17
|
+
# initialize our dependencies
|
18
|
+
driver = Driver.new
|
19
|
+
passenger = Passenger.new
|
20
|
+
|
21
|
+
# inject our dependencies into our Car object
|
22
|
+
car = Car.new :driver => driver, :passenger => passenger
|
23
|
+
|
24
|
+
end
|
25
|
+
~~~
|
26
|
+
|
27
|
+
~~~ ruby
|
28
|
+
class Car
|
29
|
+
attr_inject :driver
|
30
|
+
attr_inject :passenger
|
31
|
+
|
32
|
+
def initialize(options)
|
33
|
+
inject_attributes options
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
~~~
|
38
|
+
|
39
|
+
## Injector Example
|
40
|
+
For more inversion of control, an Injector can be used.
|
41
|
+
|
42
|
+
~~~ ruby
|
43
|
+
class Application
|
44
|
+
include Inject
|
45
|
+
|
46
|
+
# Map our depedencies
|
47
|
+
injector = Injector.new
|
48
|
+
injector.map :driver, Driver.new
|
49
|
+
injector.map :passenger, Passenger.new
|
50
|
+
|
51
|
+
# Inject our dependencies into our car object
|
52
|
+
car = Car.new
|
53
|
+
injector.apply(car)
|
54
|
+
|
55
|
+
end
|
56
|
+
~~~
|
57
|
+
|
58
|
+
~~~ ruby
|
59
|
+
class Car
|
60
|
+
attr_inject :driver
|
61
|
+
attr_inject :passenger
|
62
|
+
end
|
63
|
+
~~~
|
64
|
+
|
65
|
+
## Factory Example
|
66
|
+
Create an Injector to map objects and factories to.
|
67
|
+
|
68
|
+
~~~ ruby
|
69
|
+
require "attr_inject"
|
70
|
+
|
71
|
+
class Application
|
72
|
+
include Inject
|
73
|
+
|
74
|
+
# Map our depedencies
|
75
|
+
injector = Injector.new
|
76
|
+
injector.map :driver, Driver.new
|
77
|
+
injector.map :passenger, Passenger.new
|
78
|
+
|
79
|
+
# Factory dependencies are called
|
80
|
+
# on each inject and are passed it's
|
81
|
+
# target object
|
82
|
+
injector.factory :logger do |target|
|
83
|
+
Logger.new(target)
|
84
|
+
end
|
85
|
+
|
86
|
+
# Inject our dependencies into our car object
|
87
|
+
car = Car.new
|
88
|
+
injector.apply(car)
|
89
|
+
|
90
|
+
end
|
91
|
+
~~~
|
92
|
+
|
93
|
+
Our car object explicitly defines what dependencies it wants.
|
94
|
+
|
95
|
+
~~~ ruby
|
96
|
+
class Car
|
97
|
+
|
98
|
+
attr_inject :driver
|
99
|
+
attr_inject :passenger
|
100
|
+
attr_inject :logger
|
101
|
+
|
102
|
+
end
|
103
|
+
~~~
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "attr_inject"
|
18
|
+
gem.homepage = "http://github.com/jeremysaenz/attr_inject"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Elegant dependency injection in Ruby.}
|
21
|
+
gem.description = %Q{attr_inject is a simple dependency injection solution for ruby.}
|
22
|
+
gem.email = "jeremy.saenz@gmail.com"
|
23
|
+
gem.authors = ["Jeremy Saenz"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
task :default => :test
|
29
|
+
|
30
|
+
require 'rdoc/task'
|
31
|
+
Rake::RDocTask.new do |rdoc|
|
32
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
33
|
+
|
34
|
+
rdoc.rdoc_dir = 'rdoc'
|
35
|
+
rdoc.title = "attr_inject #{version}"
|
36
|
+
rdoc.rdoc_files.include('README*')
|
37
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/attr_inject.gemspec
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
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 = "attr_inject"
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jeremy Saenz"]
|
12
|
+
s.date = "2013-04-10"
|
13
|
+
s.description = "attr_inject is a simple dependency injection solution for ruby."
|
14
|
+
s.email = "jeremy.saenz@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"Guardfile",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.md",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"attr_inject.gemspec",
|
30
|
+
"lib/attr_inject.rb",
|
31
|
+
"lib/attr_inject/core_ext.rb",
|
32
|
+
"lib/attr_inject/exceptions.rb",
|
33
|
+
"lib/attr_inject/injector.rb",
|
34
|
+
"lib/attr_inject/target.rb",
|
35
|
+
"spec/attr_inject/core_ext_spec.rb",
|
36
|
+
"spec/attr_inject/injector_spec.rb",
|
37
|
+
"spec/attr_inject/target_spec.rb",
|
38
|
+
"spec/spec_helper.rb"
|
39
|
+
]
|
40
|
+
s.homepage = "http://github.com/jeremysaenz/attr_inject"
|
41
|
+
s.licenses = ["MIT"]
|
42
|
+
s.require_paths = ["lib"]
|
43
|
+
s.rubygems_version = "1.8.24"
|
44
|
+
s.summary = "Elegant dependency injection in Ruby."
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
51
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
53
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
54
|
+
s.add_development_dependency(%q<guard>, [">= 0"])
|
55
|
+
s.add_development_dependency(%q<guard-rspec>, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<rb-fsevent>, [">= 0"])
|
57
|
+
s.add_development_dependency(%q<growl>, [">= 0"])
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
60
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
61
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
62
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
63
|
+
s.add_dependency(%q<guard>, [">= 0"])
|
64
|
+
s.add_dependency(%q<guard-rspec>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rb-fsevent>, [">= 0"])
|
66
|
+
s.add_dependency(%q<growl>, [">= 0"])
|
67
|
+
end
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
70
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
71
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
|
72
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
73
|
+
s.add_dependency(%q<guard>, [">= 0"])
|
74
|
+
s.add_dependency(%q<guard-rspec>, [">= 0"])
|
75
|
+
s.add_dependency(%q<rb-fsevent>, [">= 0"])
|
76
|
+
s.add_dependency(%q<growl>, [">= 0"])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
data/lib/attr_inject.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class Object
|
2
|
+
def inject_attributes(params)
|
3
|
+
self.class.__inject_targets.each do |target|
|
4
|
+
target.apply params, self
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Module
|
10
|
+
def attr_inject(attribute, options={})
|
11
|
+
__inject_targets << Inject::Target.new(self, attribute, options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def __inject_targets
|
15
|
+
@__inject_targets ||= []
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Inject
|
2
|
+
|
3
|
+
class Injector < Hash
|
4
|
+
|
5
|
+
attr_accessor :parent
|
6
|
+
|
7
|
+
def map(key, val)
|
8
|
+
self[key] = val
|
9
|
+
end
|
10
|
+
|
11
|
+
def factory(key, &block)
|
12
|
+
raise ArgumentError, "Block is required for factories." if block.nil?
|
13
|
+
self[key] = block
|
14
|
+
end
|
15
|
+
|
16
|
+
def apply(target)
|
17
|
+
hash = parent ? parent.merge(self) : self
|
18
|
+
target.inject_attributes hash
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Inject
|
2
|
+
class Target
|
3
|
+
|
4
|
+
def initialize(klass, attribute, opts={})
|
5
|
+
@klass = klass
|
6
|
+
@attribute = attribute
|
7
|
+
|
8
|
+
apply_options opts
|
9
|
+
add_accessor(attribute, klass)
|
10
|
+
end
|
11
|
+
|
12
|
+
def apply(params, target)
|
13
|
+
validate! params
|
14
|
+
|
15
|
+
set_value(target, default) if !default.nil?
|
16
|
+
set_value(target, attribute_value(params)) if params.include?(attribute)
|
17
|
+
end
|
18
|
+
|
19
|
+
def required?
|
20
|
+
options[:required] && default.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
def default
|
24
|
+
options[:default]
|
25
|
+
end
|
26
|
+
|
27
|
+
def attribute
|
28
|
+
@attribute
|
29
|
+
end
|
30
|
+
|
31
|
+
def klass
|
32
|
+
@klass
|
33
|
+
end
|
34
|
+
|
35
|
+
def options
|
36
|
+
@options
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
def add_accessor(attribute, klass)
|
41
|
+
accessor_method = Proc.new{
|
42
|
+
instance_variable_get "@#{attribute}"
|
43
|
+
}
|
44
|
+
|
45
|
+
klass.send(:define_method, attribute, &accessor_method)
|
46
|
+
end
|
47
|
+
|
48
|
+
def attribute_value(params)
|
49
|
+
val = params[attribute]
|
50
|
+
val = val.call(klass) if val.respond_to?(:call)
|
51
|
+
val
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_value(target, val)
|
55
|
+
target.instance_variable_set "@#{attribute}", val
|
56
|
+
end
|
57
|
+
|
58
|
+
def validate!(params)
|
59
|
+
raise_required_error! if !params.include?(attribute) && required?
|
60
|
+
end
|
61
|
+
|
62
|
+
def raise_required_error!
|
63
|
+
raise Inject::InjectionError, ":#{attribute} is required for dependency injection."
|
64
|
+
end
|
65
|
+
|
66
|
+
def apply_options(opts)
|
67
|
+
@options = { :required => true }.merge!(opts)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "attr_inject"
|
3
|
+
|
4
|
+
class SimpleInject
|
5
|
+
|
6
|
+
attr_inject :foo
|
7
|
+
attr_inject :bar
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
class InjectViaInitialize
|
12
|
+
|
13
|
+
attr_inject :bat
|
14
|
+
attr_inject :baz
|
15
|
+
attr_inject :blam, :required => false
|
16
|
+
|
17
|
+
def initialize(params)
|
18
|
+
inject_attributes params
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "attr_inject" do
|
24
|
+
it "creates a method with attr_inject" do
|
25
|
+
i = SimpleInject.new
|
26
|
+
i.foo.should == nil
|
27
|
+
i.bar.should == nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "injects into new attributes with inject_attributes" do
|
31
|
+
a_string = "hello"
|
32
|
+
an_array = ["hello", "world"]
|
33
|
+
i = InjectViaInitialize.new :bat => a_string, :baz => an_array
|
34
|
+
|
35
|
+
i.bat.should == a_string
|
36
|
+
i.baz.should == an_array
|
37
|
+
end
|
38
|
+
|
39
|
+
it "raises an error when an required attribute is unfulfiled" do
|
40
|
+
expect{InjectViaInitialize.new :foo => "bar"}.to raise_error(Inject::InjectionError)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "attr_inject/injector"
|
3
|
+
|
4
|
+
describe Inject::Injector do
|
5
|
+
let(:injector) {subject}
|
6
|
+
let(:test_stub) {mock(:test_stub)}
|
7
|
+
|
8
|
+
it "maps options with the map method" do
|
9
|
+
injector.map(:foo, "foo_string")
|
10
|
+
injector.map(:bar, 100)
|
11
|
+
injector[:bat] = "bat"
|
12
|
+
|
13
|
+
injector[:foo].should == "foo_string"
|
14
|
+
injector[:bar].should == 100
|
15
|
+
injector[:bat].should == "bat"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "maps factories via blocks" do
|
19
|
+
injector.factory :foo do |obj|
|
20
|
+
"Hello #{obj.to_s}"
|
21
|
+
end
|
22
|
+
|
23
|
+
injector[:foo].call(:jeremy).should == "Hello jeremy"
|
24
|
+
injector[:foo].call("jordie").should == "Hello jordie"
|
25
|
+
injector[:foo].respond_to?(:call).should == true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "raises an exception when a block is not passed" do
|
29
|
+
expect { injector.factory :foo }.to raise_error(ArgumentError)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "applies its mapped values on an object" do
|
33
|
+
test_stub.should_receive(:inject_attributes).with(injector)
|
34
|
+
injector.apply test_stub
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allows the use of a parent injector" do
|
38
|
+
result = {:foo => "foo_string", :bar => "bar_string", :bat => "bat_string"}
|
39
|
+
test_stub.should_receive(:inject_attributes).with(result)
|
40
|
+
|
41
|
+
injector.map(:foo, "foo_string")
|
42
|
+
injector.map(:bar, "bar_string")
|
43
|
+
|
44
|
+
parent = Inject::Injector.new
|
45
|
+
injector.parent = parent
|
46
|
+
parent.map(:bat, "bat_string")
|
47
|
+
parent.map(:foo, "not_foo")
|
48
|
+
|
49
|
+
injector.apply(test_stub)
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "attr_inject"
|
3
|
+
|
4
|
+
describe Inject::Target do
|
5
|
+
it "it adds an accessor method on the specified class" do
|
6
|
+
c = Object.new
|
7
|
+
t = Inject::Target.new(c.class, :foo)
|
8
|
+
c.should respond_to :foo
|
9
|
+
|
10
|
+
c2 = c.class.new
|
11
|
+
c2.should respond_to :foo
|
12
|
+
end
|
13
|
+
|
14
|
+
it "applies options on intialize" do
|
15
|
+
Inject::Target.any_instance.should_receive(:apply_options)
|
16
|
+
c = Object.new
|
17
|
+
Inject::Target.new c.class, :foo, :required => false
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is required by default" do
|
21
|
+
c = Object.new
|
22
|
+
t = Inject::Target.new c.class, :foo
|
23
|
+
t.required?.should == true
|
24
|
+
end
|
25
|
+
|
26
|
+
it "is can optionally not be required" do
|
27
|
+
c = Object.new
|
28
|
+
t = Inject::Target.new c.class, :foo, :required => false
|
29
|
+
t.required?.should == false
|
30
|
+
end
|
31
|
+
|
32
|
+
it "looks up the attribute with find_attribute" do
|
33
|
+
c = Object.new
|
34
|
+
t = Inject::Target.new c.class, :foo
|
35
|
+
|
36
|
+
params = {:foo => "foobar"}
|
37
|
+
t.should_receive(:attribute_value).with(params)
|
38
|
+
t.apply params, c
|
39
|
+
end
|
40
|
+
|
41
|
+
it "calls a factory proc if the attribute value is a block" do
|
42
|
+
c = Object.new
|
43
|
+
t = Inject::Target.new c.class, :foo
|
44
|
+
|
45
|
+
params = {:foo => Proc.new{ |obj| "hello #{obj.to_s}"}}
|
46
|
+
t.apply params, c
|
47
|
+
|
48
|
+
c.foo.should == "hello Object"
|
49
|
+
|
50
|
+
c2 = c.class.new
|
51
|
+
c2.foo.should == nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it "can have default values" do
|
55
|
+
c = Object.new
|
56
|
+
t = Inject::Target.new c.class, :foo, :default => "hello world"
|
57
|
+
|
58
|
+
params = {}
|
59
|
+
t.apply params, c
|
60
|
+
|
61
|
+
c.foo.should == "hello world"
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_inject
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeremy Saenz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rdoc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.12'
|
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: '3.12'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.8.4
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.8.4
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: guard
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: guard-rspec
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: rb-fsevent
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: growl
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
description: attr_inject is a simple dependency injection solution for ruby.
|
143
|
+
email: jeremy.saenz@gmail.com
|
144
|
+
executables: []
|
145
|
+
extensions: []
|
146
|
+
extra_rdoc_files:
|
147
|
+
- LICENSE.txt
|
148
|
+
- README.md
|
149
|
+
files:
|
150
|
+
- .document
|
151
|
+
- .rspec
|
152
|
+
- Gemfile
|
153
|
+
- Gemfile.lock
|
154
|
+
- Guardfile
|
155
|
+
- LICENSE.txt
|
156
|
+
- README.md
|
157
|
+
- Rakefile
|
158
|
+
- VERSION
|
159
|
+
- attr_inject.gemspec
|
160
|
+
- lib/attr_inject.rb
|
161
|
+
- lib/attr_inject/core_ext.rb
|
162
|
+
- lib/attr_inject/exceptions.rb
|
163
|
+
- lib/attr_inject/injector.rb
|
164
|
+
- lib/attr_inject/target.rb
|
165
|
+
- spec/attr_inject/core_ext_spec.rb
|
166
|
+
- spec/attr_inject/injector_spec.rb
|
167
|
+
- spec/attr_inject/target_spec.rb
|
168
|
+
- spec/spec_helper.rb
|
169
|
+
homepage: http://github.com/jeremysaenz/attr_inject
|
170
|
+
licenses:
|
171
|
+
- MIT
|
172
|
+
post_install_message:
|
173
|
+
rdoc_options: []
|
174
|
+
require_paths:
|
175
|
+
- lib
|
176
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
177
|
+
none: false
|
178
|
+
requirements:
|
179
|
+
- - ! '>='
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
segments:
|
183
|
+
- 0
|
184
|
+
hash: 677309491037115396
|
185
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
186
|
+
none: false
|
187
|
+
requirements:
|
188
|
+
- - ! '>='
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
requirements: []
|
192
|
+
rubyforge_project:
|
193
|
+
rubygems_version: 1.8.24
|
194
|
+
signing_key:
|
195
|
+
specification_version: 3
|
196
|
+
summary: Elegant dependency injection in Ruby.
|
197
|
+
test_files: []
|