pinball 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c2cda464327444a1c7fd499861105269f0272f3f
4
+ data.tar.gz: b99ffa3a55fbd7a9b183e699fd44fdec4181f345
5
+ SHA512:
6
+ metadata.gz: df4f00d3febebbf691c82205ac77625c918c4afe07f0c57c839d3f7f9942e24d4d5fd098a5962f18367068c64a1c7337257212f78d28c62c1498919c64270659
7
+ data.tar.gz: 1a88d82d7c6e6ce0d10777a316eb82dc5fe8dd39c3eb47b21e202b86752693081e8f5e573cd3e20eb154f5b70b52fbc7889139e0bd7bec2c7d72f9845e44c75e
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ script: bundle exec rspec
8
+ cache: bundler
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Gleb Sinyavsky
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Gleb Sinyavsky
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
File without changes
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,26 @@
1
+ class Class
2
+ def inject(*deps)
3
+ check_pinball
4
+ if @dependencies
5
+ @dependencies.concat(deps).uniq!
6
+ else
7
+ @dependencies = deps
8
+ end
9
+ end
10
+
11
+ def class_inject(*deps)
12
+ deps.each do |dep|
13
+ define_singleton_method dep do
14
+ Pinball::Container.lookup(dep).fetch(self)
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def check_pinball
22
+ unless self.is_a? Pinball
23
+ self.extend Pinball
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ require 'singleton'
2
+ require_relative 'container_item'
3
+
4
+ module Pinball
5
+ class Container
6
+ include Singleton
7
+
8
+ attr_reader :items
9
+
10
+ class << self
11
+ def configure(&block)
12
+ self.instance.instance_exec(&block)
13
+ end
14
+
15
+ def lookup(key)
16
+ Pinball::Container.instance.items[key]
17
+ end
18
+
19
+ def clear
20
+ Pinball::Container.instance.items.clear
21
+ end
22
+
23
+ def define(key, value = nil, &block)
24
+ Pinball::Container.instance.define(key, value, &block)
25
+ end
26
+
27
+ def undefine(key)
28
+ Pinball::Container.instance.undefine(key)
29
+ end
30
+ end
31
+
32
+ def initialize
33
+ @items = {}
34
+ end
35
+
36
+ def define(key, value = nil, &block)
37
+ @items[key] = ContainerItem.new(value || block)
38
+ end
39
+
40
+ def undefine(key)
41
+ @items.delete(key)
42
+ end
43
+
44
+ def inject(target)
45
+ target.class.dependencies.each do |dep|
46
+ target.define_singleton_method dep do
47
+ Container.instance.items[dep].fetch(self)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,19 @@
1
+ module Pinball
2
+ class ContainerItem
3
+ attr_accessor :value
4
+
5
+ def initialize(value)
6
+ @value = value
7
+ end
8
+
9
+ def fetch(target)
10
+ if value.is_a? Proc
11
+ target.instance_eval(&value)
12
+ elsif value.is_a? Module #Class definition
13
+ value.new
14
+ else
15
+ value
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Pinball
2
+ VERSION = '0.0.2'
3
+ end
data/lib/pinball.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'pinball/version'
2
+ require 'pinball/container'
3
+ require 'pinball/class'
4
+
5
+ module Pinball
6
+ def new(*args)
7
+ object = allocate
8
+ Container.instance.inject(object)
9
+ object.send(:initialize, *args)
10
+ object
11
+ end
12
+
13
+ def dependencies
14
+ @dependencies
15
+ end
16
+ end
data/pinball.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pinball/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pinball'
8
+ spec.version = Pinball::VERSION
9
+ spec.authors = ['Gleb Sinyavsky']
10
+ spec.email = ['zhulik.gleb@gmail.com']
11
+ spec.summary = 'Simple dependency injection for ruby'
12
+ spec.description = 'Simple and stupid IOC container and dependency injection tool for ruby'
13
+ spec.homepage = 'https://github.com/zhulik/pinball'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = []
18
+ spec.test_files = `git ls-files spec`.split($/)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_development_dependency 'coveralls'
25
+ spec.add_development_dependency 'rspec', '~> 2.14.1'
26
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper.rb'
2
+ require 'pinball'
3
+
4
+ Pinball::Container.configure do
5
+ define :baz, 0
6
+ define :bar, 0
7
+ end
8
+
9
+ describe Class do
10
+ let!(:foo) { Class.new }
11
+
12
+ describe '::inject' do
13
+ it 'responds to ::inject method' do
14
+ expect(foo.respond_to?(:inject)).to be_true
15
+ end
16
+
17
+ it 'creates @dependencies array' do
18
+ foo.inject :baz
19
+ expect(foo.instance_variable_get('@dependencies')).to match_array([:baz])
20
+ end
21
+
22
+ it 'adds items to @dependencies array' do
23
+ foo.inject :baz
24
+ foo.inject :bar
25
+ expect(foo.instance_variable_get('@dependencies')).to match_array([:baz, :bar])
26
+ end
27
+
28
+ it 'doesn\'t duplicate dependencies' do
29
+ foo.inject :baz
30
+ foo.inject :bar
31
+ foo.inject :bar
32
+ expect(foo.instance_variable_get('@dependencies')).to match_array([:baz, :bar])
33
+ end
34
+ end
35
+
36
+ describe '::class_inject' do
37
+ it 'defines new method' do
38
+ foo.class_inject :baz
39
+ expect(foo.respond_to?(:baz)).to be_true
40
+ end
41
+
42
+ it 'injects valid dependency' do
43
+ Pinball::Container.define :baz, 0
44
+ foo.class_inject :baz
45
+ expect(foo.baz).to eq(0)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper.rb'
2
+ require 'pinball'
3
+
4
+ describe Pinball::Container do
5
+ before do
6
+ Pinball::Container.clear
7
+ end
8
+
9
+ describe '::configure' do
10
+ it 'allows to add new container item' do
11
+ Pinball::Container.configure do
12
+ define :baz, 0
13
+ end
14
+ expect(Pinball::Container.instance.items[:baz]).to be_an_instance_of(Pinball::ContainerItem)
15
+ end
16
+ end
17
+
18
+ describe '::lookup' do
19
+ it 'returns container_item' do
20
+ Pinball::Container.configure do
21
+ define :baz, 0
22
+ end
23
+ expect(Pinball::Container.lookup(:baz)).to be_an_instance_of(Pinball::ContainerItem)
24
+ end
25
+ end
26
+
27
+ describe '::clear' do
28
+ it 'clears all container items' do
29
+ Pinball::Container.configure do
30
+ define :baz, 0
31
+ end
32
+ Pinball::Container.clear
33
+ expect(Pinball::Container.lookup(:baz)).to be_nil
34
+ end
35
+ end
36
+
37
+ describe '::define' do
38
+ it 'adds new container item' do
39
+ Pinball::Container.define :baz, 0
40
+ expect(Pinball::Container.instance.items[:baz]).to be_an_instance_of(Pinball::ContainerItem)
41
+ end
42
+ end
43
+
44
+ describe '::undefine' do
45
+ it 'removes container item' do
46
+ Pinball::Container.define :baz, 0
47
+ Pinball::Container.undefine :baz
48
+ expect(Pinball::Container.instance.items[:baz]).to be_nil
49
+ end
50
+ end
51
+
52
+ describe '#define' do
53
+ it 'adds new container item' do
54
+ Pinball::Container.instance.define :baz, 0
55
+ expect(Pinball::Container.instance.items[:baz]).to be_an_instance_of(Pinball::ContainerItem)
56
+ end
57
+ end
58
+
59
+ describe '#undefine' do
60
+ it 'removes container item' do
61
+ Pinball::Container.instance.define :baz, 0
62
+ Pinball::Container.instance.undefine :baz
63
+ expect(Pinball::Container.instance.items[:baz]).to be_nil
64
+ end
65
+ end
66
+
67
+ describe '#inject' do
68
+ let!(:foo) { Class.new{ inject :baz, :bar, :spam } }
69
+
70
+ before do
71
+ Pinball::Container.configure do
72
+ define :baz, 0
73
+ define :bar, Hash
74
+ define :spam do
75
+ Array.new
76
+ end
77
+ end
78
+ end
79
+
80
+ it 'automatically injects dependencies to class' do
81
+ expect(foo.new.respond_to?(:baz)).to be_true
82
+ expect(foo.new.respond_to?(:bar)).to be_true
83
+ expect(foo.new.respond_to?(:spam)).to be_true
84
+ end
85
+
86
+ it 'injects valid dependencies' do
87
+ expect(foo.new.baz).to eq(0)
88
+ expect(foo.new.bar).to be_an_instance_of(Hash)
89
+ expect(foo.new.spam).to be_an_instance_of(Array)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,9 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+
5
+ $LOAD_PATH << '../lib'
6
+
7
+ RSpec.configure do |config|
8
+ config.order = 'random'
9
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pinball
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Gleb Sinyavsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.1
69
+ description: Simple and stupid IOC container and dependency injection tool for ruby
70
+ email:
71
+ - zhulik.gleb@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .coveralls.yml
77
+ - .gitignore
78
+ - .travis.yml
79
+ - Gemfile
80
+ - LICENSE
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/pinball.rb
85
+ - lib/pinball/class.rb
86
+ - lib/pinball/container.rb
87
+ - lib/pinball/container_item.rb
88
+ - lib/pinball/version.rb
89
+ - pinball.gemspec
90
+ - spec/lib/pinball/class_spec.rb
91
+ - spec/lib/pinball/container_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: https://github.com/zhulik/pinball
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.0.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Simple dependency injection for ruby
117
+ test_files:
118
+ - spec/lib/pinball/class_spec.rb
119
+ - spec/lib/pinball/container_spec.rb
120
+ - spec/spec_helper.rb