simple_injector 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 75751a79a8d5e62050da1e4e8dcf3b9f29d8fc04ce239aeb1dbb0072a99b669c
4
+ data.tar.gz: 5de5aed7094301ef9aa3285fb31250d642033cb98e90a5dca030d87ca20fbb98
5
+ SHA512:
6
+ metadata.gz: d3800a3cc0334ce69431956b6dd9a697917d0fc4c385724a9bd07ab6d301d81a13ff2d29c7ecbd63f69f4dc115e527c16022116a1bd6eac600a9948d71f5bbfa
7
+ data.tar.gz: 9399abafbfa86185477c85117da7d43e85856c97598b2d4400a9053b0100b07e03db978a0a02ba01e58109a34efedb7625870edb3564e32776b8f4ca2b26debe
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-performance
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.7.6
8
+
9
+ Exclude:
10
+ - 'bin/*'
11
+ - 'tmp/*'
12
+ - 'spec/spec_helper.rb'
13
+
14
+ NewCops: enable
15
+
16
+ Metrics:
17
+ Enabled: false
18
+
19
+ RSpec/ExampleLength:
20
+ Enabled: false
data/.rubycritic.yml ADDED
@@ -0,0 +1,7 @@
1
+ path: 'rubycritic'
2
+ no_browser: true
3
+ minimum_score: 95
4
+
5
+ paths:
6
+ - 'lib/'
7
+ - 'spec/lib/'
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.7.6
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in injector.gemspec
6
+ gemspec
7
+
8
+ gem 'byebug'
9
+
10
+ gem 'rake', '~> 13.0'
11
+
12
+ gem 'rspec', '~> 3.0'
13
+
14
+ gem 'rubocop', '~> 1.21'
15
+
16
+ gem 'rubocop-performance'
17
+
18
+ gem 'rubocop-rake'
19
+
20
+ gem 'rubocop-rspec'
21
+
22
+ gem 'rubycritic'
23
+
24
+ gem 'simplecov'
data/Gemfile.lock ADDED
@@ -0,0 +1,134 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ simple_injector (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.3)
10
+ public_suffix (>= 2.0.2, < 6.0)
11
+ ast (2.4.2)
12
+ axiom-types (0.1.1)
13
+ descendants_tracker (~> 0.0.4)
14
+ ice_nine (~> 0.11.0)
15
+ thread_safe (~> 0.3, >= 0.3.1)
16
+ byebug (11.1.3)
17
+ coercible (1.0.0)
18
+ descendants_tracker (~> 0.0.1)
19
+ descendants_tracker (0.0.4)
20
+ thread_safe (~> 0.3, >= 0.3.1)
21
+ diff-lcs (1.5.0)
22
+ docile (1.4.0)
23
+ equalizer (0.0.11)
24
+ erubi (1.12.0)
25
+ flay (2.13.0)
26
+ erubi (~> 1.10)
27
+ path_expander (~> 1.0)
28
+ ruby_parser (~> 3.0)
29
+ sexp_processor (~> 4.0)
30
+ flog (4.6.6)
31
+ path_expander (~> 1.0)
32
+ ruby_parser (~> 3.1, > 3.1.0)
33
+ sexp_processor (~> 4.8)
34
+ ice_nine (0.11.2)
35
+ json (2.6.3)
36
+ kwalify (0.7.2)
37
+ launchy (2.5.2)
38
+ addressable (~> 2.8)
39
+ parallel (1.22.1)
40
+ parser (3.2.2.0)
41
+ ast (~> 2.4.1)
42
+ path_expander (1.1.1)
43
+ public_suffix (5.0.1)
44
+ rainbow (3.1.1)
45
+ rake (13.0.6)
46
+ reek (6.1.4)
47
+ kwalify (~> 0.7.0)
48
+ parser (~> 3.2.0)
49
+ rainbow (>= 2.0, < 4.0)
50
+ regexp_parser (2.7.0)
51
+ rexml (3.2.5)
52
+ rspec (3.12.0)
53
+ rspec-core (~> 3.12.0)
54
+ rspec-expectations (~> 3.12.0)
55
+ rspec-mocks (~> 3.12.0)
56
+ rspec-core (3.12.1)
57
+ rspec-support (~> 3.12.0)
58
+ rspec-expectations (3.12.2)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.12.0)
61
+ rspec-mocks (3.12.5)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.12.0)
64
+ rspec-support (3.12.0)
65
+ rubocop (1.49.0)
66
+ json (~> 2.3)
67
+ parallel (~> 1.10)
68
+ parser (>= 3.2.0.0)
69
+ rainbow (>= 2.2.2, < 4.0)
70
+ regexp_parser (>= 1.8, < 3.0)
71
+ rexml (>= 3.2.5, < 4.0)
72
+ rubocop-ast (>= 1.28.0, < 2.0)
73
+ ruby-progressbar (~> 1.7)
74
+ unicode-display_width (>= 2.4.0, < 3.0)
75
+ rubocop-ast (1.28.0)
76
+ parser (>= 3.2.1.0)
77
+ rubocop-capybara (2.17.1)
78
+ rubocop (~> 1.41)
79
+ rubocop-performance (1.16.0)
80
+ rubocop (>= 1.7.0, < 2.0)
81
+ rubocop-ast (>= 0.4.0)
82
+ rubocop-rake (0.6.0)
83
+ rubocop (~> 1.0)
84
+ rubocop-rspec (2.19.0)
85
+ rubocop (~> 1.33)
86
+ rubocop-capybara (~> 2.17)
87
+ ruby-progressbar (1.13.0)
88
+ ruby_parser (3.20.0)
89
+ sexp_processor (~> 4.16)
90
+ rubycritic (4.7.0)
91
+ flay (~> 2.8)
92
+ flog (~> 4.4)
93
+ launchy (>= 2.0.0)
94
+ parser (>= 2.6.0)
95
+ rainbow (~> 3.0)
96
+ reek (~> 6.0, < 7.0)
97
+ ruby_parser (~> 3.8)
98
+ simplecov (>= 0.17.0)
99
+ tty-which (~> 0.4.0)
100
+ virtus (~> 1.0)
101
+ sexp_processor (4.16.1)
102
+ simplecov (0.22.0)
103
+ docile (~> 1.1)
104
+ simplecov-html (~> 0.11)
105
+ simplecov_json_formatter (~> 0.1)
106
+ simplecov-html (0.12.3)
107
+ simplecov_json_formatter (0.1.4)
108
+ thread_safe (0.3.6)
109
+ tty-which (0.4.2)
110
+ unicode-display_width (2.4.2)
111
+ virtus (1.0.5)
112
+ axiom-types (~> 0.1)
113
+ coercible (~> 1.0)
114
+ descendants_tracker (~> 0.0, >= 0.0.3)
115
+ equalizer (~> 0.0, >= 0.0.9)
116
+
117
+ PLATFORMS
118
+ ruby
119
+ x86_64-linux
120
+
121
+ DEPENDENCIES
122
+ byebug
123
+ rake (~> 13.0)
124
+ rspec (~> 3.0)
125
+ rubocop (~> 1.21)
126
+ rubocop-performance
127
+ rubocop-rake
128
+ rubocop-rspec
129
+ rubycritic
130
+ simple_injector!
131
+ simplecov
132
+
133
+ BUNDLED WITH
134
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Leon Cruz
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,84 @@
1
+ [![Rubocop](https://github.com/leoncruz/injector/actions/workflows/rubocop.workflow.yml/badge.svg)](https://github.com/leoncruz/injector/actions/workflows/rubocop.workflow.yml)
2
+ [![Rubycritic](https://github.com/leoncruz/injector/actions/workflows/rubycritic.workflow.yml/badge.svg)](https://github.com/leoncruz/injector/actions/workflows/rubycritic.workflow.yml)
3
+ [![Tests](https://github.com/leoncruz/injector/actions/workflows/test.workflow.yml/badge.svg)](https://github.com/leoncruz/injector/actions/workflows/test.workflow.yml)
4
+ # SimpleInjector
5
+
6
+ ## Installation
7
+
8
+ Install the gem and add to the application's Gemfile by executing:
9
+
10
+ $ bundle add simple_injector
11
+
12
+ If bundler is not being used to manage dependencies, install the gem by executing:
13
+
14
+ $ gem install simple_injector
15
+
16
+ ## Usage
17
+
18
+ First, declare your contract to your class
19
+
20
+ ```ruby
21
+ class CreateUserContract < SimpleInjector::Contract
22
+ register :api_client, -> { ApiClient.new }
23
+ end
24
+ ```
25
+
26
+ The `register` method receive a key, as a identifier of that instance and a proc that returns the object to be injectable into class
27
+
28
+ ```ruby
29
+ class CreateUser
30
+ include SimpleInjector
31
+
32
+ contract CreateUserContract
33
+
34
+ attr_injector :api_client
35
+
36
+ def initialize(user_params)
37
+ @user_params = user_params
38
+ end
39
+
40
+ def create
41
+ api_client.post @user_params
42
+ end
43
+ end
44
+ ```
45
+ In your service class, include the `SimpleInjector` module. This will be add the `contract` and `attr_injector` methods
46
+
47
+ The `contract` method receives a class, your contract defined previously
48
+
49
+ The `attr_injector` receives the key, has to be the same key defined in the contract and add a method in service class to retrieve the instance.
50
+ If you define more than one instance on contract class, you could add an `attr_injector` for each instance. Ex:
51
+
52
+ ```ruby
53
+ class CreateUserContract < SimpleInjector::Contract
54
+ register :api_client, -> { ApiClient.new }
55
+ register :notify_user, -> { NotifyUser.new }
56
+ register :user_model, -> { User }
57
+ end
58
+
59
+ class CreateUser
60
+ include SimpleInjector
61
+
62
+ contract CreateUserContract
63
+
64
+ attr_injector :api_client
65
+ attr_injector :notify_user
66
+ attr_injector :user_model
67
+
68
+ def initialize(user_params)
69
+ @user_params = user_params
70
+ end
71
+
72
+ def create
73
+ user = user_model.save @user_params
74
+
75
+ api_client.post '/users/create/token', { id: user.id }
76
+
77
+ notify_user.notify(user)
78
+ end
79
+ end
80
+ ```
81
+
82
+ ## License
83
+
84
+ The code is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[rubocop spec]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "simple_injector"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleInjector
4
+ # Module to handle instance registrations
5
+ class Contract
6
+ class << self
7
+ def register(name, callback)
8
+ class_name = to_s
9
+
10
+ Inject.register(class_name, name, callback)
11
+ end
12
+
13
+ def find(name)
14
+ class_name = to_s
15
+
16
+ Inject.find(class_name, name).callback.call
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleInjector
4
+ class InjectableNotFound < StandardError; end
5
+
6
+ # Singleton class to save instances and retrieve
7
+ class Inject
8
+ @injectables = {}
9
+
10
+ class << self
11
+ attr_accessor :injectables
12
+
13
+ def register(contract_name, name, callback)
14
+ (@injectables[contract_name] ||= []) << Injectable.new(name, callback)
15
+ end
16
+
17
+ def find(contract_name, name)
18
+ injectable = (@injectables[contract_name] ||= []).find { |i| i.name == name }
19
+
20
+ raise InjectableNotFound, "An instance with name '#{name}' was not registered" unless injectable
21
+
22
+ injectable
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleInjector
4
+ # Class to handle instances to are injectable
5
+ class Injectable
6
+ attr_accessor :name, :callback
7
+
8
+ def initialize(name, callback)
9
+ @name = name
10
+ @callback = callback
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleInjector
4
+ VERSION = '0.0.2'
5
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'simple_injector/version'
4
+ require_relative 'simple_injector/contract'
5
+ require_relative 'simple_injector/injectable'
6
+ require_relative 'simple_injector/inject'
7
+
8
+ # Entrypoint module
9
+ module SimpleInjector
10
+ def self.included(target)
11
+ target.extend(ClassMethods)
12
+ end
13
+
14
+ # methods to add on included class
15
+ module ClassMethods
16
+ @contractor_class = nil
17
+
18
+ def attr_injector(name)
19
+ injectable = Inject.find(@contractor_class, name)
20
+
21
+ define_method name do
22
+ instance_variable_set(:"@#{name}", injectable.callback.call) unless instance_variable_defined?(:"@#{name}")
23
+
24
+ instance_variable_get(:"@#{name}")
25
+ end
26
+ end
27
+
28
+ def contract(klass)
29
+ @contractor_class = klass.to_s
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/simple_injector/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'simple_injector'
7
+ spec.version = SimpleInjector::VERSION
8
+ spec.authors = ['Leon Cruz']
9
+ spec.email = ['leon.cruz.teixeira@gmail.com']
10
+
11
+ spec.summary = 'A gem to handle dependency injection on ruby applications'
12
+ spec.description = 'A gem to handle dependency injection on ruby applications'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.7.0'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject do |f|
20
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
21
+ end
22
+ end
23
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ # Uncomment to register a new dependency of your gem
27
+
28
+ # For more information and examples about making a new gem, check out our
29
+ # guide at: https://bundler.io/guides/creating_gem.html
30
+ spec.metadata['rubygems_mfa_required'] = 'true'
31
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_injector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Leon Cruz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem to handle dependency injection on ruby applications
14
+ email:
15
+ - leon.cruz.teixeira@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - ".rubycritic.yml"
23
+ - ".tool-versions"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - lib/simple_injector.rb
32
+ - lib/simple_injector/contract.rb
33
+ - lib/simple_injector/inject.rb
34
+ - lib/simple_injector/injectable.rb
35
+ - lib/simple_injector/version.rb
36
+ - simple_injector.gemspec
37
+ homepage:
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ rubygems_mfa_required: 'true'
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.7.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.1.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A gem to handle dependency injection on ruby applications
61
+ test_files: []