dep-inject 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +3 -2
- data/lib/dep_inject/version.rb +1 -1
- data/lib/dep_inject.rb +14 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb06a052f6302e7e6f6ea046eb6e335d4cc48d862cb2a63c22d78d751183fab4
|
4
|
+
data.tar.gz: e51e0847f08fa11ffc2be4f137b9db5ad978c688f759323e2e2e05036a9ac820
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fe39a9ae5d4247d2c057b546e3ae82619442c51fe977a32c4b5d1c0a1dd8d4fad1308646ee4245e834ac9e85e53c4a588b80b1228678a452589dc4a8fcfeb94
|
7
|
+
data.tar.gz: e03c4819dfd284e84f7c80a4e519e63e07a53299f505ff05f1a57d7724f6d3961defc89af94178abbc6468562f84df05e86be2abddb8855b04b76c9b0ada64be
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Dep-inject
|
2
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.
|
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. By injecting dependencies, the design becomes more flexible and maintainable, allowing for easier updates and replacements of components.
|
4
4
|
|
5
5
|
## Features
|
6
6
|
|
@@ -20,7 +20,7 @@ Then bundle install
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
To use DepInject, include the module in your class, specify your dependencies, and define the
|
23
|
+
To use DepInject, include the module in your class, specify your dependencies, and define the execution method. DepInject will handle the injection of the specified dependencies and enforce that only a valid execution method is exposed as a public method. The execution or trigger methods are `execute` and `call`, use one or the other in your class.
|
24
24
|
|
25
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
26
|
|
@@ -39,6 +39,7 @@ class MockTaskManager
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
require 'dep_inject'
|
42
43
|
class TaskUseCase
|
43
44
|
include DepInject
|
44
45
|
|
data/lib/dep_inject/version.rb
CHANGED
data/lib/dep_inject.rb
CHANGED
@@ -5,30 +5,37 @@ require_relative "dep_inject/version"
|
|
5
5
|
module DepInject
|
6
6
|
class PublicMethodError < StandardError; end
|
7
7
|
|
8
|
+
class DuplicatedExecutionMethodError < StandardError; end
|
9
|
+
|
8
10
|
def self.included(base)
|
9
11
|
base.extend(ClassMethods)
|
10
12
|
end
|
11
13
|
|
12
14
|
module ClassMethods
|
13
|
-
def provide(dependencies={})
|
15
|
+
def provide(dependencies = {})
|
14
16
|
define_singleton_method :build do
|
15
17
|
new(**prepare_dependencies(dependencies))
|
16
18
|
end
|
17
19
|
|
18
20
|
define_method :initialize do |**injected|
|
19
21
|
injected.each do |name, dependency|
|
20
|
-
instance_variable_set("@#{name}", dependency)
|
22
|
+
instance_variable_set(:"@#{name}", dependency)
|
21
23
|
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
+
has_execute = self.class.instance_methods.include?(:execute)
|
26
|
+
has_call = self.class.instance_methods.include?(:call)
|
27
|
+
unless has_execute || has_call
|
28
|
+
raise NotImplementedError, "Class #{self.class} must define an `execution` method"
|
25
29
|
end
|
26
30
|
|
27
|
-
|
28
|
-
|
29
|
-
raise PublicMethodError, "Class #{self.class} should only define `execute` as a public method. Additional public methods: #{public_methods_except_execute.join(', ')}"
|
31
|
+
if has_execute && has_call
|
32
|
+
raise DuplicatedExecutionMethodError, "Class #{self.class} should only define one `execution` trigger"
|
30
33
|
end
|
31
34
|
|
35
|
+
public_methods_except_execute = self.class.public_instance_methods(false) - [:execute, :call]
|
36
|
+
unless public_methods_except_execute.empty?
|
37
|
+
raise PublicMethodError, "Class #{self.class} should only define `execution` trigger as a public method. Additional public methods: #{public_methods_except_execute.join(", ")}"
|
38
|
+
end
|
32
39
|
end
|
33
40
|
end
|
34
41
|
|
@@ -49,6 +56,5 @@ module DepInject
|
|
49
56
|
dependency
|
50
57
|
end
|
51
58
|
end
|
52
|
-
|
53
59
|
end
|
54
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dep-inject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- christophenonato
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: DepInject helps to inject dependencies into your classes, enforcing public
|
14
14
|
method restrictions.
|