yadi 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 88cf04c9164469ee80198812cf4a5d33c605c85e
4
+ data.tar.gz: e581eef3673cef4cf4715e9256072adab4d1620c
5
+ SHA512:
6
+ metadata.gz: 97b02b3601c974bd1fdfa28499b8803768a3db345101da74bb9087a13c2f20490497933a196a07b8bd53478c6dc114a2855f4ec19fd477d4c69be1cce4f1e0ac
7
+ data.tar.gz: d43c9f982f99da881e4f25110b0f340ff22c3660f6bb8883e39d5437b6f7813425b305de4b6c7826f6d5e077fe71a1ea3fe78ab18edfe465eb935ac55939a3a6
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 Roman Exempliarov
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.
@@ -0,0 +1,75 @@
1
+ Yet another dependency injection container.
2
+
3
+ ## Why?
4
+
5
+ * No need to pre-fill container
6
+ * Classes can be used without a container
7
+
8
+ ## Usage
9
+
10
+ #### Auto-inject classes
11
+
12
+ ```ruby
13
+ class Foo; end
14
+ class Bar; end
15
+
16
+ class Baz
17
+ # this tells to inject Foo instance as first argument and Bar as bar: option
18
+ include Yadi::Inject('Foo', bar: 'Bar')
19
+ def initialize(foo, bar:)
20
+ @foo, @bar = foo, bar
21
+ end
22
+ end
23
+
24
+ container = Yadi::Container.new
25
+ baz = container.make('Baz')
26
+
27
+ # this gives the same result
28
+ baz = Baz.new(Foo.new, bar: Bar.new)
29
+ ```
30
+
31
+ #### Auto-inject in service container style
32
+
33
+ ```ruby
34
+ class Foo; end
35
+ class Bar; end
36
+
37
+ class Baz
38
+ # this tells to inject values from container
39
+ include Yadi::Inject('service.foo', bar: 'config.bar')
40
+ def initialize(foo, bar:)
41
+ @foo, @bar = foo, bar
42
+ end
43
+ end
44
+
45
+ container = Yadi::Container.new
46
+ container['service.foo'] = Foo.new
47
+ container['config.bar'] = 'bar'
48
+ baz = container.make('Baz')
49
+
50
+ # this gives the same result
51
+ baz = Baz.new(Foo.new, bar: 'bar')
52
+ ```
53
+
54
+ ## Installation
55
+
56
+ Add this line to your application's Gemfile:
57
+
58
+ ```ruby
59
+ gem 'yadi'
60
+ ```
61
+
62
+ And then execute:
63
+
64
+ ```sh
65
+ $ bundle
66
+ ```
67
+
68
+ Or install it yourself as:
69
+ ```sh
70
+ $ gem install yadi
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/appelsin/yadi.
@@ -0,0 +1,8 @@
1
+ require 'yadi/module_builder'
2
+ require 'yadi/container'
3
+
4
+ module Yadi
5
+ def self.Inject(*args)
6
+ ModuleBuilder.new(*args)
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ module Yadi
2
+ class Box
3
+ def initialize(contents)
4
+ @contents = contents
5
+ end
6
+
7
+ def call(container)
8
+ @contents
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Yadi
2
+ class Constantize
3
+ def self.call(klass_name)
4
+ klass_name.to_s
5
+ .split('::')
6
+ .reduce(Object) do |m, v|
7
+ m.const_get(v)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ require 'yadi/maker'
2
+ require 'yadi/box'
3
+ require 'yadi/klass_box'
4
+ require 'yadi/factory_box'
5
+
6
+ module Yadi
7
+ class Container
8
+ def initialize
9
+ @repo = {}
10
+ end
11
+
12
+ def register!(*args)
13
+ args_count = args.count
14
+ if 1 == args_count
15
+ register_klass(*args)
16
+ elsif args_count > 1
17
+ register_value(*args)
18
+ else
19
+ raise 'Unknown register call pattern'
20
+ end
21
+ end
22
+
23
+ def has?(name)
24
+ @repo.has_key?(name)
25
+ end
26
+
27
+ def register(*args)
28
+ name = args[0]
29
+ if has?(name)
30
+ raise "'#{name}' already registered. Use register! if you whant to force rewrite value."
31
+ else
32
+ register!(*args)
33
+ end
34
+ end
35
+
36
+ def register_value(name, value, wrap: Box)
37
+ @repo[name] = wrap.new(value)
38
+ end
39
+ alias_method :[]=, :register_value
40
+
41
+ def register_klass(name)
42
+ @repo[name] = KlassBox.new(name)
43
+ end
44
+ alias_method :<<, :register_klass
45
+
46
+ def make(name)
47
+ box = has?(name) ? @repo[name] : register_klass(name)
48
+ box.(self)
49
+ end
50
+ alias_method :[], :make
51
+
52
+ def resolve(name)
53
+ if has?(name)
54
+ box = @repo[name]
55
+ box.(self)
56
+ else
57
+ raise "Can't resolve #{name}"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,13 @@
1
+ module Yadi
2
+ class FactoryBox
3
+ attr_reader :contents
4
+
5
+ def initialize(klass_name)
6
+ @klass_name = klass_name
7
+ end
8
+
9
+ def call(container)
10
+ Maker.call(container, @klass_name)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module Yadi
2
+ class Injector
3
+ attr_reader :args
4
+ attr_reader :options
5
+
6
+ def initialize(args)
7
+ options = args.last.is_a?(::Hash) ? args.pop : {}
8
+ @args = args.freeze
9
+ @options = options.freeze
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Yadi
2
+ class KlassBox
3
+ def initialize(klass_name)
4
+ @klass_name = klass_name
5
+ end
6
+
7
+ def call(container)
8
+ @contents||= Maker.call(container, @klass_name)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ require 'yadi/constantize'
2
+
3
+ module Yadi
4
+ class Maker
5
+ def self.call(container, klass_name)
6
+ klass = Constantize.(klass_name)
7
+
8
+ if klass.const_defined?(:INITIALIZE_INJECT)
9
+ args = klass::INITIALIZE_INJECT.args.map do |inj|
10
+ container.make(inj)
11
+ end
12
+
13
+ options = ::Hash[
14
+ klass::INITIALIZE_INJECT.options.map do |key, inj|
15
+ [key, container.make(inj)]
16
+ end
17
+ ]
18
+
19
+ args << options unless options.empty?
20
+
21
+ klass.new *args
22
+ else
23
+ klass.new
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ require 'yadi/injector'
2
+
3
+ module Yadi
4
+ class ModuleBuilder < ::Module
5
+ def initialize(*args)
6
+ @args = args
7
+ end
8
+
9
+ def included(klass)
10
+ klass.const_set('INITIALIZE_INJECT', Injector.new(@args))
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Yadi
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yadi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'yadi'
8
+ spec.version = Yadi::VERSION.dup
9
+ spec.authors = ['Roman Exempliarov']
10
+ spec.email = ['rexe@ya.ru']
11
+ spec.license = 'MIT'
12
+
13
+ spec.summary = 'Yet another dependency injection container'
14
+ spec.homepage = 'https://github.com/appelsin/yadi'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+
23
+ # spec.add_runtime_dependency '', '>= 0.3.4'
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec'
28
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yadi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Exempliarov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-02 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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: rspec
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
+ description:
56
+ email:
57
+ - rexe@ya.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/yadi.rb
68
+ - lib/yadi/box.rb
69
+ - lib/yadi/constantize.rb
70
+ - lib/yadi/container.rb
71
+ - lib/yadi/factory_box.rb
72
+ - lib/yadi/injector.rb
73
+ - lib/yadi/klass_box.rb
74
+ - lib/yadi/maker.rb
75
+ - lib/yadi/module_builder.rb
76
+ - lib/yadi/version.rb
77
+ - yadi.gemspec
78
+ homepage: https://github.com/appelsin/yadi
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.9.3
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Yet another dependency injection container
102
+ test_files: []