smart_container 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +15 -0
  5. data/.travis.yml +28 -0
  6. data/CHANGELOG.md +6 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +4 -0
  9. data/Gemfile.lock +83 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +39 -0
  12. data/Rakefile +21 -0
  13. data/bin/console +8 -0
  14. data/bin/setup +8 -0
  15. data/lib/smart_core/container.rb +135 -0
  16. data/lib/smart_core/container/arbitary_lock.rb +22 -0
  17. data/lib/smart_core/container/definition_dsl.rb +109 -0
  18. data/lib/smart_core/container/definition_dsl/command_set.rb +87 -0
  19. data/lib/smart_core/container/definition_dsl/commands.rb +9 -0
  20. data/lib/smart_core/container/definition_dsl/commands/base.rb +48 -0
  21. data/lib/smart_core/container/definition_dsl/commands/definition.rb +9 -0
  22. data/lib/smart_core/container/definition_dsl/commands/definition/compose.rb +49 -0
  23. data/lib/smart_core/container/definition_dsl/commands/definition/namespace.rb +54 -0
  24. data/lib/smart_core/container/definition_dsl/commands/definition/register.rb +54 -0
  25. data/lib/smart_core/container/definition_dsl/commands/instantiation.rb +8 -0
  26. data/lib/smart_core/container/definition_dsl/commands/instantiation/compose.rb +53 -0
  27. data/lib/smart_core/container/definition_dsl/commands/instantiation/freeze_state.rb +27 -0
  28. data/lib/smart_core/container/definition_dsl/inheritance.rb +23 -0
  29. data/lib/smart_core/container/dependency_compatability.rb +9 -0
  30. data/lib/smart_core/container/dependency_compatability/definition.rb +38 -0
  31. data/lib/smart_core/container/dependency_compatability/general.rb +61 -0
  32. data/lib/smart_core/container/dependency_compatability/registry.rb +36 -0
  33. data/lib/smart_core/container/dependency_resolver.rb +93 -0
  34. data/lib/smart_core/container/dependency_resolver/route.rb +83 -0
  35. data/lib/smart_core/container/dependency_resolver/route/cursor.rb +47 -0
  36. data/lib/smart_core/container/entities.rb +11 -0
  37. data/lib/smart_core/container/entities/base.rb +26 -0
  38. data/lib/smart_core/container/entities/dependency.rb +38 -0
  39. data/lib/smart_core/container/entities/dependency_builder.rb +50 -0
  40. data/lib/smart_core/container/entities/namespace.rb +73 -0
  41. data/lib/smart_core/container/entities/namespace_builder.rb +41 -0
  42. data/lib/smart_core/container/errors.rb +65 -0
  43. data/lib/smart_core/container/key_guard.rb +31 -0
  44. data/lib/smart_core/container/mixin.rb +83 -0
  45. data/lib/smart_core/container/registry.rb +250 -0
  46. data/lib/smart_core/container/registry_builder.rb +51 -0
  47. data/lib/smart_core/container/version.rb +9 -0
  48. data/smart_container.gemspec +38 -0
  49. metadata +191 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 238540cd0bdf9e1753b190d639cf1b0de8876ec5fbe5c5b152235543177079e5
4
+ data.tar.gz: aab0620b71b507954e40a3b6e92aee2928432624c3db32b8fdde2a2c7c09a347
5
+ SHA512:
6
+ metadata.gz: 21c636030e9b8911c7af18af53d4450676fdf7ca14d3438354bf6da7b0c3375441ee06099fb06774cef311930e418e3d9b95c1b1528826b6f291cef82140195c
7
+ data.tar.gz: 9e45275d4c8cea45fc87590650f67b1b677ce44a3b1e3524206e7ebf6ec3dc8f29d6cd3e1e5a0c409a6a5f21cfc268ae65c9c82e51a59dac9154f41fd8dc289f
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .rspec_status
10
+ /.idea
11
+ .ruby-version
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,15 @@
1
+ inherit_gem:
2
+ armitage-rubocop:
3
+ - lib/rubocop.general.yml
4
+ - lib/rubocop.rake.yml
5
+ - lib/rubocop.rspec.yml
6
+
7
+ AllCops:
8
+ TargetRubyVersion: 2.4.9
9
+ Include:
10
+ - lib/**/*.rb
11
+ - spec/**/*.rb
12
+ - Gemfile
13
+ - Rakefile
14
+ - siege.gemspec
15
+ - bin/console
@@ -0,0 +1,28 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ before_install:
5
+ - gem install bundler -v 2.1.2
6
+ script:
7
+ - bundle exec rake rubocop
8
+ - bundle exec rake rspec
9
+ jobs:
10
+ fast_finish: true
11
+ include:
12
+ - rvm: 2.4.9
13
+ os: [linux, osx]
14
+ - rvm: 2.5.7
15
+ os: [linux, osx]
16
+ - rvm: 2.6.5
17
+ os: [linux, osx]
18
+ - rvm: 2.7.0
19
+ os: [linux, osx]
20
+ - rvm: ruby-head
21
+ os: [linux, osx]
22
+ - rvm: jruby-head
23
+ os: [linux, osx]
24
+ allow_failures:
25
+ - rvm: ruby-head
26
+ os: [linux, osx]
27
+ - rvm: jruby-head
28
+ os: [linux, osx]
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## [0.1.0] - 2020-01-02
5
+
6
+ - Release :)
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at iamdaiver@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ gemspec
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ smart_container (0.1.0)
5
+ smart_engine (~> 0.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ armitage-rubocop (0.78.0)
11
+ rubocop (= 0.78.0)
12
+ rubocop-performance (= 1.5.2)
13
+ rubocop-rails (= 2.4.1)
14
+ rubocop-rake (= 0.5.0)
15
+ rubocop-rspec (= 1.37.1)
16
+ ast (2.4.0)
17
+ coderay (1.1.2)
18
+ diff-lcs (1.3)
19
+ docile (1.3.2)
20
+ jaro_winkler (1.5.4)
21
+ json (2.3.0)
22
+ method_source (0.9.2)
23
+ parallel (1.19.1)
24
+ parser (2.7.0.1)
25
+ ast (~> 2.4.0)
26
+ pry (0.12.2)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.9.0)
29
+ rack (2.0.8)
30
+ rainbow (3.0.0)
31
+ rake (13.0.1)
32
+ rspec (3.9.0)
33
+ rspec-core (~> 3.9.0)
34
+ rspec-expectations (~> 3.9.0)
35
+ rspec-mocks (~> 3.9.0)
36
+ rspec-core (3.9.1)
37
+ rspec-support (~> 3.9.1)
38
+ rspec-expectations (3.9.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.9.0)
41
+ rspec-mocks (3.9.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.9.0)
44
+ rspec-support (3.9.2)
45
+ rubocop (0.78.0)
46
+ jaro_winkler (~> 1.5.1)
47
+ parallel (~> 1.10)
48
+ parser (>= 2.6)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ ruby-progressbar (~> 1.7)
51
+ unicode-display_width (>= 1.4.0, < 1.7)
52
+ rubocop-performance (1.5.2)
53
+ rubocop (>= 0.71.0)
54
+ rubocop-rails (2.4.1)
55
+ rack (>= 1.1)
56
+ rubocop (>= 0.72.0)
57
+ rubocop-rake (0.5.0)
58
+ rubocop
59
+ rubocop-rspec (1.37.1)
60
+ rubocop (>= 0.68.1)
61
+ ruby-progressbar (1.10.1)
62
+ simplecov (0.17.1)
63
+ docile (~> 1.1)
64
+ json (>= 1.8, < 3)
65
+ simplecov-html (~> 0.10.0)
66
+ simplecov-html (0.10.2)
67
+ smart_engine (0.2.0)
68
+ unicode-display_width (1.6.0)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ armitage-rubocop (~> 0.78)
75
+ bundler (~> 2.1)
76
+ pry (~> 0.12)
77
+ rake (~> 13.0)
78
+ rspec (~> 3.9)
79
+ simplecov (~> 0.17)
80
+ smart_container!
81
+
82
+ BUNDLED WITH
83
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Rustam Ibragimov
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # SmartCore::Container &middot; [![Gem Version](https://badge.fury.io/rb/smart_container.svg)](https://badge.fury.io/rb/smart_container) [![Build Status](https://travis-ci.org/smart-rb/smart_container.svg?branch=master)](https://travis-ci.org/smart-rb/smart_container)
2
+
3
+ Thread-safe semanticaly-defined IoC/DI Container.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ gem 'smart_container'
11
+ ```
12
+
13
+ ```shell
14
+ bundle install
15
+ # --- or ---
16
+ gem install smart_container
17
+ ```
18
+
19
+ ```ruby
20
+ require 'smart_core/container'
21
+ ```
22
+
23
+ ---
24
+
25
+ ## Contributing
26
+
27
+ - Fork it ( https://github.com/smart-rb/smart_container/fork )
28
+ - Create your feature branch (`git checkout -b feature/my-new-feature`)
29
+ - Commit your changes (`git commit -am '[feature_context] Add some feature'`)
30
+ - Push to the branch (`git push origin feature/my-new-feature`)
31
+ - Create new Pull Request
32
+
33
+ ## License
34
+
35
+ Released under MIT License.
36
+
37
+ ## Authors
38
+
39
+ [Rustam Ibragimov](https://github.com/0exp)
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop'
6
+ require 'rubocop/rake_task'
7
+ require 'rubocop-performance'
8
+ require 'rubocop-rspec'
9
+ require 'rubocop-rake'
10
+
11
+ RuboCop::RakeTask.new(:rubocop) do |t|
12
+ config_path = File.expand_path(File.join('.rubocop.yml'), __dir__)
13
+ t.options = ['--config', config_path]
14
+ t.requires << 'rubocop-performance'
15
+ t.requires << 'rubocop-rspec'
16
+ t.requires << 'rubocop-rake'
17
+ end
18
+
19
+ RSpec::Core::RakeTask.new(:rspec)
20
+
21
+ task default: :rspec
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'smart_core/container'
6
+
7
+ require 'irb'
8
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'smart_core'
4
+
5
+ # @api public
6
+ # @since 0.1.0
7
+ module SmartCore
8
+ # @api public
9
+ # @since 0.1.0
10
+ class Container
11
+ require_relative 'container/version'
12
+ require_relative 'container/errors'
13
+ require_relative 'container/arbitary_lock'
14
+ require_relative 'container/key_guard'
15
+ require_relative 'container/entities'
16
+ require_relative 'container/definition_dsl'
17
+ require_relative 'container/dependency_compatability'
18
+ require_relative 'container/registry'
19
+ require_relative 'container/registry_builder'
20
+ require_relative 'container/dependency_resolver'
21
+ require_relative 'container/mixin'
22
+
23
+ # @since 0.1.0
24
+ include DefinitionDSL
25
+
26
+ # @return [SmartCore::Container::Registry]
27
+ #
28
+ # @api private
29
+ # @since 0.1.0
30
+ attr_reader :registry
31
+
32
+ # @return [void]
33
+ #
34
+ # @api public
35
+ # @since 0.1.0
36
+ def initialize
37
+ build_registry!
38
+ @access_lock = ArbitaryLock.new
39
+ end
40
+
41
+ # @param dependency_name [String, Symbol]
42
+ # @param dependency_definition [Block]
43
+ # @return [void]
44
+ #
45
+ # @api public
46
+ # @sicne 0.1.0
47
+ def register(dependency_name, &dependency_definition)
48
+ thread_safe { registry.register_dependency(dependency_name, &dependency_definition) }
49
+ end
50
+
51
+ # @param namespace_name [String, Symbol]
52
+ # @param dependencies_definition [Block]
53
+ # @return [void]
54
+ #
55
+ # @api public
56
+ # @since 0.1.0
57
+ def namespace(namespace_name, &dependencies_definition)
58
+ thread_safe { registry.register_namespace(namespace_name, &dependencies_definition) }
59
+ end
60
+
61
+ # @param dependency_path [String, Symbol]
62
+ # @return [Any]
63
+ #
64
+ # @api public
65
+ # @since 0.1.0
66
+ # @version 0.1.0
67
+ def resolve(dependency_path)
68
+ thread_safe { DependencyResolver.resolve(self, dependency_path) }
69
+ end
70
+ alias_method :[], :resolve
71
+
72
+ # @param dependency_path [String, Symbol]
73
+ # @return [Any]
74
+ #
75
+ # @api public
76
+ # @since 0.1.0
77
+ def fetch(dependency_path)
78
+ thread_safe { DependencyResolver.fetch(self, dependency_path) }
79
+ end
80
+
81
+ # @return [void]
82
+ #
83
+ # @api public
84
+ # @since 0.1.0
85
+ def freeze!
86
+ thread_safe { registry.freeze! }
87
+ end
88
+
89
+ # @return [Boolean]
90
+ #
91
+ # @api public
92
+ # @since 0.1.0
93
+ def frozen?
94
+ thread_safe { registry.frozen? }
95
+ end
96
+
97
+ # @return [void]
98
+ #
99
+ # @api public
100
+ # @since 0.1.0
101
+ def reload!
102
+ thread_safe { build_registry! }
103
+ end
104
+
105
+ # @option resolve_dependencies [Boolean]
106
+ # @return [Hash<String|Symbol,SmartCore::Container::Entities::Base|Any>]
107
+ #
108
+ # @api public
109
+ # @since 0.1.0
110
+ def hash_tree(resolve_dependencies: false)
111
+ thread_safe { registry.hash_tree(resolve_dependencies: resolve_dependencies) }
112
+ end
113
+ alias_method :to_h, :hash_tree
114
+ alias_method :to_hash, :hash_tree
115
+
116
+ private
117
+
118
+ # @return [void]
119
+ #
120
+ # @api private
121
+ # @since 0.1.0
122
+ def build_registry!
123
+ @registry = RegistryBuilder.build(self)
124
+ end
125
+
126
+ # @param block [Block]
127
+ # @return [Any]
128
+ #
129
+ # @api private
130
+ # @since 0.1.0
131
+ def thread_safe(&block)
132
+ @access_lock.thread_safe(&block)
133
+ end
134
+ end
135
+ end