dep-inject 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 714cfbce718d38561a3cd9977c26a88a51235795f6b50a81c1673fb2e1be21d6
4
+ data.tar.gz: 5454342a77296850b4dd9a0590f0349ce729f99076cf691f6641c16a9c25530e
5
+ SHA512:
6
+ metadata.gz: 5bfb65dcba2603139df734646ba31a1df627071c2bf176a7906fceac88d1514e10896cb1189a7867d6f50344fc69a1f011b38b2608ec2b9661ce06a61585179e
7
+ data.tar.gz: '09d03a5158cadadd67feadc2d99ae19a5278e532aba65ad0a854e12b01a0916b2b3400608684741b003113a108a2000ceb0b4ec17badb1307cb271854908ce18'
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-10-18
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 chris-nonato
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,68 @@
1
+ # Dep-inject
2
+
3
+ Dep-inject is a simple Ruby gem that allows for clean and flexible dependency injection in Ruby classes. It provides a constructor method (`build`) and enforces a single public method (`execute`) to encourage well-structured use cases and services. The gem raises errors if other public methods are defined, ensuring that classes stay focused on a single responsibility.
4
+
5
+ ## Features
6
+
7
+ - **Dependency Injection**: Easily inject dependencies such as repositories, service objects, etc.
8
+ - **Enforced Interface**: Ensures that only the `execute` method is public.
9
+ - **Error Handling**: Raises specific errors when expected conventions are not followed.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'dep-inject'
17
+ ```
18
+
19
+ Then bundle install
20
+
21
+ ## Usage
22
+
23
+ To use DepInject, include the module in your class, specify your dependencies, and define the execute method. DepInject will handle the injection of the specified dependencies and enforce that only execute is exposed as a public method.
24
+
25
+ Dependencies can be any class or object. DepInject will initialize and inject them into your use case at runtime. The dependencies are injected as instance variables with the same name as the keys passed in provide.
26
+
27
+ ### Basic Example
28
+
29
+ ```ruby
30
+ class MockLogger
31
+ def log(message)
32
+ "Logged: #{message}"
33
+ end
34
+ end
35
+
36
+ class MockTaskManager
37
+ def create_task(name)
38
+ "Task #{name} created"
39
+ end
40
+ end
41
+
42
+ class TaskUseCase
43
+ include DepInject
44
+
45
+ provide(
46
+ logger: MockLogger,
47
+ task_manager: MockTaskManager
48
+ )
49
+
50
+ def execute(task_name)
51
+ task = @task_manager.create_task(task_name)
52
+ @logger.log("Executing #{task}")
53
+ end
54
+ end
55
+
56
+ # Instantiate using constructor method and execute
57
+ usecase = TaskUseCase.build
58
+ usecase.execute("Test Task")
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ This project is intended to be a welcoming space for contribution. Pull requests are welcome.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the MIT License.
68
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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 "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DepInject
4
+ VERSION = "0.1.0"
5
+ end
data/lib/dep_inject.rb ADDED
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dep_inject/version"
4
+
5
+ module DepInject
6
+ class PublicMethodError < StandardError; end
7
+
8
+ def self.included(base)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ def provide(dependencies={})
14
+ define_singleton_method :build do
15
+ new(**prepare_dependencies(dependencies))
16
+ end
17
+
18
+ define_method :initialize do |**injected|
19
+ injected.each do |name, dependency|
20
+ instance_variable_set("@#{name}", dependency)
21
+ end
22
+
23
+ unless self.class.instance_methods.include?(:execute)
24
+ raise NotImplementedError, "Class #{self.class} must define an `execute` method"
25
+ end
26
+
27
+ public_methods_except_execute = self.class.public_instance_methods(false) - [:execute]
28
+ unless public_methods_except_execute.empty?
29
+ raise PublicMethodError, "Class #{self.class} should only define `execute` as a public method. Additional public methods: #{public_methods_except_execute.join(', ')}"
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def prepare_dependencies(dependencies)
38
+ dependencies.transform_values do |dependency|
39
+ instantiate_dependency(dependency)
40
+ end
41
+ end
42
+
43
+ def instantiate_dependency(dependency)
44
+ if dependency.respond_to?(:build)
45
+ dependency.build
46
+ elsif dependency.respond_to?(:new)
47
+ dependency.new
48
+ else
49
+ dependency
50
+ end
51
+ end
52
+
53
+ end
54
+ end
data/sig/dinject.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module DepInject
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dep-inject
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - christophenonato
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-10-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: DepInject helps to inject dependencies into your classes, enforcing public
14
+ method restrictions.
15
+ email:
16
+ - chr.nonato@protonmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".standard.yml"
23
+ - CHANGELOG.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/dep_inject.rb
28
+ - lib/dep_inject/version.rb
29
+ - sig/dinject.rbs
30
+ homepage: https://github.com/christophenonato/dep-inject
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/christophenonato/dep-inject
35
+ source_code_uri: https://github.com/christophenonato/dep-inject
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 3.0.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.5.16
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: A lightweight dependency injection gem
55
+ test_files: []