pinball 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +3 -0
- data/README.md +12 -1
- data/lib/pinball.rb +6 -0
- data/lib/pinball/class.rb +2 -5
- data/lib/pinball/container.rb +16 -3
- data/lib/pinball/version.rb +1 -1
- data/pinball.gemspec +3 -3
- data/spec/lib/pinball/class_spec.rb +15 -0
- data/spec/lib/pinball/container_spec.rb +34 -4
- metadata +24 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47516b049661968362374418a15718a832ee2748
|
4
|
+
data.tar.gz: 935d17aab842e964e98d32e993edffb1c1fe57c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26597ab29677f387763717c6b6aaf780ce4c3fd4af30680d463ee4eca89247da56bce48772f6d70480f504e4caac0570b7de775b4e56418958e85254dddcf3cc
|
7
|
+
data.tar.gz: 10d39becea4769759500d0846073ac61752e87e5a9324fc55886c494ccceb8dd7831830691c82a2b70d55eb2e7d1bb7adcfc2bb0ec1000b2e904cce111721c74
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pinball
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.2
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ dependencies between your classes.
|
|
8
8
|
|
9
9
|
# Features
|
10
10
|
|
11
|
-
* Stores objects, classes(as factories) and
|
11
|
+
* Stores objects, classes(as factories), blocks and singletons
|
12
12
|
* Can inject dependencies to classes and instances
|
13
13
|
* Simple DSL for configuring the container
|
14
14
|
* Stored block will be call in dependent class instance
|
@@ -97,6 +97,17 @@ This block will be executed it `FirstService` instance context where
|
|
97
97
|
`@current_user` will be accessible.
|
98
98
|
**Notice:** each call of `second_service` will call this block over and over again.
|
99
99
|
|
100
|
+
### Singleton injection
|
101
|
+
|
102
|
+
Instead of class injection, singleton injection will not create new objects every time. It will create only one and then
|
103
|
+
returns it. Perfect for stateless services and other singletons. Modifying of classes is not required.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class Service
|
107
|
+
inject_singleton :service
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
100
111
|
### class_inject
|
101
112
|
|
102
113
|
Sometimes you need to inject dependency to class, when it must be available in
|
data/lib/pinball.rb
CHANGED
data/lib/pinball/class.rb
CHANGED
data/lib/pinball/container.rb
CHANGED
@@ -24,13 +24,16 @@ module Pinball
|
|
24
24
|
Pinball::Container.instance.define(key, value, &block)
|
25
25
|
end
|
26
26
|
|
27
|
+
def define_singleton(key, klass)
|
28
|
+
Pinball::Container.instance.define_singleton(key, klass)
|
29
|
+
end
|
30
|
+
|
27
31
|
def undefine(key)
|
28
32
|
Pinball::Container.instance.undefine(key)
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
32
36
|
def initialize
|
33
|
-
|
34
37
|
@items = {}
|
35
38
|
end
|
36
39
|
|
@@ -38,14 +41,24 @@ module Pinball
|
|
38
41
|
@items[key] = ContainerItem.new(value || block)
|
39
42
|
end
|
40
43
|
|
44
|
+
def define_singleton(key, klass)
|
45
|
+
if klass.instance_method(:initialize).arity <= 0
|
46
|
+
@items[key] = ContainerItem.new(klass.new)
|
47
|
+
else
|
48
|
+
raise Pinball::WrongArity.new('Singleton dependency initializer should not have mandatory params')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
41
52
|
def undefine(key)
|
42
53
|
@items.delete(key)
|
43
54
|
end
|
44
55
|
|
45
56
|
def inject(target)
|
46
57
|
target.class.dependencies.each do |dep|
|
47
|
-
target.
|
48
|
-
|
58
|
+
unless target.respond_to?(dep)
|
59
|
+
target.define_singleton_method dep do
|
60
|
+
Container.instance.items.merge(overridden_dependencies)[dep].fetch(self) rescue raise Pinball::UnknownDependency.new("Dependency #{dep} is unknown, check your pinball config")
|
61
|
+
end
|
49
62
|
end
|
50
63
|
end
|
51
64
|
end
|
data/lib/pinball/version.rb
CHANGED
data/pinball.gemspec
CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
-
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.3'
|
23
23
|
|
24
|
-
spec.add_development_dependency 'coveralls'
|
25
|
-
spec.add_development_dependency 'rspec', '~> 2.14
|
24
|
+
spec.add_development_dependency 'coveralls', '~> 0.7'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
26
26
|
end
|
@@ -37,6 +37,21 @@ describe Class do
|
|
37
37
|
foo.inject :bar
|
38
38
|
expect(foo.dependencies).to match_array([:baz, :bar])
|
39
39
|
end
|
40
|
+
|
41
|
+
it 'resolves known dependencies' do
|
42
|
+
foo.inject :baz
|
43
|
+
foo.inject :bar
|
44
|
+
foo.inject :egg
|
45
|
+
|
46
|
+
expect(foo.new.baz).to eq(0)
|
47
|
+
expect(foo.new.bar).to eq(1)
|
48
|
+
expect(foo.new.egg).to eq(2)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'raises exception while resolving unknown dependecies' do
|
52
|
+
foo.inject :unknown
|
53
|
+
expect { foo.new.unknown }.to raise_error(Pinball::UnknownDependency)
|
54
|
+
end
|
40
55
|
end
|
41
56
|
|
42
57
|
describe '::class_inject' do
|
@@ -41,11 +41,41 @@ describe Pinball::Container do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
+
describe '::define_singleton' do
|
45
|
+
context 'with class without mandatory initializer params' do
|
46
|
+
it 'adds new container item' do
|
47
|
+
Pinball::Container.define_singleton :baz, String
|
48
|
+
expect(Pinball::Container.instance.items[:baz].value).to be_an_instance_of(String)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'with class with mandatory initialize params' do
|
53
|
+
it 'raises exception' do
|
54
|
+
WithMandatory = Class.new do
|
55
|
+
def initialize(param)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
expect{Pinball::Container.define_singleton :baz, WithMandatory}.to raise_error(Pinball::WrongArity)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
44
64
|
describe '::undefine' do
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
65
|
+
context 'with usual dependency' do
|
66
|
+
it 'removes container item' do
|
67
|
+
Pinball::Container.define :baz, 0
|
68
|
+
Pinball::Container.undefine :baz
|
69
|
+
expect(Pinball::Container.instance.items[:baz]).to be_nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with singleton dependency' do
|
74
|
+
it 'removes container item' do
|
75
|
+
Pinball::Container.define_singleton :baz, String
|
76
|
+
Pinball::Container.undefine :baz
|
77
|
+
expect(Pinball::Container.instance.items[:baz]).to be_nil
|
78
|
+
end
|
49
79
|
end
|
50
80
|
end
|
51
81
|
|
metadata
CHANGED
@@ -1,71 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pinball
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gleb Sinyavsky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '10.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '10.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: coveralls
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
47
|
+
version: '0.7'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
54
|
+
version: '0.7'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 2.14
|
61
|
+
version: '2.14'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 2.14
|
68
|
+
version: '2.14'
|
69
69
|
description: Simple and stupid IOC container and dependency injection tool for ruby
|
70
70
|
email:
|
71
71
|
- zhulik.gleb@gmail.com
|
@@ -73,9 +73,11 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
- .coveralls.yml
|
77
|
-
- .gitignore
|
78
|
-
- .
|
76
|
+
- ".coveralls.yml"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".ruby-gemset"
|
79
|
+
- ".ruby-version"
|
80
|
+
- ".travis.yml"
|
79
81
|
- Gemfile
|
80
82
|
- LICENSE
|
81
83
|
- LICENSE.txt
|
@@ -100,17 +102,17 @@ require_paths:
|
|
100
102
|
- lib
|
101
103
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
104
|
requirements:
|
103
|
-
- -
|
105
|
+
- - ">="
|
104
106
|
- !ruby/object:Gem::Version
|
105
107
|
version: '0'
|
106
108
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
109
|
requirements:
|
108
|
-
- -
|
110
|
+
- - ">="
|
109
111
|
- !ruby/object:Gem::Version
|
110
112
|
version: '0'
|
111
113
|
requirements: []
|
112
114
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.
|
115
|
+
rubygems_version: 2.2.2
|
114
116
|
signing_key:
|
115
117
|
specification_version: 4
|
116
118
|
summary: Simple dependency injection for ruby
|