koine-di 1.0.0

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
+ SHA256:
3
+ metadata.gz: 4ef78903580aadd9cdc99368872217d256ffece8a19f7104af9d20e2ba630ffb
4
+ data.tar.gz: b0ebaaa7a05693689e8fc6e9ef1f93296da9a97a74be4f75745b97f0564f674e
5
+ SHA512:
6
+ metadata.gz: e52aa1ebc41a064dc1770e6f54a0aba97dd19c2212a5211c208f8056c4c136c581c401f0518d4992cfc08da0aaadb5fa9a95efb5ad522511545e47923a08b4c7
7
+ data.tar.gz: e02b730b3b2fa823ddeeae232e3d1b372450c4e09c2ee6506fc988ed4eedcaf6050bd65874cea1a537958c270fe72562f98952e8307cbe1275d5f58b75cbfce8
@@ -0,0 +1,24 @@
1
+ name: Unit tests
2
+ on:
3
+ push:
4
+ branches:
5
+ - master
6
+ pull_request:
7
+ branches:
8
+ - '*'
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - uses: ruby/setup-ruby@v1
15
+ - name: Build and test with Rake
16
+ run: |
17
+ sudo apt-get -yqq install libpq-dev
18
+ bundle install --jobs 4 --retry 3
19
+ bundle exec rake test:coverage
20
+ - name: Coveralls
21
+ uses: coverallsapp/github-action@master
22
+ with:
23
+ github-token: ${{ secrets.GITHUB_TOKEN }}
24
+
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.0.0
data/.scrutinizer.yml ADDED
@@ -0,0 +1,15 @@
1
+ checks:
2
+ ruby:
3
+ code_rating: true
4
+ duplicate_code: true
5
+
6
+ build:
7
+ tests:
8
+ override:
9
+ -
10
+ command: 'bundle exec rake test:scrutinizer'
11
+ environment:
12
+ 'SCRUTINIZER_CC_FILE': 'my-coverage'
13
+ coverage:
14
+ file: 'my-coverage'
15
+ format: 'rb-cc'
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundle
4
+ rvm:
5
+ # - 1.9.3
6
+ - 2.0
7
+ - 2.1
8
+ - 2.2
9
+ - 2.3.0
10
+ - 2.4.0
11
+ - 2.4.3
12
+ - 2.5.0
13
+ script: rake test:coveralls
14
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nurse.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-2017 Marcelo Jacobus
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.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # Koine::Di
2
+
3
+ Koine::Di, for your dependency injection
4
+ A fork of [Nurse](https://github.com/mjacobus/nurse-rb) under a safer namespace.
5
+
6
+ Code quality
7
+
8
+ [![Coverage Status](https://coveralls.io/repos/github/mjacobus/koine-di/badge.svg?branch=master)](https://coveralls.io/github/mjacobus/koine-di?branch=master)
9
+ [![Code Climate](https://codeclimate.com/github/mjacobus/koine-di/badges/gpa.svg)](https://codeclimate.com/github/mjacobus/koine-di)
10
+
11
+ Package information
12
+
13
+ [![Gem Version](https://badge.fury.io/rb/koine-di.svg)](https://badge.fury.io/rb/koine-di)
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'koine-di'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install koine-di
30
+
31
+ ## Usage
32
+
33
+ ### Defining dependencies
34
+
35
+ ```ruby
36
+ dependency_manager = Koine::Di::DependencyContainer.new
37
+
38
+ dependency_manager.share(:connection) do |dependency_manager|
39
+ MyConnection.new("mysql://root@localhost/my_db")
40
+ end
41
+
42
+ dependency_manager.share(:user_repository) do |dependency_manager|
43
+ connection = dependency_manager.get(:connection)
44
+ UserRepository.new(connection)
45
+ end
46
+ ```
47
+
48
+ Also, you can use the singleton instance. Use singleton if you do not have
49
+ control over how classes, such as controllers, are created.
50
+
51
+ ```ruby
52
+ dependency_manager = Koine::Di.instance
53
+ ```
54
+
55
+ ### Fetching dependencies
56
+ ```ruby
57
+ class UsersController < SomeBaseController
58
+ def index
59
+ @users = repository.find_all
60
+ end
61
+
62
+ private
63
+
64
+ def repository
65
+ dependency_manager.get(:user_repository)
66
+ end
67
+ end
68
+ ```
69
+
70
+ ### Using factories
71
+
72
+ ```ruby
73
+ class DatabaseConnectionFactory < Koine::Di::ServiceFactory
74
+ share true
75
+ key :db_connection
76
+
77
+ def create_service(dependencies)
78
+ DatabaseConnection.new(dependencies.get(:db_config))
79
+ end
80
+ end
81
+
82
+ class UserRepositoryFactory < Koine::Di::ServiceFactory
83
+ key :user_repository
84
+
85
+ def create_service(dependencies)
86
+ UserRepository.new(dependencies.get(:db_connection))
87
+ end
88
+ end
89
+
90
+ dependency_manager.add_factory(DatabaseConnectionFactory.new)
91
+ dependency_manager.add_factory(UserRepositoryFactory.new)
92
+ ```
93
+
94
+ ## Development
95
+
96
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
97
+
98
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
99
+
100
+ ## Contributing
101
+
102
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mjacobus/koine-di. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
103
+
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
108
+
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'spec'
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ namespace :test do
11
+ task :coveralls do
12
+ ENV['COVERALLS'] = 'true'
13
+ Rake::Task['test:coverage'].invoke
14
+ end
15
+
16
+ task :coverage do
17
+ ENV['COVERAGE'] = 'true'
18
+ Rake::Task['test'].invoke
19
+ end
20
+
21
+ task :scrutinizer do
22
+ ENV['SCRUTINIZER'] = 'true'
23
+ Rake::Task['test'].invoke
24
+ end
25
+ end
26
+
27
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'nurse'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/koine-di.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'koine/di/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'koine-di'
8
+ spec.version = Koine::Di::VERSION
9
+ spec.authors = ['Marcelo Jacobus']
10
+ spec.email = ['marcelo.jacobus@gmail.com']
11
+
12
+ spec.summary = 'Koine DI, for your dependency injection'
13
+ spec.description = 'Koine DI, for your dependency injection. Dependency injection container'
14
+ spec.homepage = 'https://github.com/mjacobus/koine-di'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ fail 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundler'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'minitest'
33
+ spec.add_development_dependency 'minitest-reporters'
34
+ spec.add_development_dependency 'scrutinizer-ocular'
35
+ spec.add_development_dependency 'coveralls'
36
+ spec.add_development_dependency 'simplecov-lcov'
37
+ spec.add_development_dependency 'simplecov'
38
+ end
@@ -0,0 +1,73 @@
1
+ module Koine
2
+ module Di
3
+ # The dependency container class
4
+ class DependencyContainer
5
+ class UndefinedDependency < RuntimeError; end
6
+ class DependencyAlreadyDefined < RuntimeError; end
7
+
8
+ def initialize
9
+ @factories = {}
10
+ @shared_factories = {}
11
+ @instances = {}
12
+ end
13
+
14
+ def share(dependency, &block)
15
+ ensure_undefined(dependency)
16
+ shared_factories[to_key(dependency)] = block
17
+ self
18
+ end
19
+
20
+ def set(dependency, &block)
21
+ ensure_undefined(dependency)
22
+ factories[to_key(dependency)] = block
23
+ self
24
+ end
25
+
26
+ def defined?(dependency)
27
+ key = to_key(dependency)
28
+ shared_factories.key?(key) || factories.key?(key)
29
+ end
30
+
31
+ def get(dependency, &_block)
32
+ key = to_key(dependency)
33
+
34
+ if self.defined?(key)
35
+ return from_shared_factories(key) || from_factories(key)
36
+ end
37
+
38
+ return yield if block_given?
39
+
40
+ fail UndefinedDependency, "'#{dependency}' was not defined"
41
+ end
42
+
43
+ def add_factory(factory)
44
+ factory.attach_to(self)
45
+ end
46
+
47
+ protected
48
+
49
+ attr_reader :factories
50
+ attr_reader :instances
51
+ attr_reader :shared_factories
52
+
53
+ def from_shared_factories(key)
54
+ return unless shared_factories.key?(key)
55
+ instances[key] ||= shared_factories[key].call(self)
56
+ end
57
+
58
+ def from_factories(key)
59
+ factories[key].call(self) if factories.key?(key)
60
+ end
61
+
62
+ def ensure_undefined(dependency)
63
+ return unless self.defined?(dependency)
64
+ fail DependencyAlreadyDefined,
65
+ "Dependency '#{dependency}' was already defined"
66
+ end
67
+
68
+ def to_key(object)
69
+ object.to_s
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,51 @@
1
+ module Koine
2
+ module Di
3
+ class ServiceFactory
4
+ def attach_to(dependency_manager)
5
+ if shared?
6
+ dependency_manager.share(dependency_key) do |di|
7
+ create_service(di)
8
+ end
9
+
10
+ return
11
+ end
12
+
13
+ dependency_manager.set(dependency_key) do |di|
14
+ create_service(di)
15
+ end
16
+ end
17
+
18
+ def create_service(_dependency_manager)
19
+ raise 'create_service must be implemented'
20
+ end
21
+
22
+ def dependency_key
23
+ raise 'dependency_key must be implemented'
24
+ end
25
+
26
+ def shared?
27
+ false
28
+ end
29
+
30
+ class << self
31
+ private
32
+
33
+ def share(boolean)
34
+ instance_eval do
35
+ define_method :shared? do
36
+ boolean
37
+ end
38
+ end
39
+ end
40
+
41
+ def key(dependency_key)
42
+ instance_eval do
43
+ define_method :dependency_key do
44
+ dependency_key
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ module Koine
2
+ module Di
3
+ class SharedServiceFactory < ServiceFactory
4
+ def shared?
5
+ true
6
+ end
7
+
8
+ class << self
9
+ private
10
+
11
+ def share(_shared)
12
+ raise 'You cannot change the shared state of a shared service factory. Extend Nurse::ServiceFactory instead'
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Koine
2
+ module Di
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
data/lib/koine/di.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'koine/di/version'
2
+ require 'koine/di/dependency_container'
3
+ require 'koine/di/service_factory'
4
+ require 'koine/di/shared_service_factory'
5
+
6
+ module Koine
7
+ module Di
8
+ def self.instance
9
+ @instance ||= Koine::Di::DependencyContainer.new
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: koine-di
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Jacobus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-24 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: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
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: minitest-reporters
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: scrutinizer-ocular
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-lcov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
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
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Koine DI, for your dependency injection. Dependency injection container
126
+ email:
127
+ - marcelo.jacobus@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".github/workflows/unit-tests.yml"
133
+ - ".gitignore"
134
+ - ".ruby-version"
135
+ - ".scrutinizer.yml"
136
+ - ".travis.yml"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - koine-di.gemspec
145
+ - lib/koine/di.rb
146
+ - lib/koine/di/dependency_container.rb
147
+ - lib/koine/di/service_factory.rb
148
+ - lib/koine/di/shared_service_factory.rb
149
+ - lib/koine/di/version.rb
150
+ homepage: https://github.com/mjacobus/koine-di
151
+ licenses:
152
+ - MIT
153
+ metadata:
154
+ allowed_push_host: https://rubygems.org
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubygems_version: 3.2.3
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Koine DI, for your dependency injection
174
+ test_files: []